Posted on December 15, 2014, 8:25 am by about-dev.com
I had a huge problem in understanding how the GIT works. I was probably stuck in thinking in the SVN way.
I have found some very useful presentations describing some possible workflows that should help you understand how GIT was designed to work, at a macroscopic level.
I have also found a very helpful interactive HowTo that should introduce you to make the first steps in working with GIT and teach you a few basic commands. I followed it twice and I have extracted the basic commands and I have written them down with some explanations beside them.
After learning the basic commands I found myself in the position of needing something more and I have looked for some other tutorials and I have found one. This one teaches you the commands in some how the order that you will need them. After reading this tutorial, I have completed a lot more commands in my list and the explanations necessary for use near them.
These were mostly enough for me to begin working with GIT. From now on I have searched for some specific needs from GIT almost daily, but this wasn't a big problem. I think this is very normal.
But I still feel the need to better understand how the GIT works at a microscopic level. And I have found some useful diagrams.
And the most enlightening.
And a few more diagrams:
After analysing all of the diagrams above, I have decided to make my own diagram making a mix from all of them and adding a little bit from my way of seeing the things.
Eventually I have found very useful this online book about git.
Assuming that you already know what is a versioning system and you want to learn how to use GIT, I would suggest you to follow a few steps.
1. learn the basic used terms: repository, branch, stash, stage, working directory, add, commit, push, pull, merge, fetch, checkout, clone...
2. place the terms on the diagram (http://java.jiderhamn.se/wp-content/uploads/Git_visual_overview.pdf). I would add the stash to the diagram.
3. see some possible workflows https://www.atlassian.com/git/tutorials/comparing-workflows/
4. follow the interactive howto https://try.github.io/levels/1/challenges/1
5. learn the basic commands from http://git-scm.com/docs/gittutorial
6. put the learnt stuff into practice and keep close a list with the most common commands
PLACES: repository, branch, stash, stage, working directory
- the working directory is the place where you directly make the changes to the files (where you write the code, add/edit/rename/delete the files...)
- when you add new files, edit or delete existent files, these actions are done independent of any version control system, but you can choose to add the changes to versioning system afterwards
- repository is the place where git store the versions of the source files
- repository can be remote or local (you can set multiple remote repositories, but you can have only one local repository)
- you can connect one or more remote repositories to a local one
- branch is a version of the source files
- multiple branches can be stored in a repository
- stash is a local storage place for uncommited changes
- a repository can have multiple stashes
- the stash is very useful when you need to switch branches and you have unfinished work in the working directory
- stage is the place where you temporary place the changes before sending them to a local branch
- you can have multiple changed files but you can choose to commit to a branch just a part of them, and you can do this by adding just the wanted changes to stage
ACTIONS: init, clone, add files, commit, checkout, fetch, push, pull, merge (in general actions that create/update/delete places or make the connections between different places)
Init: initialize a local repository in the current folder; command = git init
Clone: clone a remote repository into a local folder creating a local repository; command = git clone
Add: add the local changes to stage
Commit: add the changes from stage to local branch
Checkout: switch between branches
Fetch: retrieve the list of branches toghether with the history of changes from a remote repository
Push: send the changes from a local branch to a remote branch
Pull: retrieve the changes from a remote branch to a local branch
Merge: overlap the changes from another branch to the current one
RELATIONS BETWEEN PLACES:
working directory - stage:
- you can add changed files to stage for an eventual commit; command = git add
- you can remove the staged files receving just the flag changed in the working folder
working directory - local branch:
- you can switch from a local branch to another local branch, resulting the change of the working directory files: command = git checkout
working directory - stash:
- you can move changes from working directory to stash; command = git stash
- you can retrieve changes from stash into working directory; command = git stash apply
stage - local branch:
- you can send the changes from stage to local branch; command = git commit
- you can reset a commit from local branch, retrieving thos changes into stage; command = git reset
local branch - remote repository:
- you can send changes from a local branch to a remote branch; command = git push
- you can retrieve changes from a remote branch to a local branch; command = git pull
LIST OF COMMON COMMANDS:
|git init||#initializes a local repository in the current folder|
|git clone $remoteRepositoryUrl $folderPath||#clones a remote repository ($remoteRepositoryUrl) to a local folder ($folderPath)|
|git status||#shows the list of changes in working directory and in stage versus the current branch|
|git branch $branchName||#creates a local branch with the name $branchName using as starting point the current branch|
|git branch -D $branchName||#deletes the local branch $branchName|
|git branch -v||#shows the list of branches from local repository|
|git branch -a||#shows the list of branches from all conencted repositories, local or remote, that were previously fetched|
|git remote add $repositoryAlias "$remoteRepositoryUrl"||#creates an alias ($repositoryAlias) for a remote repository ($remoteRepositoryUrl)|
|git remote rm $repositoryAlias||#removes an alias ($repositoryAlias) for a remote repository|
|git remote -v||#shows the list of all connected remote repositories|
|git checkout $branchName||#switch to the branch $branchName (if it exists)|
|git checkout -b $branchName||#creates the branch $branchName referenced to the current one and automatically switches to it|
|git checkout $fileName||#restores the file $fileName from the previous commit|
|git checkout -- $fileName||#restores the file $fileName from the previous commit (use -- if you have a branch named as the $fileName)|
|git fetch $repositoryAlias||#retrieves the list of branches and changes form a remote repository|
|git fetch -a||#retrieves the list of branches and changes from all connected remote repositories|
|git stash||#store the uncommited changes to a stash|
|git stash apply||#retrieve the changes from a stash (from the last one)|
|git stash list||#list all the stashes created|
|git stash drop||#deletes a stash (the last one)|
|git add $fileName||#adds a changed file to stage|
|git rm $fileName||#adds a deleted file to stage|
|git rm --cached $fileName||#adds to stage the deletion of the $fileName without deleting it from the working folder|
|git commit -m "message"||#commits the changes from stage to the current branch|
|git push $repositoryAlias $branchName||#send the changes from the local branch $branchName to remote repository $repositoryAlias|
|git push $repositoryAlias :$branchName||#deletes the branch $branchName from the remote repository $repositoryAlias|
|git pull $repositoryAlias $branchName||#retrieve the changes from the repository $repositoryAlias to the local branch $branchName|
|git merge $branchName||#overlap the changes from the branch $branchName to the current branch|
|git reset $fileName||#removes the file $fileName from stage|
|git reset --soft HEAD~1||#undo the last commit, keeping the changes in the working tree|
|git reset --hard HEAD^||#resets the current branch and the working folder to the state of the previous commit|
|git reset --hard $commitId||#resets the current branch and the working folder to the state of the commit $commitId|