Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git commandline cheat-sheet

git cheat-sheet

The git command-line utility has plenty of inconsistencies http://steveko.wordpress.com/2012/02/24/10-things-i-hate-about-git/

A GUI like http://sourcetreeapp.com is often helpful, but staying on the command line usually quicker. This is a list of the commands I use most frequently, listed by functional category:

Current state

git status list which (unstaged) files have changed
git diff list (unstaged) changes to files
git log list recent commits

Adding files to repo

git add fn stage file
git commit -m 'message' commit file
git commit -am 'message' add/commit all changes from all tracked files (no untracked files) in one go

Undoing previous actions

http://git-scm.com/book/en/Git-Tools-Rewriting-History
git reset filename unstage file
git commit --amend -m 'message' alter the last commit (add any staged files, new comment)
git reset --soft HEAD^ undo previous commit, put changes in staging
git reset --hard HEAD^ Undo last commit and all changes
git reset --hard HEAD^^ Undo two (^^) last commits and all changes
git checkout -- cats.html index.html Undo all changes that were made to files cats.html and index.html
git rebase --onto <commit-id>\^ <commit-id> HEAD remove specific commit from repository. the \ in ^ is just an escape char to make zsh play nice and is not necessary if using bash.

Remote repositories

git remote add origin git@example.com:example/petshop.git add a remote repository
git push -u origin master push current local repo to remote. -u sets it to default for the future
git remote -v show show the available remote repositories that have been added
git remote show origin show local<->remote branch tracking and sync status
git remote -v update bring remote refs up to date (and -v show which branches were updated)
git pull checkout and merge remote changes in one go
git fetch origin update the local cache of the remote repository
git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.
git show-branch *master will show you the commits in all of the branches whose names end in master (eg master and origin/master).
git log HEAD..origin/master --oneline shows commit messages
git diff HEAD..origin/master shows all changes on remote compared to local HEAD

Branches

git branch list currently existing branches
git branch [branchname] create new branch
git checkout branchname move to that branch
git checkout -b branchname create and checkout new branch in one go
git branch -d branchname remove branch

Merging branch back to master

git checkout master; git merge branchname; conditions for fast-forward merge - nothing new on master between branch start/end points

Branches on remote

git fetch origin``git branch -r list remote branches (after a fetch)
git push origin :branchname delete remote branch 'branchname'
git remote prune origin clean up deleted remote branches (let's say someone else deleted a branch on the remote)
git remote show origin show local<->remote branch tracking and sync status (duplicate info under "remote repositories")

Push local branch to differently named remote branch. Eg Heroku only deploys master

git push heroku yourbranch:master simple form git push heroku-staging staging:master (localBranchName:remoteBranchName)

Tagging

git tag list all tags
git checkout v0.0.1 checkout code
git tag -a v0.0.3 -m 'Version 0.0.3' add new tag
git push --tags push new tags to remote

Dealing with large files - keep them outside the repo on an ssh machine.

http://stackoverflow.com/questions/540535/managing-large-binary-files-with-git
http://git-annex.branchable.com/walkthrough/ #see ssh section

git annex add mybigfile
git commit -m 'add mybigfile'
git push myremote git annex copy --to myremote mybigfile this command copies the actual content to myremote
git annex drop mybigfile remove content from local repo
git annex get mybigfile retrieve the content
git annex copy --from myremote mybigfilespecify the remote from which to get the file

@rickp2006
Copy link

rickp2006 commented Dec 5, 2017

Thanks inphomercial, that was the one command I wanted to use but couldn't.

@DarwinniwraD
Copy link

DarwinniwraD commented Jan 3, 2018

👍

@ibrowne
Copy link

ibrowne commented Jan 11, 2018

As someone fairly new to using GitHub in the terminal, this is super helpful. But I'm wondering... what about all the Git stash commands? cf. https://git-scm.com/docs/git-stash

@manju-h
Copy link

manju-h commented Feb 13, 2018

Is there anyway i can find all the commited files using commit message

eg:
adding A.java B.java
git commit -m "somemessage to FIX bug "

adding B.java C.java
git commit -m "again doing commit to FIX some bug"


Is there anyway i can find (A ,B, C, D files) using key word "FIX" used in both commit messages

@minhluc77
Copy link

minhluc77 commented Feb 22, 2018

it is helpful. Thanks,

@sunilshah2711
Copy link

sunilshah2711 commented Mar 4, 2018

It's awesome. Thanks :)

@mhlangagc
Copy link

mhlangagc commented Apr 5, 2018

Needed and helpful. 💯

@arduino731
Copy link

arduino731 commented Jun 16, 2018

make my day! some of those I cant remember all of them but the list that you provide is helpful to remind every developer. 💘

@grebulator
Copy link

grebulator commented Jun 16, 2018

This is exactly what I was looking for! Thanks

@leetam
Copy link

leetam commented Jul 26, 2018

Perfect to share with people new to git!

@bhdang
Copy link

bhdang commented Aug 7, 2018

Awesome! thank you.

@taurus05
Copy link

taurus05 commented Aug 19, 2018

A very useful quick search manual for a beginner like me. Thank you!

@lingnet
Copy link

lingnet commented Sep 7, 2018

Thanks!

@crunchyluke
Copy link

crunchyluke commented Nov 9, 2018

Very helpful, thanks.

@Guangfei0
Copy link

Guangfei0 commented Nov 14, 2018

Very helpful, thanks!

@marjantehrani
Copy link

marjantehrani commented Feb 22, 2019

Thank you...this is very helpful 👍

@jasonballinger
Copy link

jasonballinger commented Mar 3, 2019

Thanks, this is super helpful and very straightforward.

@Vasuji
Copy link

Vasuji commented Sep 25, 2019

Thanks for such a wonderful collection!

@bkrupam
Copy link

bkrupam commented Nov 14, 2019

Couldn't be more thankful.Thanks

@Fileru
Copy link

Fileru commented Dec 13, 2019

how to pull source code from log?
ex: 1 commit "first commit"
2 commit "second commit"
I wanna change my code into first commit code.

@alamrani2020
Copy link

alamrani2020 commented Mar 15, 2020

Thanks a lot

@vishnuvashok
Copy link

vishnuvashok commented Nov 24, 2020

Thank you!

@olavomello
Copy link

olavomello commented Feb 2, 2021

Thank you!

@sreimoo
Copy link

sreimoo commented Jun 24, 2021

Thanks

@RihardsKK
Copy link

RihardsKK commented Sep 2, 2021

Thank you very much! This always helpfull! :)

@jackson-mil
Copy link

jackson-mil commented Jan 10, 2022

Thank you very much, this is great!

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