Built-in shorthand
Beyond aliases, Git has a host of built-in shorthand notations that save typing once you know them. Most appear in the man pages but are easy to overlook.
HEAD relatives
HEAD # current commit
HEAD~ # one before
HEAD~1 # also one before
HEAD~5 # five before
HEAD^ # first parent of HEAD
HEAD^^ # parent of parent
HEAD^2 # second parent (merge)
HEAD^{tree} # the tree object of HEAD
~N walks up by N first-parent steps. ^N selects the Nth parent of HEAD. They are different.
Reflog references
HEAD@{1} # HEAD before its last move
HEAD@{yesterday} # HEAD as of yesterday
HEAD@{2.hours.ago} # HEAD as of two hours ago
main@{push} # remote-tracking equivalent
Branch shorthand
git checkout - # previous branch (like cd -)
git switch - # same, modern syntax
git merge @{u} # merge from upstream
git rebase @{u} # rebase onto upstream
git push HEAD # push current branch to its name remotely
SHA shorthand
You can use any unique prefix of a commit SHA:
git show abc # works if "abc" is unique
git checkout abc1234
Configure the default abbreviation length:
git config --global core.abbrev 12
Range shorthand
main..feature # commits in feature not main
main...feature # symmetric difference
feature^@ # all parents of feature
feature^! # feature itself, excluding parents
Pathspec shorthand
git add :/ # repository root, regardless of cwd
git checkout -- :/some-path
git diff -- '*.js' # all .js files
git diff -- ':!vendor/' # exclude vendor/
The -- separator
git checkout main # branch
git checkout -- main # path
git diff master file # ambiguous; could be branch or path
git diff master -- file # unambiguously a path
Use -- to disambiguate when a name could be either.
Useful one-character flags
git commit -a # add tracked files and commit
git commit -am "msg" # add tracked, commit with message
git pull -r # rebase instead of merge
git push -u # set upstream
git log -p # show diffs
git log -S 'string' # pickaxe search
git log -G 'regex' # regex pickaxe
Subcommand abbreviation
Git auto-completes unambiguous subcommands:
git st # status (with autocomplete or alias)
git co main # checkout (alias)
git rev # revert (no other "rev*" commands)
The CLI does not abbreviate by default - rely on shell completion or aliases.
Useful environment variables
GIT_EDITOR=vim git commit
GIT_AUTHOR_DATE='2024-01-01T12:00:00' git commit -m "backdated"
GIT_PAGER=cat git log # disable pager
GIT_TRACE=1 git fetch # debug output
Configuration shorthand
git config --global -e # open editor on global config
git config -l # list everything
git config --get-regexp '^alias\\.' # all aliases
The compounding effect
Each piece of shorthand saves only seconds, but you use them dozens of times a day. Spend an afternoon internalising them; for the rest of your career, you will move through Git noticeably faster than colleagues who have not.