DEV Community

camilla
camilla

Posted on

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?

Discussion (9)

Collapse
vlasales profile image
Vlastimil Pospichal • Edited on

My aliases in ~/.gitconfig

ci = commit -v
co = checkout
unstage = reset HEAD --
last = log -3
list = log -10 --oneline --decorate
newbranch = checkout -b
exclude = !editor .git/info/exclude
ignore = !editor .gitignore
master = checkout master
ref = symbolic-ref --short -q HEAD
mktags = !mktags
globalConfig = config --edit --global
aliases = config --get-regexp ^alias\\.

In other cases I don't use abbreviations, because using bash-complete is easier to me.

Collapse
jbazalar profile image
Jason Bazalar • Edited on

I like using s for status, o for pull origin, po for push origin and a few other short ones.

However, there was no point in git aliases for me if I didn't alias git as simply g in ~/.bashrc or .~/profile

g s
g bd (branch -d)
g b
g co
g c (commit -m)
g m (merge)

I try to read the letters in my head as the full command so I remember the commands elsewhere.

Collapse
vlasales profile image
Vlastimil Pospichal • Edited on

In my ~/.bashrc is included:

alias gita='git add'
alias gitb='git branch'
alias gitc='git checkout'
alias gitm='git merge'
alias gitr='git rebase'
alias gits='git status'
Thread Thread
devcamilla profile image
camilla Author

This is nice too. Surely there are lots of ways to do this. Thanks for sharing.

Collapse
devcamilla profile image
camilla Author

That's a nice touch. Didn't thought of that before. Thanks for sharing.

Collapse
devcamilla profile image
camilla Author • Edited on

Oh, it's the first I heard of bash complete. Thanks for sharing.

Collapse
pkristiancz profile image
Patrik Kristian

lol :) even try CTRL+R for searching in history :)
you guys have some interestig aliases, to be honest, for many of them i am not sure what they do.. time to study i guess :)

Collapse
jessekphillips profile image
Jesse Phillips

The aliases I see facilitate a flow I would recommend not to use.

I also do hybrid command line myself. When I go to commit I'll use git gui. Git status isn't as nice as gitk.

Even though the fixup alias intrigued me, I think keeping the commit separate and finalizing at the end of work with an interactive rebase can lend to more options for rework.

Collapse
nezteb profile image
Noah Betzen