Table of Contents

Doing things with git

Git has become probably the most popular source-control system today, and if you're new to it, or don't use it very often, there are some things you might want to do which seem simple, but you just can't remember or work out the required command.

I think git is an unpleasantly inconsistent collection of tools and commands which disappointingly failed to learn from the disparate and sometimes conflicting options to other command-line utilities (for example, does -n mean "add line numbers to the output" [grep, cat] or does it mean "just do a dummy run; change nothing" [rsync, rename], or does it mean "display IP addresses and port numbers instead of host names and protocol names" [netstat, iptables]?). Linus had the opportunity to create a new family of tools which behaved in a way people could understand and remember, but he probably just never used DEC VMS and therefore didn't realise there was a better way of doing things than how Unix does it.

So, guides like this are necessary. This is a summary of things you are likely to want to do with git, and how to do them, for the times when you can't remember, have never needed to do them before, or are simply baffled by the inconsistency of how git works.

How to find out...

...which branch I’m currently looking at / working in?

git status

...which files are affected by a commit?

git show --name-only <commit reference>
git show --name-status <commit reference>

...which files have been affected since a commit?

git show --name-only <commit reference>..
git show --name-status <commit reference>..

...which commits have affected a file?

git log --follow <path to file>

...what commits have taken place since a specific commit?

git log <commit reference>..

...what commits are in a different branch from the one I’m working in?

git log <other branch name>

...what actual changes are in a commit?

git show <commit reference>

...what the differences in a file between two commits are?

git diff <commit reference 1> <commit reference 2> <path to file>

or

git diff <commit reference 1>..<commit reference 2> <path to file>

...what the differences in a file between two branches are?

git diff <branch name 1>: <branch name 2>: <path to file>

...which files differ between two branches?

git diff <branch name 1>: <branch name 2>: --name-status

...what does a file look like in another branch or commit?

git show <branch name>:<path to file>
git show <commit reference>:<path to file>

...all differences between two branches?

git diff <branch name 1>: <branch name 2>:

...all differences between two commits?

git diff <commit reference 1> <commit reference 2>

...which files contain conflicts after doing a merge?

git diff --name-only --diff-filter=U
git diff --name-status --diff-filter=U
git diff --diff-filter=U

Go up
Return to main index.