Why Git Alias

devcamilla

Camilla Santiago

Posted on August 2, 2018

Why Git Alias

I've been using git aliases for a while now. People have seen me using them, asked me, I explained, encouraged them to use it too and failed. Why?

Perhaps my convincing powers is not that effective so I will try to appeal one more time.

Git Alias

Git aliases are shorthand for git commands. Just add it to your ~/.gitconfig.

[alias]
    st = status
    ch = checkout
    chb = checkout -b
Enter fullscreen mode Exit fullscreen mode

Now, instead of typing checkout -b, you can just type chb.

git checkout -b bug12345
git chb bug12345
Enter fullscreen mode Exit fullscreen mode

Cool, right? So, what else?

Why Git Alias?

It's cool!

Yes, I've said it again. But isn't it just cool, really? Being able to accomplish multiple commands in one go. While others execute git commands one by one, you take a shortcut and use aliases. Doing something out of the traditional. Gotta admit, it feels badass to use git aliases at times (especially when pair programming)

alt text

It saves time

I don't think I need to emphasize this more. You can stage, commit then push your changes in few clicks. Which will save you more or less 10 seconds, considering you commit roughly 10x a day, can save you at least 2 minutes a day. Which you can use to fix your coffee, go to comfort room, update your code reviewer or just stretch and have a quick nap.

alt text

My git aliases

Here are some aliases that I frequently use. All, I share here.

[alias]
    #current branch
        me = !git rev-parse --abbrev-ref HEAD
    #publish
        up = !git push origin -u $(git me)
    #unpublish
        down = !git push origin --delete $(git me)
    #stage all then commit with message
        acm = !git add . && echo 'Staged all changes, if any.' && git commit -m $message
Enter fullscreen mode Exit fullscreen mode

I recently discovered the fixup git alias here. It lets you amend your staged changes to a specific commit.

fixup = "!f() { TARGET=$(git rev-parse "$1"); git commit --fixup=$TARGET ${@:2} && EDITOR=true git rebase -i --autostash --autosquash $TARGET^; }; f"

Enter fullscreen mode Exit fullscreen mode

To use, just provide a few SHA-1 hash of the commit you want to amend to.

git fixup d670460b
Enter fullscreen mode Exit fullscreen mode

Then voila! Your recent staged changes should be fixed up to that commit.

There are lots of possibilities you can do with aliases. For one, when resolving rebase conflicts, I also use git alias to open TortoiseGit and execute diff on my source files. It's up to us to discover these possibilities.

Why Not Git Alias

X: So what's the git command for publishing branch again?
Me: git up
X: not a git command
Me: (Remembers it's an alias) Oh, wait. Let me check.

Okay, because you use aliases it's normal to forget the original commands. There's no escaping that. But don't worry I got your back. There's also a git alias for that.

# list aliases
    la = !git config -l | grep alias
Enter fullscreen mode Exit fullscreen mode

Use git la to list all your aliases and the corresponding commands. Great way to hack around using git aliases.

Conclusion

I hope I introduced Git Alias enough to make you try it. Just want to share the bliss of using it. Hope you also find it useful in your daily programming experience.

For others using it too, care too share your favorite aliases? What do you like the most about it?

For others who don't, why not?

💖 💪 🙅 🚩
devcamilla
Camilla Santiago

Posted on August 2, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Why Git Alias
git Why Git Alias

August 2, 2018