Git cheat sheet
.. contents:: Table of Contents
:backlinks: none
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