Git productivity with aliasing and multiple commands
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?
Git Aliases
The quickest way to do this is via git aliases. There are several gains we can make:
- Fewer keystrokes: like
git ci
instead ofgit commit
,git co
forgit checkout
etc. 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?
- e.g.
git cop
to combine checkout and pull operations - e.g.
git rbs
to combine all the steps needed to rebase from master into your current branch:git checkout master
,git pull
,git checkout -
(check out previous branch),git rebase master
,git push
.
- e.g.
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
C:\Program Files(x86)\Git\libexec\git-core\
- 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
Notes:
- 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.
References:
- Phil Haack’s GitHub Flow Like a Pro
- @vitalyb’s Git alias references
- Stack Overflow Git alias for multiple commands