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
  • It’s important to know which branch any edits, git adds, git commits or git pushes are going to affect!

...which files are affected by a commit?

git show --name-only <commit reference>
  • Lists the filenames affected by the commit
git show --name-status <commit reference>
  • Lists the filenames affected by the commit, and also shows whether they were Added, Modified, Deleted or Renamed (moved)

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

git show --name-only <commit reference>..
  • Lists the filenames affected since the commit
git show --name-status <commit reference>..
  • Lists the filenames affected since the commit, and also shows whether they were Added, Modified, Deleted or Renamed (moved)

...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>
  • There are many options for the git show command, use git help show to find out more
  • This command generates essentially the same output as git diff <commit reference 1> <commit reference 2> (see below) when reference 2 immediately precedes reference 1

...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>
  • This will show the differences in the specified file between the version in branch 1 and the version in branch 2.
  • You do not have to have either of these branches currently checked out.

...which files differ between two branches?

git diff <branch name 1>: <branch name 2>: --name-status
  • You do not have to have either of these branches currently checked out.

...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>
  • This command generates essentially the same output as git show <commit reference 1> (see above) if reference 1 immediately follows reference 2.

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

git diff --name-only --diff-filter=U
  • This will list the files which contain conflicts
git diff --name-status --diff-filter=U
  • This will list the files and almost certainly always show U for Updated
git diff --diff-filter=U
  • This will show the differences (ie: the areas of conflict) in the files

Go up
Return to main index.