home
Git cheat sheet
Improve your Git skills
print

git version: 2.20 - Date: December 2018

Local Git State and changes

gitZone

Manage local changes

Show changes between commits, commit and working tree gitDiff

Show the working tree status git status

Add <file> contents to the index git add <file>

Remove <file> from the index git reset <file>

Add current contents of the index in a new commit git commit -m 'subject' -m 'body'

Who changed what in git blame <file>

Remote

List all remotes repositories git remote -v

Show information about remote git remote show <remote>

Add new remote repository git remote add <remote> <url>

Dowload all changes from git fetch <remote>

Download changes and integrate into HEAD git pull <remote> <branch>

Publish changes on a remote git push <remote> <branch>

Commit messages

  • Limit the subject line to 50 characters: <type>(<scope>): <summary>
    • <type>: chore, docs, feat, fix, refactor, style, or test.
    • <scope>: optional
    • <summary> present tense, imperative mood.
  • Do not end the subject with a period
  • Use the body to explain what, why and how

Tip: Separate subject from body with a blank line. git commit -m 'subject' -m 'body'

Commit rules

  • One commit for one task: if you have 2 tasks in one file you can use git add -p to chose which chunk to add
  • Try to commit as often as possible
  • Do not commit a half-done work.
  • Commit a stable version: tests should not failed

Tag

Tag a commit git tag <tag-name>


Branch

Create a new local branch and switch HEAD branch git checkout -b newBranch

List local branches git branch

Delete a local branch git branch -d <branch>

Merge

Merge <branch> on your current branch git merge <branch>

Reapply commits (of <branch>) on top of current branch git rebase <branch>

Undo

List all operations on repository git reflog

Discard all local changes in your working directory git reset --hard HEAD

Discard local changes in a file git checkout HEAD <file>

Revert a commit (add a new commit with contrary changes) git revert <commit>

Change the last commit git commit --amend Be careful: don't amend a published commit!

Remove from repository but not on disk git rm --cached <file>

Proxy

Add proxy on your global configuration git config --global http.proxy http://proxy.com:1234

Unset the global proxy git config --global --unset http.proxy

Add proxy on your current project directory git config http.proxy http://proxy.com:1234

Check your global proxy git config --global --get http.proxy

Check your local proxy git config --get http.proxy

default client

Visualize git tree gitk --all

Graphical git operations git gui

Settings

Add --global flag if you want to apply the setting to all off your local repositories git config user.name "your-user-name" git config user.email "your-email-addr"

Rebase by default when doing git pull git config pull. rebase true

Fix conflicts only once git config rerere.enabled 1

Store and remember credentials git config credential.helper store