Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
My simply Git Cheatsheet

Using Git

Global Settings

Reminder

Press minus + shift + s and return to chop/fold long lines!

Show folder content: ls -la

Notes

Do not put (external) dependencies in version control!

Setup

See where Git is located: which git

Get the version of Git: git --version

Create an alias (shortcut) for git status: git config --global alias.st status

Help

Help: git help

General

Initialize Git: git init

Get everything ready to commit: git add .

Get custom file ready to commit: git add index.html

Commit changes: git commit -m "Message"

Add and commit in one step: git commit -am "Message"

Remove files from Git: git rm index.html

Update all changes: git add -u

Remove file but do not track anymore: git rm --cached index.html

Move or rename files: git mv index.html dir/index_new.html

Undo modifications (restore files from latest commited version): git checkout -- index.html

Restore file from a custom commit (in current branch): git checkout 6eb715d -- index.html

Reset

Go back to commit: git revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Soft reset (move HEAD only; neither staging nor working dir is changed): git reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Undo latest commit: git reset --soft HEAD~

Mixed reset (move HEAD and change staging to match repo; does not affect working dir): git reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Hard reset (move HEAD and change staging dir and working dir to match repo): git reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Update & Delete

Test-Delete untracked files: git clean -n

Delete untracked files (not staging): git clean -f

Unstage (undo adds): git reset HEAD index.html

Update most recent commit (also update the commit message): git commit --amend -m "New Message"

Branch

Show branches: git branch

Create branch: git branch branchname

Change to branch: git checkout branchname

Create and change to new branch: git checkout -b branchname

Rename branch: git branch -m branchname new_branchname or: git branch --move branchname new_branchname

Show all completely merged branches with current branch: git branch --merged

Delete merged branch (only possible if not HEAD): git branch -d branchname or: git branch --delete branchname

Delete not merged branch: git branch -D branch_to_delete

Merge

True merge (fast forward): git merge branchname

Merge to master (only if fast forward): git merge --ff-only branchname

Merge to master (force a new commit): git merge --no-ff branchname

Stop merge (in case of conflicts): git merge --abort

Stop merge (in case of conflicts): git reset --merge // prior to v1.7.4

Merge only one specific commit: git cherry-pick 073791e7

Rebase: git checkout branchname » git rebase master or: git merge master branchname (The rebase moves all of the commits in master onto the tip of branchname.)

Squash multiple commits into one: git rebase -i HEAD~3 (source)

Stash

Put in stash: git stash save "Message"

Show stash: git stash list

Show stash stats: git stash show stash@{0}

Show stash changes: git stash show -p stash@{0}

Use custom stash item and drop it: git stash pop stash@{0}

Use custom stash item and do not drop it: git stash apply stash@{0}

Delete custom stash item: git stash drop stash@{0}

Delete complete stash: git stash clear

Gitignore & Gitkeep

About: https://help.github.com/articles/ignoring-files

Useful templates: https://github.com/github/gitignore

Add or edit gitignore: nano .gitignore

Track empty dir: touch dir/.gitkeep

Log

Show commits: git log

Show oneline-summary of commits: git log --oneline

Show oneline-summary of commits with full SHA-1: git log --format=oneline

Show oneline-summary of the last three commits: git log --oneline -3

Show only custom commits: git log --author="Sven" git log --grep="Message" git log --until=2013-01-01 git log --since=2013-01-01

Show only custom data of commit: git log --format=short git log --format=full git log --format=fuller git log --format=email git log --format=raw

Show changes: git log -p

Show every commit since special commit for custom file only: git log 6eb715d.. index.html

Show changes of every commit since special commit for custom file only: git log -p 6eb715d.. index.html

Show stats and summary of commits: git log --stat --summary

Show history of commits as graph: git log --graph

Show history of commits as graph-summary: git log --oneline --graph --all --decorate

Compare

Compare modified files: git diff

Compare modified files and highlight changes only: git diff --color-words index.html

Compare modified files within the staging area: git diff --staged

Compare branches: git diff master..branchname

Compare branches like above: git diff --color-words master..branchname^

Compare commits: git diff 6eb715d git diff 6eb715d..HEAD git diff 6eb715d..537a09f

Compare commits of file: git diff 6eb715d index.html git diff 6eb715d..537a09f index.html

Compare without caring about spaces: git diff -b 6eb715d..HEAD or: git diff --ignore-space-change 6eb715d..HEAD

Compare without caring about all spaces: git diff -w 6eb715d..HEAD or: git diff --ignore-all-space 6eb715d..HEAD

Useful comparings: git diff --stat --summary 6eb715d..HEAD

Blame: git blame -L10,+1 index.html

Releases & Version Tags

Show all released versions: git tag

Show all released versions with comments: git tag -l -n1

Create release version: git tag v1.0.0

Create release version with comment: git tag -a v1.0.0 -m 'Message'

Checkout a specific release version: git checkout v1.0.0

Collaborate

Show remote: git remote

Show remote details: git remote -v

Add remote upstream from GitHub project: git remote add upstream https://github.com/user/project.git

Add remote upstream from existing empty project on server: git remote add upstream ssh://root@123.123.123.123/path/to/repository/.git

Fetch: git fetch upstream

Fetch a custom branch: git fetch upstream branchname:local_branchname

Merge fetched commits: git merge upstream/master

Remove origin: git remote rm origin

Show remote branches: git branch -r

Show all branches: git branch -a

Compare: git diff origin/master..master

Push (set default with -u): git push -u origin master

Push: git push origin master

Force-Push: `git push origin master --force

Pull: git pull

Pull specific branch: git pull origin branchname

Clone to localhost: git clone https://github.com/user/project.git or: git clone ssh://user@domain.com/~/dir/.git

Clone to localhost folder: git clone https://github.com/user/project.git ~/dir/folder

Clone specific branch to localhost: git clone -b branchname https://github.com/user/project.git

Delete remote branch (push nothing): git push origin :branchname or: git push origin --delete branchname

Archive

Create a zip-archive: git archive --format zip --output filename.zip master

Export/write custom log to a file: git log --author=sven --all > log.txt

Troubleshooting

Ignore files that have already been committed to a Git repository: http://stackoverflow.com/a/1139797/1815847

Security

Hide Git on the web via .htaccess: RedirectMatch 404 /\.git (more info here: http://stackoverflow.com/a/17916515/1815847)

Large File Storage

Website: https://git-lfs.github.com/

Install: brew install git-lfs

Track *.psd files: git lfs track "*.psd" (init, add, commit and push as written above)

@yolayne

This comment has been minimized.

Show comment Hide comment
@yolayne

yolayne Jan 27, 2015

Small typo under "General" section:

Get verything ready to commit: git add .

yolayne commented Jan 27, 2015

Small typo under "General" section:

Get verything ready to commit: git add .

@wreckday

This comment has been minimized.

Show comment Hide comment
@wreckday

wreckday Feb 11, 2015

good one!

good one!

@Bhavik3

This comment has been minimized.

Show comment Hide comment
@Bhavik3

Bhavik3 Feb 12, 2015

it helps a lot for me ....thanks

Bhavik3 commented Feb 12, 2015

it helps a lot for me ....thanks

@hofmannsven

This comment has been minimized.

Show comment Hide comment
@hofmannsven

hofmannsven Feb 16, 2015

Thanks, also updated the typo 😊

Owner

hofmannsven commented Feb 16, 2015

Thanks, also updated the typo 😊

@boonchu

This comment has been minimized.

Show comment Hide comment
@boonchu

boonchu Feb 16, 2015

I thought about quick manual for git. You did it. Thanks.

boonchu commented Feb 16, 2015

I thought about quick manual for git. You did it. Thanks.

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Mar 17, 2015

Great work. Any of the branch stuff work for releases? When migrating for example, I am able to run (in Unix) for remote ingit branch -r | grep -v '->'; do git branch --track $remote; done and then upon a push, get all of my branches to the new repo. Any shortcuts for the releases?

ghost commented Mar 17, 2015

Great work. Any of the branch stuff work for releases? When migrating for example, I am able to run (in Unix) for remote ingit branch -r | grep -v '->'; do git branch --track $remote; done and then upon a push, get all of my branches to the new repo. Any shortcuts for the releases?

@SekharBeri

This comment has been minimized.

Show comment Hide comment
@SekharBeri

SekharBeri Mar 31, 2015

Great work it helps me lot thank you

Great work it helps me lot thank you

@ryrych

This comment has been minimized.

Show comment Hide comment
@ryrych

ryrych May 1, 2015

Thanks for the info how to chop long files! It is especially useful for generated translations.js - every time you make a little change in a translation first you get hundreds of lines before and then the same amount of lines after (as there's only one line in the file)

ryrych commented May 1, 2015

Thanks for the info how to chop long files! It is especially useful for generated translations.js - every time you make a little change in a translation first you get hundreds of lines before and then the same amount of lines after (as there's only one line in the file)

@jfmercer

This comment has been minimized.

Show comment Hide comment
@jfmercer

jfmercer Jun 5, 2015

Great work. Thanks.

jfmercer commented Jun 5, 2015

Great work. Thanks.

@adampmoss

This comment has been minimized.

Show comment Hide comment
@adampmoss

adampmoss Aug 18, 2015

Thanks for this. Bookmarked.

Thanks for this. Bookmarked.

@tomer-ben-david

This comment has been minimized.

Show comment Hide comment
@tomer-ben-david

tomer-ben-david Sep 7, 2015

Best git cheatsheet, bookmarked.

Best git cheatsheet, bookmarked.

@tomer-ben-david

This comment has been minimized.

Show comment Hide comment
@tomer-ben-david

tomer-ben-david Sep 8, 2015

I tend to use this alot, do you as well?

Show working tree status and ignore untracked files:
git status -uno

I tend to use this alot, do you as well?

Show working tree status and ignore untracked files:
git status -uno

@boobiebewbs

This comment has been minimized.

Show comment Hide comment
@boobiebewbs

boobiebewbs Sep 23, 2015

how do I set myself as a normal branch not a master branch?

how do I set myself as a normal branch not a master branch?

@NZOzzDeveloper

This comment has been minimized.

Show comment Hide comment
@NZOzzDeveloper

NZOzzDeveloper Jan 28, 2016

thanks for the cheat sheet :)

thanks for the cheat sheet :)

@baluragala

This comment has been minimized.

Show comment Hide comment
@baluragala

baluragala Feb 2, 2016

Superb !!! Great Cheatsheet..

Superb !!! Great Cheatsheet..

@jsroyal

This comment has been minimized.

Show comment Hide comment
@jsroyal

jsroyal Feb 6, 2016

Awesome!!

jsroyal commented Feb 6, 2016

Awesome!!

@kropiv

This comment has been minimized.

Show comment Hide comment
@kropiv

kropiv May 23, 2016

thanks a lot!

kropiv commented May 23, 2016

thanks a lot!

@developersamim

This comment has been minimized.

Show comment Hide comment
@developersamim

developersamim Jun 28, 2016

very helpful... awesome tutorial

very helpful... awesome tutorial

@mhdphp

This comment has been minimized.

Show comment Hide comment
@mhdphp

mhdphp Jul 3, 2016

Excellent... thanks!

mhdphp commented Jul 3, 2016

Excellent... thanks!

@kishorboddu

This comment has been minimized.

Show comment Hide comment
@kishorboddu

kishorboddu Jul 4, 2016

thanks a lot, very helpful

thanks a lot, very helpful

@adharjain

This comment has been minimized.

Show comment Hide comment
@adharjain

adharjain Jul 12, 2016

Awsome Help!

Awsome Help!

@tiennv90

This comment has been minimized.

Show comment Hide comment
@tiennv90

tiennv90 Aug 8, 2016

thank you very much for making this script

tiennv90 commented Aug 8, 2016

thank you very much for making this script

@rjangheldotcom

This comment has been minimized.

Show comment Hide comment
@rjangheldotcom

rjangheldotcom Aug 8, 2016

nice work, thanks.

nice work, thanks.

@Brentophillips

This comment has been minimized.

Show comment Hide comment
@Brentophillips

Brentophillips Oct 12, 2016

Best list I've found, thanks! I have a large folder in a GitHub repo master branch with too many pngs inside to delete manually online. Any chance you can go through the steps, in order and for a beginner, showing how, after I've cloned my repo on my desktop, I can delete a folder and return the repo?

Best list I've found, thanks! I have a large folder in a GitHub repo master branch with too many pngs inside to delete manually online. Any chance you can go through the steps, in order and for a beginner, showing how, after I've cloned my repo on my desktop, I can delete a folder and return the repo?

@Nzalo

This comment has been minimized.

Show comment Hide comment
@Nzalo

Nzalo Oct 18, 2016

thanks for this 👍

Nzalo commented Oct 18, 2016

thanks for this 👍

@phaniapsr

This comment has been minimized.

Show comment Hide comment
@phaniapsr

phaniapsr Nov 11, 2016

Covered all basic commands.. Thanks alot

Covered all basic commands.. Thanks alot

@stevexyz

This comment has been minimized.

Show comment Hide comment
@stevexyz

stevexyz Dec 5, 2016

Get everything ready to commit including deletion and renaming: git add -A .

stevexyz commented Dec 5, 2016

Get everything ready to commit including deletion and renaming: git add -A .

@stevexyz

This comment has been minimized.

Show comment Hide comment
@stevexyz

stevexyz Dec 5, 2016

See the last actions on the repo: "git reflog"

stevexyz commented Dec 5, 2016

See the last actions on the repo: "git reflog"

@khalidrizvi

This comment has been minimized.

Show comment Hide comment
@khalidrizvi

khalidrizvi Jan 25, 2017

very helpful

very helpful

@srinivas1787

This comment has been minimized.

Show comment Hide comment
@srinivas1787

srinivas1787 Jan 27, 2017

Awesome Sir.... Really very help ful :)

Awesome Sir.... Really very help ful :)

@fazlearefin

This comment has been minimized.

Show comment Hide comment
@fazlearefin

fazlearefin Feb 4, 2017

Get everything ready to commit: git add .

If you are not in the base of the git repo, this command does not add all files to staging area. Better to use

git add -A

Get everything ready to commit: git add .

If you are not in the base of the git repo, this command does not add all files to staging area. Better to use

git add -A

@Rongcong

This comment has been minimized.

Show comment Hide comment
@Rongcong

Rongcong Feb 8, 2017

It's brilliant. Thanks for your contribution!

Rongcong commented Feb 8, 2017

It's brilliant. Thanks for your contribution!

@jackbraj

This comment has been minimized.

Show comment Hide comment
@jackbraj

jackbraj Feb 14, 2017

There is a typo in "Merge to master (forc a new commit): git merge --no-ff branchname".
It should be force?

There is a typo in "Merge to master (forc a new commit): git merge --no-ff branchname".
It should be force?

@SARAN-thala

This comment has been minimized.

Show comment Hide comment
@SARAN-thala

SARAN-thala Feb 19, 2017

Nice one.
It's very useful for beginners to Git.

Nice one.
It's very useful for beginners to Git.

@JaharshKotha

This comment has been minimized.

Show comment Hide comment
@JaharshKotha

JaharshKotha Feb 23, 2017

What about going to different versions in the log ?

What about going to different versions in the log ?

@mohammedfouad90

This comment has been minimized.

Show comment Hide comment
@mohammedfouad90

mohammedfouad90 Mar 16, 2017

Good work man .. Thanks :)

Good work man .. Thanks :)

@ileontiuc

This comment has been minimized.

Show comment Hide comment
@ileontiuc

ileontiuc Mar 31, 2017

Hi! very nice overview!
do you know if there a way of checking whether the changes from a specific commit are still maintained in another commit? (via git terminal)

Hi! very nice overview!
do you know if there a way of checking whether the changes from a specific commit are still maintained in another commit? (via git terminal)

@imusman

This comment has been minimized.

Show comment Hide comment
@imusman

imusman Apr 12, 2017

@sipoma

This comment has been minimized.

Show comment Hide comment
@sipoma

sipoma Apr 28, 2017

Well Done!!! Very Very Useful

sipoma commented Apr 28, 2017

Well Done!!! Very Very Useful

@KateGH

This comment has been minimized.

Show comment Hide comment
@KateGH

KateGH May 12, 2017

Thank you for sharing, really useful!!

KateGH commented May 12, 2017

Thank you for sharing, really useful!!

@wiseosho

This comment has been minimized.

Show comment Hide comment
@wiseosho

wiseosho May 30, 2017

Thank you for sharing!.

Thank you for sharing!.

@huoxudong125

This comment has been minimized.

Show comment Hide comment
@edwinpopham

This comment has been minimized.

Show comment Hide comment
@edwinpopham

edwinpopham Aug 9, 2017

Cool easy place to get a reminder about some commands

Cool easy place to get a reminder about some commands

@parameshnalla

This comment has been minimized.

Show comment Hide comment
@parameshnalla

parameshnalla Aug 21, 2017

Great work. Thank you

Great work. Thank you

@michaeljwiebe

This comment has been minimized.

Show comment Hide comment
@michaeljwiebe

michaeljwiebe Sep 11, 2017

Awesome, thanks for this resource! Just one question here, this seems like it can't be right since its the same command...

Commit to most recent commit: git commit --amend -m "Message"
Update most recent commit message: git commit --amend -m "New Message"

Awesome, thanks for this resource! Just one question here, this seems like it can't be right since its the same command...

Commit to most recent commit: git commit --amend -m "Message"
Update most recent commit message: git commit --amend -m "New Message"

@JerryKacmar

This comment has been minimized.

Show comment Hide comment
@JerryKacmar

JerryKacmar Sep 11, 2017

Great list, thanks for this.

I would add:
Show history of changes for one file: gitk [filename]

Great list, thanks for this.

I would add:
Show history of changes for one file: gitk [filename]

@amoljore751986

This comment has been minimized.

Show comment Hide comment
@amoljore751986

amoljore751986 Sep 22, 2017

Thanks, Very helpful

Thanks, Very helpful

@MuraraAllan

This comment has been minimized.

Show comment Hide comment
@MuraraAllan

MuraraAllan Sep 22, 2017

Thank you!

Thank you!

@count48

This comment has been minimized.

Show comment Hide comment
@count48

count48 Oct 9, 2017

This will be there on my bookmarks forever , thanks for putting this together @hofmannsven

count48 commented Oct 9, 2017

This will be there on my bookmarks forever , thanks for putting this together @hofmannsven

@yh2n

This comment has been minimized.

Show comment Hide comment
@yh2n

yh2n Oct 17, 2017

Very helpful, thanks!

yh2n commented Oct 17, 2017

Very helpful, thanks!

@AwakenedOne

This comment has been minimized.

Show comment Hide comment
@AwakenedOne

AwakenedOne Nov 6, 2017

Thanks. A huge time saver and very helpful.

Thanks. A huge time saver and very helpful.

@rajops

This comment has been minimized.

Show comment Hide comment
@rajops

rajops Nov 9, 2017

Nice that you but together most of the Git commands, thanks 👍

rajops commented Nov 9, 2017

Nice that you but together most of the Git commands, thanks 👍

@sanyog96

This comment has been minimized.

Show comment Hide comment
@sanyog96

sanyog96 Nov 11, 2017

Thanks

Thanks

@JimmyCDChen

This comment has been minimized.

Show comment Hide comment
@JimmyCDChen

JimmyCDChen Nov 20, 2017

Thanks!

Thanks!

@Arkangel17

This comment has been minimized.

Show comment Hide comment
@Arkangel17

Arkangel17 Nov 30, 2017

awesome. thanks!

awesome. thanks!

@hoangthach252

This comment has been minimized.

Show comment Hide comment
@hoangthach252

hoangthach252 Dec 5, 2017

The best cheat sheets. Thanks !

The best cheat sheets. Thanks !

@huoxudong125

This comment has been minimized.

Show comment Hide comment
@huoxudong125

huoxudong125 Dec 20, 2017

Thanks

Thanks

@abdelaziz321

This comment has been minimized.

Show comment Hide comment
@abdelaziz321

abdelaziz321 Jan 30, 2018

Very helpful 👍

Very helpful 👍

@Valentina9091

This comment has been minimized.

Show comment Hide comment
@Valentina9091

Valentina9091 Feb 11, 2018

Thanks a lot, very helpful

Thanks a lot, very helpful

@pksjw

This comment has been minimized.

Show comment Hide comment
@pksjw

pksjw Feb 26, 2018

This is handy, thanks!

pksjw commented Feb 26, 2018

This is handy, thanks!

@cavrilionis

This comment has been minimized.

Show comment Hide comment
@cavrilionis

cavrilionis Mar 1, 2018

Thanks for this. Bookmarked.

Thanks for this. Bookmarked.

@Joshhortt

This comment has been minimized.

Show comment Hide comment
@Joshhortt

Joshhortt Mar 11, 2018

Awesome Cheatsheet, Thanks buddy!

Awesome Cheatsheet, Thanks buddy!

@tech0909

This comment has been minimized.

Show comment Hide comment
@tech0909

tech0909 Mar 12, 2018

Nice!

Nice!

@bestorw01

This comment has been minimized.

Show comment Hide comment
@bestorw01

bestorw01 Mar 12, 2018

Nice! one

Nice! one

@LambertStrategy

This comment has been minimized.

Show comment Hide comment
@LambertStrategy

LambertStrategy Apr 4, 2018

Useful! Thanks.

Useful! Thanks.

@jviaches

This comment has been minimized.

Show comment Hide comment
@jviaches

jviaches Apr 4, 2018

Useful list. Well done !

jviaches commented Apr 4, 2018

Useful list. Well done !

@albannurkollari

This comment has been minimized.

Show comment Hide comment
@albannurkollari

albannurkollari Apr 5, 2018

Huge thanks and appreciation. Helpful utmost!

Huge thanks and appreciation. Helpful utmost!

@tripit-qe

This comment has been minimized.

Show comment Hide comment
@tripit-qe

tripit-qe Apr 6, 2018

Thanks for all the info. I do have a question if this is the right forum to discuss or post it. Apologize in advance if it is.
Question: How to subtract multiple scattered commit from my local branch?
Story behind it: I have created a branch B out of another branch A which is in review pending. But I need my other branch A data to just refer while working on branch B. Now I am done with branch B and about to create a pull request. While reviewing I felt I need to omit branch A commit from my current branch B. Do I have to use cherry - pick command or how would I trash branch A data from my branch B?

Thanks for all the info. I do have a question if this is the right forum to discuss or post it. Apologize in advance if it is.
Question: How to subtract multiple scattered commit from my local branch?
Story behind it: I have created a branch B out of another branch A which is in review pending. But I need my other branch A data to just refer while working on branch B. Now I am done with branch B and about to create a pull request. While reviewing I felt I need to omit branch A commit from my current branch B. Do I have to use cherry - pick command or how would I trash branch A data from my branch B?

@trevorrd

This comment has been minimized.

Show comment Hide comment
@trevorrd

trevorrd Apr 12, 2018

Thank you! I will need to print this out, but this helped me a lot! Thanks for taking the time to document it and give examples! :) Cheers!

Thank you! I will need to print this out, but this helped me a lot! Thanks for taking the time to document it and give examples! :) Cheers!

@ConnerAiken

This comment has been minimized.

Show comment Hide comment
@ConnerAiken

ConnerAiken Apr 14, 2018

Yes, thank you!

Yes, thank you!

@christopher-paul-shaw

This comment has been minimized.

Show comment Hide comment
@christopher-paul-shaw

christopher-paul-shaw Apr 26, 2018

On security section, dont use git repositories for serving websites, rather keep the source in the repository but have a deployment system to prepare the code and copy just the website over to the server,

christopher-paul-shaw commented Apr 26, 2018

On security section, dont use git repositories for serving websites, rather keep the source in the repository but have a deployment system to prepare the code and copy just the website over to the server,

@noelinetb

This comment has been minimized.

Show comment Hide comment
@noelinetb

noelinetb May 3, 2018

Thanks for this!

Thanks for this!

@evermoreg

This comment has been minimized.

Show comment Hide comment
@evermoreg

evermoreg May 8, 2018

emmm it is really helpful. well explained!

emmm it is really helpful. well explained!

@davewilsondev

This comment has been minimized.

Show comment Hide comment
@davewilsondev

davewilsondev May 9, 2018

Thanks :)

Thanks :)

@ajernejcic

This comment has been minimized.

Show comment Hide comment
@ajernejcic

ajernejcic May 11, 2018

very helpful, thank you

very helpful, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment