A few (somewhat) lesser known Git tips
This post describes a few lesser known features of Git. It’s starting out small but to like my OSX Command Line Essentials to will be added to as I encounter new things.
For the purposes of the description there are four main areas that I will refer to:
- The working area (i.e. the files on your system)
- The index (i.e. the files that have been ‘added’ or ‘staged’)
- The repository (i.e. the files that have been committed)
- The stash
diff subtleties
git diffwill compare the working area to the indexgit diff --cachedcompares the index to the repositorygit diff branch_one branch_twocompares two branches
Useful diff formatting options
A standard diff command in Git will show you the line as it was and the line as it is. That’s how Git works internally to identify changes but it’s not always most helpful. Sometimes we need to zoom in to easily identify the specific words or characters that have changed. At other times we might just need to know which files that have changed and not be concerned about how. There are useful options for both scenarios:
- the
--color-wordsand--word-diffoptions can be used to better see exactly what has changed within a line. - the
--statcommand will show only what files have changed.
rm with caution
While git add moves a file from the working area to the index, git rm needs to be treated with caution:
git rm -fwill remove the file from both the index and the working area. It is destructivegit rm --cachedremoves the file from the index but retains the changes in the working area
surgical logging
Here are some useful variants of the git log command:
git log --grep voltswill filter results to those that have ‘volts’ in the commit messagegit log -Gvolts [--patch]will filter results to those that added or removed the string ‘volts’ to any file