Skip to content

Instantly share code, notes, and snippets.

@Gabriel-p
Last active August 29, 2015 14:10
Show Gist options
  • Save Gabriel-p/3099c41c6a29b6e114b6 to your computer and use it in GitHub Desktop.
Save Gabriel-p/3099c41c6a29b6e114b6 to your computer and use it in GitHub Desktop.
Git cheat sheet

Branch management

Create a new branch

git checkout -b <new-branch>

Pull & track branch

(http://stackoverflow.com/a/1710427/1391441)

git branch -f <branch> origin/<branch>

Push all your branches to remote

(http://stackoverflow.com/a/21232996/1391441)

git push --all -u

Delete a branch

(http://stackoverflow.com/questions/2003505/delete-a-git-branch-both-locally-and-remotely)

  • Remotely
git push origin --delete <branch>
  • Locally
git branch -d <branch>
  • Force it
git branch -D <branch>

Compare branches

git difftool branch..master

or

git difftool master..branch

Files changed between branches

(http://stackoverflow.com/questions/822811/showing-which-files-have-changed-between-git-branches)

git diff --name-status master..<branch>

Better

git diff --stat --color master..<branch>

Compare same file in two different branches

(http://stackoverflow.com/q/27104753/1391441)

git difftool branch1 branch2 file.dat

Copy file from another branch

(http://stackoverflow.com/a/307872/1391441)

Positioned in the branch where I want the file to be copied:

git checkout otherbranch myfile.txt

Rebase/merge a branch into master

(http://kevinold.com/2013/04/17/my-git-workflow.html, http://stackoverflow.com/a/9147389/1391441)

git co branch
git rebase master
git co master
git merge branch

Rename a branch

(https://gist.github.com/lttlrck/9628955)

git branch -m old_branch new_branch         # Rename branch locally
git push origin :old_branch                 # Delete the old branch
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Tagging

Lightweight

git tag vx.x.x

Push all tags

git push --tags

Commits management

Discard unstaged changes

(http://stackoverflow.com/a/14075772/1391441)

git checkout .

Files/folders management

List all --assume-unchanged files

(http://stackoverflow.com/a/2363495/1391441)

git ls-files -v | grep '^[[:lower:]]'

Git aliases

(http://githowto.com/aliases)

git config --global alias.acp '!func(){ git add -A && git commit -am "$1" && git push origin master; }; func'
git config --global alias.fs '!func(){ git fetch && git status; }; func'
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment