Git: A Handy Guide for Beginners

When I was new to git I was looking for a place where I could find all the basic git commands in one place.  Now, that I am slightly acquainted with it I decided to create one myself, which I can refer to and others can benefit from it too.

My main sources for learning git were stackoverflow, Git-Ref, and Git-SCM . Although, there are a number of awesome GUI based tools available like EGit and Source Tree I prefer using a combination of the Git Bash and Git Gui tools that come bundled with the default Git software for windows. Although, the other tools make the task very easy and quick, you don’t get enough information if something goes wrong. And most help for Git is available for the command line version and not for the tools.

Enough, of rambling and onwards with the guide.

Initial Setup

1]Initializing a git repository in an existing directory
git init

2] Push up an existing repository
cd /path/to/my/repo
git remote add origin https://asloobq@bitbucket.org/asloobq/test.git
git push -u origin --all # pushed up the repo and its refs for the first time
git push -u origin --tags # pushed up any tags
That’s two hyphens (-) before all and tags, btw.

3] Downloading the code first time (cloning repository).
git clone [url]
eg. git clone https://github.com/asloobq/cocos2d-x-CCLayerPanZoom

4] Setting up user info.
git config --global user.name "John Doe"
git config --global user.email johndoe@gmail.com
That’s two hyphens (-) before global, btw.

Basic Committing, Reverting, etc.

1] Check which files have changed in current working copy
git status

2] Add new files to staging area (staging area has list of files which will be committed ).
git add [file path]

3] Remove files from staging.
git reset HEAD [file path]

4] Commit changes.
git commit -m "commit message"

5] Undo the previous commit but leave changes as it is.
git reset --soft HEAD^

6] Revert a particular file to a particular commit
git checkout [commit SHA id] [filepath]
eg. git checkout 0624b44a447db26862acc635cc69e83f16e610f2 Classes/Main.cpp

7] Revert changes on a file to the current revision
git checkout [filepath]

8] Amend a commit. Suppose you made a commit in which you added the wrong files or you left out the correct ones. Then, you just need to ‘git add’ the correct files and ‘git rm’ the wrong files and say amend.
git rm wrongfile.cpp
git add correctfile.cpp
git commit --amend

Branching

1] Get list of local branches.
git branch

2] Get list of remote branches.
git branch -r

3] Create a local branch to track a remote branch (Basically create a copy of the remote branch so that you can work on it).
git branch -b [local-branch-name] [remote-branch-name]
eg. git branch -b develop origin/develope

4] Switch to a local branch.
git checkout [local-branch-name]
Checkout is a very useful command, you can use it to switch to branch, revert files, create branches, etc.

5] Push branch. When you do a push/pull/fetch, you are trying to sync the git repo on the server with your local repo. A push will transfer your changes on the local branch to the remote branch.
git push origin [local-branch-name]
(Assuming this local branch is already setup to track a remote branch, if not it will create a new branch on the server)

6] Pull changes. This is the git equivalent of ‘SVN Update’. It downloads the latest commit from the server and merges it with your local branch.
git pull origin [local-branch-name]

Pull rebase. This is a better version of pull. It reverts your local repo to the point of last push from your side. Then, it downloads the commits from the server and applies it on your repo. And then, it applies your new commits over the repo. This essentially results in less no of merges and a clean trunk. Be sure to have your working directory clean by stashing it before doing a rebase.
git pull --rebase
In case, you have to resolve a merge, you can do the same and use git rebase --continue to exit.

7] Fetch. This will do the half the job that ‘Pull’ does, ie. get the latest commits for all the branches but it will not merge them into your local copy. Good to see which branches have been updated.
git fetch

8] Reset branch to older revision.
git checkout [commit SHA id]
This will change the code, but will leave you without any branch (you cant commit unless you are on a branch). So this is just to View the state of the repo at that commit.
If you want to branch off from that state (so that you can make commits on it) use
git checkout -b [local-branch-name] [commit SHA id]

9] Reset branch to current revision (clear all changes).
git reset --hard #Revert changes to modified files.
git clean -fd # Remove all untracked files and directories.

10] Copy changes from another branch up to a particular commit.
git merge [commit SHA id]
Scenario: You are on your deploy branch and want to pull in some changes from the master branch, but not merge the two branches together then you can simply use git merge [commit SHA id of the commit in master].

11] Cherry pick one commit from another branch
git cherry-pick [commit SHA id]
If you need to *copy* just one commit from another branch to your current branch, use this command. The commit will be cherry-picked and applied to the Head of your current branch.

12] Merge one branch into another.
git merge [local-branch-name]

13] Resolve merge conflict. I recommend installing TortoiseSvn which comes with the tortoisemerge tool.
git mergetool

Miscellaneous

1] Stash changes, if you don’t want to commit to them and would like to switch branch or pull changes from server.
git stash

2] Pop Stash
git stash pop

…More commands are on the way. If you’d like me to add anything to the list or if I have made a mistake anywhere, do let me know. Happy Coding!

Pro Tip: Read the git messages carefully. More often than not, it gives you commands to undo the action or to do the next logical thing.

Leave a comment