Git tips

Various tips for DVCS

Checkout file with  a specific revision number

$ git checkout ab3d60b53ac0b8e1563cf7df426f342866d5eb45 status.php
$ md5sum status.php
 b8b9f641fee7cadcc1794d698f7ea60c  status.php
$ git checkout ff6d3c14cb3d72fb9ea3a6ab54175f6466b3e1d9 status.php
$ md5sum status.php
 6ff102a672d0a34418537178da3ec0d0  status.php
$ git checkout ab3d60b53ac0b8e1563cf7df426f342866d5eb45 status.php
$ md5sum status.php
b8b9f641fee7cadcc1794d698f7ea60c  status.php

List changed files after commit

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

Shallow repository

It happens very often with SparkleShare:

 fatal: protocol error: expected old/new/ref, got 'shallow a5.......'

Solution with new git is quite simply:

git fetch --unshallow

Handling endlines

Git will automatically manage line endings for you if you set the core.autocrlf option. On Linux, you usually want to use input for this setting.

$ git config --global core.autocrlf input

Squashing commits with rebase

Let's assume that we have following last four commits which we would like to squash into one:

commit acfe2284d1fb6cd24f2c5e0f32e4f79a0338f47c
Author: bluszcz <bluszcz@bluszcz.net>
Date:   Mon Nov 3 00:10:43 2014 +0100

    Quiet cron

commit fe15c9a68de92b199147b241fead42021cd0179f
Author: bluszcz <bluszcz@bluszcz.net>
Date:   Sun Nov 2 11:02:09 2014 +0100

    Fix for upload

commit 432668e742d8b49f88e2d1ab1e33be6e5850eb38
Author: bluszcz <bluszcz@bluszcz.net>
Date:   Sat Nov 1 17:59:42 2014 +0100

    Gnu social

commit ca377a9bcbc68bbaec8a3110f57592382c7f32fc
Author: bluszcz <bluszcz@bluszcz.net>
Date:   Sat Nov 1 17:44:13 2014 +0100

    Gnu social

To perform this operation we need rebase command from git:

> git rebase -i HEAD~4

After performing this command git is opening an editor for us:

pick ca377a9 Gnu social
pick 432668e Gnu social
pick fe15c9a Fix for upload
pick acfe228 Quiet cron

# Rebase 453bc4d..acfe228 onto 453bc4d
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

I choose to rebase as following:

pick ca377a9 Gnu social
fixup 432668e Gnu social
squash fe15c9a Fix for upload
squash acfe228 Quiet cron

 Which opens me another editor to edit commit message:

# This is a combination of 4 commits.
# The first commit's message is:
Gnu social

# The 2nd commit message will be skipped:

#       Gnu social

# This is the 3rd commit message:

Fix for upload

# This is the 4th commit message:

Quiet cron

which I change into:

* Gnu social
* Fix for upload
* Quiet cron

which finally gives me:

[detached HEAD a6aaf13] * Gnu social * Fix for upload * Quiet cron
 Author: bluszcz <bluszcz@bluszcz.net>
 4 files changed, 11 insertions(+), 14 deletions(-)
Successfully rebased and updated refs/heads/testsquash.

$ git log

commit a6aaf13e299131829c7976180ce26fef7bc62ae4
Author: bluszcz <bluszcz@bluszcz.net>
Date:   Sat Nov 1 17:44:13 2014 +0100

    * Gnu social
    * Fix for upload
    * Quiet cron

More tips