Git’s great, but typing out all those commands can get a bit verbose at times. Here are five aliases that will radically speed up your Git workflow:
1. Quick commit.
Sometimes you just want to make a quick commit. You know you’ve modified the right files, you know what needs to be added. This command quickly adds the files and commits them, with whatever message you specify.
c = "!git add -A && git commit -m "
Now you can run,
g c "Commit message goes here"
(Note that I also have a global alias for git in my bash profile)
2. Amend to the last commit, without editing the commit message.
A lot of developers like to keep their commit history clean and correct, without a lot of negligible commits lying around. At my old company, we used to have a “one commit per pull request” policy, back when we used Github for code reviews. So we would constantly be amending and squashing our local commits.
This command will automatically amend to the previous commit, without needing to edit the commit message.
amend = !git add -A && git commit --amend --no-edit
3. Push to the current remote branch.
In my workflow I typically want to push to whatever remote branch I’m working on, and I usually want to push to origin. Here’s a command that turns that whole action into a single “g p”:
p = !git push origin $(git rev-parse --abbrev-ref HEAD)
Here the rev-parse command gets the name of the current local branch. If no remote branch exists for the local branch, this command will create a new one.
This is starting to get efficient! We’ve now reduced the number of commands needed to make a remote change to:
g c "Some commit message"
4. Sync with the remote.
At my old company, we also had a “no merge commits” policy, so rebasing was the norm. Here’s a command that fetches from all the remotes, syncs the remote change from origin, and applies your commits on top of them.
f = !git fetch --all && git rebase origin/master
5. Start a new branch.
This one’s pretty simple, but it saves some time. Make a new branch and check it out with:
n = !git checkout -b
To add any of these aliases, just edit your ~/.gitconfig file and add each alias under [alias], like so:
Additionally, you can have repo-specific aliases. Just edit .git/config in the repo where you want to add the alias, and follow the same syntax. This can be useful if you want to override the “g p” alias to push to a different remote (for my Heroku apps, for example, I have my “g p” command push to the Heroku production remote).
Stay tuned for a follow up post on other git tricks, including how to optimize your ~/.gitconfig file for ultimate productivity!