How to git clone only the latest revision

Most of the time when I clone a Github repository, I just need the latest revision. There is no point wasting my time cloning the entire repository with all the changes.

To do this:

$ git clone --depth=1 https://github.com/joe/foobar.git

Tried with: Git 1.9.1

Side-by-side color diff at terminal using CDiff

20150708_cdiff

If you use Mercurial, Git or Subversion at the shell, CDiff is a useful tool to have around. By default, hg diff or git diff is displayed in the classic diff format. This shows the before-and-after changed lines one below the other. Personally, I find this very confusing. I very much prefer the side-by-side diff view used in GUI diff programs. cdiff is a great way to get this side-by-side diff view at the shell in full color!

Install

CDiff is a Python program that can be installed from PyPI using this command:

$ sudo pip install cdiff

Note: Do not confuse this package with the colordiff package that is available in Ubuntu repositories. That is something else!

Usage

  • Invoke cdiff in any subdirectory of a Mercurial, Git or Subversion repository. By default, this shows a colorful diff of the changes in all uncommitted files.

  • To get side-by-side diff view use: cdiff -s

  • CDiff assumes that your window width is 80 by default. I prefer to view side-by-side diff that uses the full width of my terminal window. This can be achieved by invoking: cdiff -s -w 0

  • To diff two files, just pass them through diff -u and pipe that to cdiff:

$ diff -u file1.cpp file2.cpp | cdiff -s

Tried with: CDiff 0.9.7, Python 2.7.6 and Ubuntu 14.04

How to set user name and email in Git

To commit and perform such operations in Git, a user name and email need to be provided. These are global configuration values in Git.

  • Setting user name and email from the commandline is easy:
$ git config --global user.name "Average Joe"
$ git config --global user.email "joe@bloke.com"
  • These settings are written to the global gitconfig file of the user.

  • If you prefer using the TortoiseGit GUI to perform this operation, see this post.

Tried with: Git 1.8.0

How to generate patch of commit in Git

You have cloned an online Git repository, ran into a bug, found the cause, fixed it and committed it. Now, you want to submit the patch of the fix to the maintainer. The Git incantation to do this is:

git format-patch origin/master

This creates a patch file that the maintainer can pull into his Git repository.

Tried with: Git 1.7.4