If you use Git for version control, then committing, branching, merging and rebasing are some of the most common tasks you perform. Multiple times a day, sometimes multiple times an hour. Why not speed them up a bit?
The quickest way to do this is via git aliases. There are several gains we can make:
- Fewer keystrokes: like
git ciinstead of
git checkoutetc. It all adds up.
- Combining repetitive commands: if you frequently perform the same sequence of steps, why not combine them all into a single alias?
git copto combine checkout and pull operations
git rbsto combine all the steps needed to rebase from master into your current branch:
git checkout master,
git checkout -(check out previous branch),
git rebase master,
Here’s a look at the aliases I use most frequently (each alias should be a row under the
[alias] section of your .gitconfig file):
ci = commit st = status co = checkout di = diff --color-words br = branch cob = checkout -b cm = !git add -A && git commit -m fc = !git fetch && git checkout save = !git add -A && git commit -m 'SAVEPOINT' wip = commit -am "WIP"
Creating custom git commands
Aliases are just one way. My colleague Jeremy Jordan prefers creating custom commands for git, like so:
- Navigate to
- Create a new file with no file extension
- Name this file git-[YOUR_COMMAND_NAME]. e.g.: git-customCommand
- Edit the file (see the attached example)
- The first line of the file should be how the file will be interpreted. Ex. ‘#!/bin/bash’ to execute the scrip in bash. You can also create scripts using Ruby or Python but this line would need to change.
- The reset of the script is your command so enter the commands that would accomplish the above scenario
Example Git Command File:
#!/bin/bash git checkout master git pull git checkout - git rebase master
- The alias defines and calls a bash function, that chains git commands using &&.
- The quotes (“) around the function definition & call are not necessary for *nix environments, only for Windows.
- Interestingly, commands such as “git rebase” are actually shell commands.