Skip to content

Instantly share code, notes, and snippets.

@mgiaco
Forked from loonies/git-cheat-sheet.md
Created February 25, 2016 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgiaco/3b9a3596beb6c8133bfc to your computer and use it in GitHub Desktop.
Save mgiaco/3b9a3596beb6c8133bfc to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheat Sheet

Although there are enough resources on the web about Git, I will keep this one for my own reference. Minimal Git version required 1.7.2.

TOC


Legend

  • index: staging area (Imagine you are loading sand into the truck with bucket. Well, the bucket is like index and truck like a repository :)
  • <sha1>: sha1 hash of commit
  • <file>: path to the file (path/to/file.ext)
  • <branch>: branch name
  • <repository>: remote repository name

Info

Search the history for a change matching a pattern

git log -G <pattern> -- <path>
git log -S <string> -- <path>
git log -S <pattern> --pickaxe-regex -- <path>

Find commits where files were deleted

git log --diff-filter=D --summary

Checkout deleted file in the working tree

git checkout <sha1>^ <file>

Only show the content of a file from a specific revision

git show <sha1>^:<file>

Show diff between branches detecting renames

 git diff -M <branch>

Show file's history

git log -p <file>

Show changes on a branch that is not merged upstream

git cherry -v <upstream_branch> <new_branch>
git log <upstream_branch>..<new_branch>

Show log with changed files

git log --name-only
git log --name-status
git log --stat

Get latest tag in the current branch

git describe --exact-match --abbrev=0
git describe --abbrev=0 --tags

Find out if a change is part of a release

git name-rev --name-only <sha1>

Find out which branch contains a change

git branch --contains <sha1>

TOC

Adding

Add changes to the index chunk by chunk

git add --patch <file>
  • y: stage this chunk
  • n: do not stage this chunk
  • s: split this chunk into smaller chunks
  • e: edit this chunk

TOC

Branching

Create local branch

If not provided, Git uses HEAD as the new branch start point.

git checkout -b <branch>
git checkout -b <branch> <start>

or

git branch <branch>
git checkout <branch>

git branch <branch> <start>
git checkout <branch> <start>

Delete local branch

Delete already merged branch

git branch -d <branch>

Force branch deletion

git branch -D <branch>

TOC

Patching

Copy commit range from one branch to another

Pick from start <sha1> commit till end <sha1> commit.

git cherry-pick <sha1>..<sha1>

Creating and applying patches

By default Git will create a patch for every commit. Use --stdout > <patch>.patch for combined patch.

Create patches for the last N commits (each commit in it's own patch).

git format-patch HEAD~<N>

Create patches containing all commits from the current branch against another <branch> branch (each commit in it's own patch).

git format-patch <branch>

Creating combined patch.

git format-patch HEAD~<N> --stdout > <patch>.patch
git format-patch <branch> --stdout > <patch>.patch

Check what changes are in the patch

git apply --stat <patch>.patch

Test the patch before applying

git apply --check <patch>.patch

Apply patch

git am [--signoff] < <patch>.patch

TOC

Undoing

git reset contains great explanation and examples.

Split commit

--soft option will keep files in the index.

git reset [--soft] HEAD^

Undo a merge or pull

git reset --hard

Undo a merge or pull inside a dirty work tree

git reset --merge ORIG_HEAD

Revert a bad commit

git revert <sha1>

Checkout a deleted file into the work tree

git checkout <sha1>^ -- <file>

TOC

Remotes

Crete a new local branch by pulling a remote branch

git checkout -b <branch> <repository>/<branch>

Track a remote branch with an existing local

git branch --set-upstream <branch> <repository>/<branch>

Delete remote branch

git push <repository> :heads/<branch>
git push <repository> :<branch>
git push <repository> --delete <branch> 

Prune remote-tracking branches that are deleted from a remote repo

git remote prune <repository>

Change remote URL

git remote set-url <repository> https://example.com/repo.git

TOC

Subtree

  • --squash do not preserve history (squash history)

Add subtree as non-remote repository

Add subtree

git subtree add --prefix <directory> <url> <branch> [--squash]

Pull subtree

git subtree pull --prefix <directory> <url> <branch> [--squash]

Add subtree as remote repository

Add remote

 git remote add -f <name> <url>

Add subtree

git subtree add --prefix <directory> <remote> <branch> [--squash]

Pull subtree changes

git fetch <remote> <branch>
git subtree pull --prefix <directory> <remote> <branch> [--squash]

Push subtree changes

git subtree push --prefix=<directory> <remote> <branch>

TOC

Submodules

Update submodules

git submodule foreach 'git checkout master && git pull origin master'

Update submodule's URL

Edit the .gitmodules file, then run:

git submodule sync

Remove submodule

  • remove the submodule's entry in the .gitmodules file
  • remove the submodule's entry in the .git/config
  • run git rm –cached path/to/module - without a trailing slash!
  • remove the submodule from the filesystem, run rm -rf path/to/module/
  • commit changes

TOC

Additional resources

TOC

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