git cheatsheet

Last edited: 30.11.2018

Show the first commit in a repo

$ git rev-list --max-parents=0 HEAD

Remove the first commit in a repo (i.e. import artifact from svn2git)

$ COMMIT=$(git rev-list --max-parents=0 HEAD)
$ git filter-branch --parent-filter 'sed "s;-p ${COMMIT};;"' HEAD

Unbreak 'git rebase' on OSX >= 10.7

see also: A Simple Tweak for Making 'git rebase' Safe on OS X

$ git config --global core.trustctime false

Get a brief overview of what did change in a specific commit (even merge commits)

$ git diff --raw 1001029065^ 1001029065
:100644 100644 d3827e7... cd5ac03... M  VERSION
:100644 100644 323fae0... 7601807... M  code.c
:000000 100644 0000000... e45c9c2... A  other.c
:000000 100644 0000000... d702569... A  release/release.c

Here is a way to remove a specific commit id knowing only the commit id you would like to remove.

$ git rebase --onto commit-id^ commit-id

Show repository change history (also useful for finding files)

$ git log --name-status --pretty=short

Show changed files in a certain revision

$ git show --name-only --pretty="format:" 90846d8ca7706fd17ea8b67021149b41ab48beab

Show file at a certain revision

$ git show 90846d8ca7706fd17ea8b67021149b41ab48beab:./API/ARAFuture.h

Recover file at a certain revision to different path

$ git show 90846d8ca7706fd17ea8b67021149b41ab48beab:./API/ARAFuture.h > /tmp/foo.h

Show history of changes for a particular file

$ git log -- API/ARAFuture.h

Show history of changes for a particular file (including diffs)

$ git log -p -- API/ARAFuture.h

Show history of changes made by a specific author on a branch after a specific date

$ git log --since 2012-04-25  --pretty="medium" --name-status Melodyne_Editor-2.0-Releases

Show history on a particular branch since specific date

$ git log branch_name --after 2012-02-23

Show hashes of all commits on a branch in chronological order

$ git log HEAD..Melodyne_Editor-2.0-Releases-XAR --reverse --pretty="format:%h"

Change a specific commit (either before you have pushed or if you can afford to push with force)

$ git rebase -i commit-id^
# select edit, apply stash/whatever, git add
# if you want to add something
$ git commit --amend
# if you want to redo the staging, however:
$ git reset --soft HEAD^
$ git stash # (BONUS! save stuff for later)
$ git commit -c ORIG_HEAD # get original author, timestamp and message + edit it further
$ git rebase --continue

Automatically create tags from Version file in repo

$ for h in `git log --format="%h" Version`; do git show $h:./Version > /tmp/; . /tmp/; echo "$h -> v$MAJOR_VERSION.$MINOR_VERSION.$SUBMINOR_VERSION"; git tag "v$MAJOR_VERSION.$MINOR_VERSION.$SUBMINOR_VERSION" $h; done

Repo/Bare repo

$ git init/clone --bare

Turn repo into mirror

$ git config remote.origin.fetch "+refs/*:refs/*"
$ git config remote.origin.mirror true