1. Fine-grained staging with git add -p

git add -p (or --patch) starts an interactive prompt that steps through all the current file changes, and allows you to stage different hunks of a file, rather than the entire file.

You'll see a diff of the hunk, and a prompt with various options that you can perform on it. Type the desired option and hit enter to perform the action.

A terminal showing the output of running `git add -p`
Each hunk is interated through, offering you various options to perform.
You can press ? to receive information regarding what each of the options do. The most common will be y to stage the hunk, n to skip over the hunk without staging, and q to stop. Pressing q will persist any hunks that you've already pressed y to.

This is a fantastically powerful command that allows much more fine grained control over staging than git add -A does.

You can also run git stash with the --patch flag, to perform that same functionality when stashing changes!

2. Stash new files with git stash -u

You might already be aware that you can stash your changes in git. This removes any "dirty" files in the git directory, so you have a clean working directory. You can then bring these changes back (even on another branch!) by using git stash apply or git stash pop.

However, this only moves dirty files—files that have been modified. Untracked files (files that have been newly created, and haven't been committed yet) won't be moved to the stash. Luckily, there is a flag, -u (or --include-untracked), that does move untracked files to stash.

This can be really useful when you need to stash all work on a feature that involved touching existing code and also creating new files.

3. Discarding unstaged file changes with git checkout -- file

git checkout is usually used to jump between branches in git. It can also be used to restore files that have been modified, but not staged (i.e., revert all changes to a file).

To restore a file using git checkout, use it like git checkout -- <filename>. All your changes to the file will be removed without a prompt, so be careful!

Most popular IDEs and git clients offer this in their GUI, but if you're already using git on the command line, I find it can be simpler to type this command directly into the command line, rather than context switching to a different app.

You can be even more fine-grained by combining this command with the -p flag from #1: git checkout -p -- <filename> will start an interactive propt to remove changes from a file.

4. Succinct status with git status --short

I find that the output from the git status command can be overly verbose. It contains information about current branch, as well as hints about which commands to run to stage files, which you should be plenty familiar with already.

To drastically cut down on the amount of information you get when you run git status, add a --short (or -s) flag to it. This will show only changes made to files, in a really concise, easy-to-read list.

Terminal output comparing the difference between `git status` and `git status --short`
Much shorter!

5. Create git aliases for common commands

Git has built-in management for creating aliases for commands. What this means is that you can specify a command to type after git, that will run a command of your choice. These commands can be as verbose as you want, so this comes in really handy for reducing the amount of typing needed for more complex git commands.

For example, I have a command set up called tree, that runs git log --graph --decorate --oneline. To run it, I just type git tree.

To add a command, simply run git config --global alias.<alias-here> "desired command here". You should omit the leading git from your desired command.

Here are a few that I have registered:

git config --global alias.co "checkout"
git config --global alias.nah "reset --hard"
git config --global alias.s "status -s"
git config --global alias.a "add"
git config --global alias.pop "stash pop"
git config --global alias.fa "fetch --all"
git config --global alias.tree "log --graph --decorate --oneline"
git config --global alias.today "log --since=8am --sparse --author=Tom --format=oneline --pretty --date=short"

I would highly recommend adding the co, s, a and pop commands to your own setup.

The today command is really useful to run as you're logging off, to give you a summary of commits you've made to a given project today.

If any of these tips were new to you, and you'd like to hear more, please consider subscribing to future posts by entering your email below.

The git website has a free book on git, containing everything you'd ever need to know and more. If you're craving more git how-tos, I'd recommend checking it out.