Git Cheatsheet

This prob­a­bly serves as a quick git ref­er­ence or cheat­sheet. I used to main­tain this when I was new to git. Prob­a­bly still help­ful for begin­ners out there.

Revert all for files added but not com­mit­ted
git check­out .

Adding/Staging files
git add .

Com­mit­ting with mes­sage
com­mit ‑m “my mes­sage”

Make new branch (local)
git check­out ‑b new_branch

Then make that branch on serv­er ready to push
git push ‑u ori­gin feature_branch_name
Or sim­ply
git push –all ‑u (to push all your local branch­es to serv­er and set track­ing for them too)

Change from one branch to anoth­er
git check­out another_branch_name

Merge branch­es
Sup­pose you have branched ‘mas­ter’ and ‘feature1’ and you want to bring the con­tents of ‘mas­ter’ into ‘feature1’, means you want to update your ‘feature1’ branch, then you do -

git check­out feature1
git merge mas­ter

if you want to bring in the con­tents of ‘feature1’ into ‘mas­ter’, when your ‘feature1’ work is done, then you do

git check­out mas­ter
git merge feature1

In fact, this 2 step process is the bet­ter way to merge your fea­ture branch­es into mas­ter

In order to resolve con­flicts, you might have to do
git merge­tool

After all this is done, you do a com­mit merge and push from mas­ter

Equiv­a­lent of hg out­go­ing
If you want to list com­mits that are on your local branch dev, but not the the remote branch origin/dev, do:
git fetch ori­gin # Update origin/dev if need­ed
git log origin/

Equiv­a­lent of git incom­ing
git fetch ori­gin # Update origin/dev if need­ed
git log dev..origin/dev

See the his­to­ry of a file

gitk /pathtofile/

When you want to set­up a new repo in Github and already have some code in local

Cre­ate the remote repos­i­to­ry, and get the URL such as or, add readme or .git­ig­nore or what­ev­er you want
Local­ly, at the root direc­to­ry of your source, git init
git pull {url from step 1}
git add . then git com­mit ‑m ‘ini­tial com­mit com­ment’
git remote add ori­gin [URL From Step 1]
git pull ori­gin mas­ter
git push ori­gin mas­ter

Pull all branch­es to local

git fetch –all
git pull –all

List all branch­es

git branch ‑a –list all local branch­es
git branch ‑r –list all remote branch­es

and then do a sim­ple git check­out full­branch­name to move into that branch

Pull cer­tain branch from serv­er

git pull ori­gin brnach-name

Set­ting up git merge­tool on win­dows

down­load kdiff3 exe, install it

open git­con­fig file in C:\Program Files (x86)\Git\etc

open git­con­fig using cmd in Admin mode, by notepad git­con­fig com­mand

add the fol­low­ing there -

tool = kdiff3

[merge­tool “kdiff3”]
path = C:/Program Files/KDiff3/kdiff3.exe
keep­Back­up = false
trustEx­it­Code = false

save it, close it

on git bash do
git con­fig –glob­al merge.tool kdiff3


Just down­load and install kdiff 3
And do the fol­low­ing

$ git con­fig –glob­al –add merge.tool kdiff3
$ git con­fig –glob­al –add mergetool.kdiff3.path “C:/Program Files/KDiff3/kdiff3.exe”
$ git con­fig –glob­al –add mergetool.kdiff3.trustExitCode false
$ git con­fig –glob­al –add diff.guitool kdiff3
$ git con­fig –glob­al –add difftool.kdiff3.path “C:/Program Files/KDiff3/kdiff3.exe”
$ git con­fig –glob­al –add difftool.kdiff3.trustExitCode false
Delete a branch from local

git branch ‑D branch_name

Delete a branch from remote

git push ori­gin –delete <branch­Name>

Delete untracked file
git clean ‑f file­name­with­path
or git clean ‑f ‑d file­name­with­path


git clean ‑f ‑n to show what files will be removed
git clean ‑f to actu­al­ly remove those

use git clean ‑fd to remove untracked direc­to­ries

use git sta­tus to check whether some­thing left untracked or not

Reverse a com­mit­ted push
git reverse <commit’s hash> will cre­ate a new com­mit, which you’ll have to push

Cher­ry pick a com­mit
Lets say you want to cher­ry pick com­mit 6a23b56 from fea­ture branch to mas­ter. You must be in mas­ter and then do
git cher­ry-pick ‑x 6a23b56
that’s all !

Remov­ing Files
Say that u delet­ed a file from disk, now it will show as delet­ed in git sta­tus. How to make that change on serv­er also?
git rm <file path and name>
git com­mit
git push

Mov­ing repo from one loca­tion to anoth­er (or dupli­cat­ing repo to some new loca­tion)
git remote add new_repo_name new_repo_url
Then push the con­tent to the new loca­tion
git push new_repo_name mas­ter
Final­ly remove the old one
git remote rm ori­gin
After that edit the.git/config file to change the new_repo_name to ori­gin.

If you don’t remove the ori­gin (orig­i­nal remote repos­i­to­ry), you can sim­ply just push changes to the new repo with
git push new_repo_name mas­ter

If you delete a file in one branch and don’t com­mit or stash then those files will appear delet­ed on oth­er branch­es
Switch­ing branch­es car­ries uncom­mit­ted changes with you. Either com­mit first, run git check­out . to undo them, or run git stash before switch­ing. (You can get your changes back with git stash apply

Revert a spe­cif­ic file to some ear­li­er git ver­sion
Find the com­mit where you went wrong, either using git log or git lop ‑p or gitk
Find the com­mit hash
git check­out com­mit­code filepath

Now com­mit again

Com­mit a sin­gle file
git com­mit ‑m ‘com­ments’ filepath

SSH set­up between ur machine’s repo and serv­er repo
Cd to home dir
ssh-key­gen ‑t rsa ‑C “”
clip < ~/.ssh/

Save on serv­er

How to see my last n com­mits
git log ‑n 5 –author=vaibhavk

How to see con­tents of a par­tic­u­lar com­mit
git show hash­val­ue

What’s A, B and C in Kdiff3
A is the orig­i­nal file, before any merge con­flicts hap­pened
B is the your cur­rent file (includ­ing any uncom­mit­ted changes)
C is the incom­ing file that caused merge con­flict

How to see uncom­mit­ted changes for a spe­cif­ic file against ear­li­er ver­sion(?)
git diff filepath

Undo git add
Git reset filepath

See if a com­mit is in a branch or its in what branch­es
git branch ‑a –con­tains 4f08c85ad (remove ‑a to see only your local branch­es)
List git com­mits not pushed yet
git log origin/master..master
Or git log <since>..<until>
You can use this with grep to check for a spe­cif­ic, known com­mit:
git log <since>..<until> | grep <com­mit-hash>

Undo last com­mit

git com­mit ‑m “Some­thing ter­ri­bly mis­guid­ed”
git reset HEAD~
« edit files as nec­es­sary »
git add <what­ev­er>
git com­mit ‑c ORIG_HEAD

Leave a Reply

Your email address will not be published. Required fields are marked *