Five life-changing git aliases

Devin Finzer
May 12, 2016 · 3 min read
Image for post
Image for post
Git aliases in action. Note that git is aliased as “g”.

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)

alias g=git

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"
g p

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!

The Lazy Developer

Helping developers be more lazy.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store