Git Usage

Basic Usage

Clone

Download a repository from the server to your computer:

git clone git@git.example.org:[...]/${repository}.git

Pull

Update an existing repository with changes from the server to your computer:

git pull

Status

Check what has changed in the repository on your computer:

git status

Diff

Check how it has changed in the repository on your computer:

git diff

Commit

Save your changes as a commit:

git commit -a -m "My commit message."

Push

Push your commit(s) from your computer to the server:

git push

Log

Show the list of your commits:

git log

Advanced Usage

Log

Show with diff in between the commits:

git log -p

Show simple log with graph to visualize merges and such:

git log --oneline --decorate --graph --all

Show simple log with wich files where touched:

git log --name-only --oneline

Branches

List local branches:

git branch

List remote branches:

git branch -r

List all branches:

git branch -a

Create branch:

git branch mybranch

Switch branch:

git checkout mybranch

Remove branch:

git branch -D mybranch

Remove remote branch:

git push origin :mybranch

Setting default branch in bare repository:

git symbolic-ref HEAD refs/heads/mybranch

Checkouts

Checkout master branch from a server:

git clone git://myserver/myrepository.git
git clone http://myserver/myrepository.git
git clone ssh://myserver/myrepository.git
git clone user@myserver:myrepository.git

Checkout any branch other than master:

git clone git://myserver/myrepository.git
git checkout -b mybranch origin/mybranch

Revert all modifications upto last commit on master branch:

git checkout -f master

Remove all untracked files and directories on current branch:

git clean -dfx

Update local indices from origin server but don’t apply updates on local branches:

git fetch

Update local checkout from origin server and merge updates:

git pull

Update local checkout from origin server, merge updates, and rebase local changes on top of it (if any):

git pull --rebase

Commits

Add normal commit:

git commit -a -m "My commit."

Add singed-off commits:

git commit -a -S -m "My commit."

Add signed commit (e.g. for initial imports):

git commit -s -S -s -m "My commit."

Add commit of only specific files:

git commit -m "My commit." myfirstfile mysecondfile mythirdfile [...]

Files

Add a file or a directory (recursively):

git add myfile
git add mydirectory

Add all files and directories, recursively:

git add .

Add all files and directories, account for removals (Git 1.9 and newer), recursively:

git add . -A

Remove a file or a directory (recursively):

git rm myfile
git rm -r mydirectory

Rename a file or a directory:

git mv myfile mynewfile
git mv mydirectory mynewdirectory

Merges

Merge upto specific commit (ID) from other branch:

git merge ID

Merge upto specific commit (ID) from other branch and squash the commits:

git merge --squash ID
git commit -a -m "My squashed commit."

Redo specific commit (ID) from other branch that would conflict:

git cherry-pick ID

Remove all commits before a particular one, and make that one (ID or TAG) the initial:

echo ID > .git/info/grafts
git filter-branch

Pushes

Push to master branch to server:

git push
git push origin master

Push branch other than master to server:

git push origin mybranch:refs/heads/mybranch

Push all branches to server:

git push --all

Push tags to server:

git push --tags

Remotes

List remote locations:

ls .git/refs/remotes/*

Show remote location:

git remote show mylocation

Add new remote location:

git remote add mylocation git://mylocation/myrepository.git

Remote existing remote location:

git remote rm mylocation

Get updates from remote location:

git remote update mylocation

Tags

Add signed tag (e.g. for initial tag):

git tag -s -m "My tag." mytag

Add normal tag of HEAD:

git tag -a -m "My tag." mytag

Add normal tag to HEAD, but use branch convention:

git tag -a -m "My tag." mybranch/mytag

Add normal tag of a specific commit (ID) older than HEAD:

git tag -a -m "My tag." mytag ID

List tags:

git tag

Rename tag:

git tag -m mytag mynewtag

Remove tag:

git tag -d mytag

Remove remote tag:

git push origin :refs/tags/mytag

Expert Usage

Branches

Removing a file from history:

git filter-branch --tree-filter 'rm -f mydirectory/myfile' HEAD
git reflog expire --expire=0 --all
git prune
git repack -adf

Creating an empty branch without history (plumbing):

git symbolic-ref HEAD refs/heads/mybranch
git rm --cached -r . && rm -rf *

*Commit new content*

Creating an empty branch without history (porcelain):

mkdir ../mybranch
cd ../mybranch
git init

*Commit new content*

cd -
git fetch ../mybranch master:mybranch