Creating Git aliases
A few months ago a friend introduced me to Git aliases, and I’ve come to enjoy using them enough to set them up on every workspace I use.
Aliases are customizable shortcuts for full or partial commands, allowing the user to enter something like “git ci” instead of “git commit -m”.
They can cut down on the typing required for common commands, but can also be appreciated when you find that you no longer have to keep on searching for that twenty-character command you use every few days.
Git aliases are set using the following command:
If you want the alias to apply to all repositories on your user account, add the --global argument.
Particularly useful commands to alias
Branching
- “branch”: list local branches, follow with parameters for other commands
- “checkout -b BRANCH-NAME”: create a new branch and check it out
- “push --set-upstream origin BRANCH-NAME”: push local history to a new remote branch
Logging
- “log --follow -p -- FILE-PATH”: display history of a file across file moves and renames
- eg.
git log --follow -p -- **/*computer-vision*
- eg.
- “log --graph --name-status --oneline”: display a concise view of your commit history
- I have this aliased to “lg”: easier to remember.
Personal aliases
As an example, the following commands set global aliases that I have for my current user. You can set the names of aliases to whatever you like.
On Linux devices, global Git settings are stored in ~/.gitconfig. On Windows, this is C:/Users/USERNAME/.gitconfig.
Once you’ve set up an alias, you can start using it immediately, for example, by using “git cfg” to list all global Git config settings.
If you tend to be trigger-happy, you may want to avoid setting aliases for commands that are difficult to reverse. I prefer using branches over stashes, and stay away from aliases for commands like merge, rebase and reset to give a few extra seconds to think about whether that’s really what I want to do.
If you enjoy using Git aliases, you may like bash aliases even more!
Helpful resources:
- Git config documentation: https://git-scm.com/docs/git-config