Usage of terminal is one of the best things a programmer does. It makes us feel like a little hacker playing around (at-least to me ;D ) with his cozy tool. We write little tools (when ever possible) to automate small tasks or cut the pain of typing a very long commands.
Git has the awesome feature of writing your own aliases to commands. I like it a lot and written a set of aliases that I use everyday (I call them short-codes) and I wanted to share them hoping to-be useful to others.
And it starts here ..
These are the list of aliases that I use everyday.
alias g!='git init'
alias g.='git add .'
alias g.-file='git add'
alias gb='git branch'
alias gb-new='git checkout -b'
alias gblame='git blame'
alias gcl='git clone'
alias gc='git commit -m'
alias gcout='git checkout'
alias gd='git diff'
alias gf='git fetch'
alias gl='git log'
alias gph='git push'
alias gph-f='git push -f'
alias gpl='git pull'
alias gr='git remote'
alias gr-list='git remote -v'
alias gr-add='git remote add'
alias greset='git reset --hard'
alias grevert-head='git revert HEAD'
alias grevert='git revert'
alias gs='git status'
alias gsh='git stash'
alias gsh-a='git stash apply'
alias gsh-c='git stash clear'
alias gsh-d='git stash drop'
alias gsh-l='git stash list'
alias gsh-p='git stash pop'
If you feel they are helpful, you can create your own aliases with the above list or I have created a git repo with detailed instructions to install and use them.
Make sure to reopen your terminal to see the changes.
Update: I'd recommend beginners to learn and understand actual commands before using any tools/aliases for git.
Top comments (33)
To be a little nitpicky, those are technically shell aliases to git, not actually git aliases.
However, the link you included shows how to use git aliases.
I'd actually recommend against making a ton of shell aliases as they don't possess the full power that doing real git aliases can provide.
You can also run into trouble if you try to combine shell scripts with git. If you use real git aliases, you can use the aliases, but if you use shell aliases, you cannot.
Instead, I just do this single shell alias:
alias g=git
Then include all the git aliases you want in ~/.gitconfig.
To execute, you just do:
Here's a large chunk of my ~/.gitconfig:
Thanks for the detailed explanation Valentin. I wanted to write a bash script for git aliases so that I could learn both bash and git. That was my requirement and I guess it is time to make the changes to the my little code snippet.
I'm stealing this
Dang. There is such a thing as too many Git aliases. Seems to me that after a certain point, unless you have them all memorized, you'd start running into diminishing returns.
I don't use all of them and by no means have them all memorized.
For the most part, they're just acronyms: 'dw' == 'diff words'.
I also kind of use my git alias file as place to keep notes for commands.
You should see my shell alias file :P I like to joke that I hate typing.
Thanks for the clarifying comment.
Please, please use
git push --force-with-lease
instead of-f
or--force
.Like all things git, the naming (a lease?) is a bit wonky. But basically, it will only do the forcing behavior when your local copy of the remote (e.g. GitHub) is up to date. It will refuse to overwrite commits that you haven't seen. This is much safer.
I like the gs and gd alias, as I would use those a lot.
One I copied from a colleague:
lg=log --graph --all --date=local --format=format:'%C(cyan)%h%C(reset) - %C(red)%d%C(reset) - %C(white)(%ai)%C(reset) %C(white dim)%s%C(reset) %C(white) -- %an%C(reset)' --abbrev-commit --color
One that I expanded the above to:
lge=log --graph --all --date=local --name-status --format=format:'%C(cyan)%h%C(reset) - %C(red)%d%C(reset) - %C(white)(%ai)%C(reset) %C(white dim)%s%C(reset) %C(white) -- %an%C(reset)' --abbrev-commit --color
Great, I remember following pretty graphs for git and saw similar on Stack Overflow but never got a chance to try it. Will check it now.
:-) Glad to suggest and contribute.
Sure, If you want, you can raise a PR in repo with your own aliases.
bash-it would solve some of the problems of using your own made-up aliases (git or bash) (like other devs being familiar with them).
Recently I removed my own old aliases I got used over the years and got familiar with bash-it pretty quickly (they have a logic and once I got it I repeat the full command in my head but only type the first letters like "Git Commit All Message is gcam".
Git is only one plugin of the bash-it framework, I recommend to check it out.
As convenient as shell aliases can be, familiarity with the actual git commands and arguments help us stay sharp. Additionally if you are helping someone else on their computer or yours, they won't understand your commands or learn anything from them--unless they are using the same aliases.
I worked for a customer that built their own custom configuration management solution to make it easier to use git. This had the effect of weakening developers' ability to cope with git directly. Anytime they confronted atypical use scenarios (e.g. resolving merge conflicts) they were fairly helpless to resolve it.
IMO, knowing git well is one of the most important skills any current developer should have. Unlike programming languages, we now have the great benefit of a single dominant tool for version control--thankfully an open source one. The stronger you are with git, the more productive you will be.
Yes, git is an important skill to any developer. I agree with the points you have mentioned, but one can always use the git commands if they feel aliases are not helping. These will work as just a convenience for anyone to speed up the VC tasks and I personally don't prefer to use GUI tools available for git (despite their benefits) so settled with the aliases.
"but IMO it is not related to productivity"
That's a lot of aliases! :) I stick to writing the full command, out of fear that I may eventually forget the actual, and just know the alias!
Nice post, Sarath.
Arun.
Thought I'd chime in - never looked into git aliases much, but I made a bash script containing all my helper commands a while back when I was learning git, and since moved them into bash aliases for ones I use frequently. At this point, they are just convenience since I know the base commands, so I would recommend learning the underlying commands first, but these are just lazy helper aliases.
Exactly, even for me they are just convenience, I'd never recommend them for beginners. Why did I forget to mention it in the post? Will do it now.
Bash was built for convenience... Almost every script / alias / program I write is simply to make my life easier.
I'm lazy, so I have aliases like
..
forcd ..
and...
forcd ../..
etc,bashrc
forsource ~/.bashrc
,ebashrc
forvim ~/.bashrc
... If I type it regularly enough, sooner or later it will be "functioned/aliased". Any time I have to use Windows or Mac I cry a little.Love git aliases, but as Valentin said they're shell aliases. Regardless, shell aliases and git aliases are very useful. Here's my list of git aliases if your interested.
One shell alias that find very helpful is for grabbing a PR from a repo hosted on GitHub (maybe it works on BitBucket too, not sure).
This is a fish shell alias, but I think you get the idea.
Thanks Nick, I have seen a couple of aliases that could be helpful to me form your gist, will add them soon
Any other Windows users who knows how to alias those
git
commands by chance?Found this that could be helpful to you to write your now git script aliases
gist.github.com/wzup/36b5159d8c580...
Oh nice!
Thanks Sarath.
I installed bash on Windows 10 recently and seems like a good way to try it out :)
I just wrote an article on git aliases. Teaching Git Some New Tricks
I won't rehash what others have said about bash aliases versus Git aliases, but I do second the point that you should learn to walk before you run and understand what a Git command does before making aliases.
Yes, that is why I don't recommend this for beginners !