You are almost there. You just need to put the alias in the right file. Because Git doesn’t automatically infer your command if you type it in partially, you can easily set up an alias for each command using git config
like so:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
And then you use it the aliases like: git ci
, git co
, git br
, git st
in any repo.
You can also run an external command through an alias. In that case, you start the command with a !
character. This is useful if you write your own tools that work with a Git repository:
git config --global alias.visual '!gitk'
You might have also noticed that the config
command takes in several parameters (like the --global
one). If we look at the docs man git config
:
For writing options: write to global ~/.gitconfig file rather than the repository .git/config, write to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn’t.
For reading options: read only from global ~/.gitconfig and from $XDG_CONFIG_HOME/git/config rather than from all available files.
See also the section called “FILES”.
There is also --system
, which writes to /etc/gitconfig
, --local
, for the local repo .git/gitconfig
, and --worktree
, which is similar to --local
.
But you can just directly edit the files themselves. It will look similar to this:
# in ~/.gitconfig
[alias]
lg = log --all --stat --pretty=oneline --graph --format='%h %d %an %cr %s' --oneline
l = log --all --stat --graph --format='%h %d %an %cr %s'
up = pull --rebase
br = branch --verbose -a
sfp = push --force-with-lease