git aliases
because git's enough of a git as git is
mood: conveniencedtipsoftwareone useful feature of git's you may not have known about is aliases: git configuration options which let you write shorter git commands.
here are the two i use the most:
[alias]
tree = log --oneline --all --graph
amend = commit --amend --no-edit
the syntax here should be fairly familiar to anyone who knows ini files.
you have an [alias]
section,
and inside that, you have your aliases, =
whatever they should expand to.
more specifically, what these commands do is:
-
tree
: display the repo's full commit history as a nice, compact, readable tree – invaluable when working on bigger repos with complex history, or just when trying to figure out which commit you need to cherry-pick. -
amend
: put any staged changes into the most recent commit. very useful if you realize before pushing that you forgot to format your code, for example, or when manually cleaning up history.
you can add them by manually editing your .gitconfig
,
including adding them per-repository, though i've never found a usecase for that.
i prefer setting them with the command:
git config set --global alias.name 'replacement command here'
take note: don't alias it to git something
!
if you want git foo
to run git status
,
you alias it to just 'status'
.
if you want to alias it to a full shell command,
put a !
in front, e.g.:
git config set --global alias.shell '!echo hello there'
but keep in mind:
Care should be taken if your shell alias is a "one-liner" script with multiple commands (e.g. in a pipeline), references multiple arguments, or is otherwise not able to handle positional arguments added at the end. For example:
alias.cmd = "!echo $1 | grep $2"
called asgit cmd 1 2
will be executed asecho $1 | grep $2 1 2
, which is not what you want.A convenient way to deal with this is to write your script operations in an inline function that is then called with any arguments from the command-line. For example
alias.cmd = "!c() { echo $1 | grep $2 ; }; c"
will correctly execute the prior example.
i would strongly recommend setting some aliases up to make your typical workflows a little easier. if you're running git at the cli, it can be one hell of a process, so optimize it!