Jordi Pla

Git Recipes

Basic

Clone repository:

git clone [URL]

Add file to index:

git add [FILE]

Check index status:

# verbose
git status

# silent
git status -s

View changes:

# all files
git diff

# one file only
git diff [FILE]

# insert/delete statistics
git diff --numstat  

Commit changes:

git commit -m 'Commit message.'

Add all changed files to index and commit changes:

git commit -am 'Commit message.'

Push local commits to remote repository:

# use default remote & branch
git push

# specify remote & branch
git push origin master

Fetch commits from remote repository:

git fetch [REMOTE]

Merge received commits with local commits:

git merge

Fetch and merge remote commits with local commits:

# equivalent to git fetch [REMOTE]
git pull [REMOTE]
git merge

Remotes

Show remotes:

git remote -v

Add remote:

git remote add foo [URL]

Remove remote:

git remote rm [FILE]

Rename remote:

git remote rename [FROM] [TO]

Branches

List branches:

# current working branch is shown with a star (*)
git branch

Create branch:

git branch [BRANCH]

Work on branch (move HEAD):

git checkout [BRANCH]

Create branch and work on it:

git checkout -b [BRANCH]

Send branch to remote repository:

git push -u origin [BRANCH]

Receive branch from remote repository:

git checkout --track origin/[BRANCH]

Merge branches:

# move HEAD back to master
git checkout master

# add changes from branch to master
git merge [BRANCH]

Delete branch:

git branch -d [BRANCH]

Patches

Create a patch file:

git diff > [FILENAME].patch

Apply a patch:

git apply [FILENAME].patch

Create a formatted patch, i.e. a patch with 10 commits:

git format-patch -10 HEAD --stdout > [FILENAME].patch

Note: that format-patch will add commit metadata to the patch file. It's the recommended way to generate patches to be sent via email to maintainers.

To apply a patch of commits to a repository:

git am [FILENAME].patch

Tags

There are two types of tags: lightweight and annotated.

A lightweight tag is just a pointer to a commit. An annotated tag have commit-like metadata (date, author, description, etc), and are useful to identify releases.

Note: Local tags are not transfered using push. They can be sent using the --tags option.

List tags:

# all tags
git tag
# tags starting with tagged
git tag -l 'tagged*'

Create lightweight tag:

# last commit is used by default
git tag foo [commit]

Create annotated tag:

# last commit is used by default
git tag -a v1.4 -m "Version 1.1." [commit]

See tag (and associated commit) metadata:

git show v1.1

Send tag to remote repository:

git push [REMOTE] v1.1

Send all local tags to remote repository:

git push [REMOTE] --tags

Rename a tag:

git tag new old -m "Tag message."
git tag -d old
git push origin :old
git push --tags

Archives

Create archive of HEAD:

git archive -o [FILENAME].zip HEAD

Create archive of tag v1.1.0:

git archive --prefix=git-1.1.0/ -o git-1.1.0.tar.gz v1.1.0

Extract archive to another place:

git archive --prefix=project/ HEAD | tar xC /tmp