Not another git cheatsheet you say?? There are a ton of git cheatsheets already there are it is highly unlikely that i’ll ever refer to a cheat sheet when I can just google that sh** in 5 min and go through 5 Stack Overflow answers. Copy-paste random things until something works!
Why another Git Cheatsheet?
I beg to differ. I say this because most of the git cheat-sheets out there are very generic and have commands that I will rarely use. In my 4 years of dev career, which is pretty small as compared to some of the veterans out there, but still have managed to narrow down to the following git commands.
These are the commands that I use daily, almost. Some of these are the ones that come handy when I f*** up and f*** up bad. Some of these are just because I am too lazy to type.
Why a blog? Why a publicly available blog?
This is majorly a self-note blog post. A reference point for me. No matter where I am, and irrespective of the organisation I am working for, this blog post will the go-to point.
What’s in it for you?
Nothing special but everything you want. To be honest, bookmark it, or ignore it if you feel it’s too much. But the following are the commands that I have used and I am positive that I’ll be using it more!
Useful GIT resources
ReadMe and Markdown
Git Alias
Git Alias are nothing but git
shortcuts. For example:
cp = cherry-pick
st = status -s
cl = clone
ci = commit
co = checkout
br = branch
diff = diff --word-diff
dc = diff --cached
A progammer uses simple commands like fetch, pull, merge, rebase, push on a daily basis and like a 1000 times. There are commands that are simple, yet verbose to type. Git Alias helps to create shortcuts for these git scripts.
You can add alias one at a time:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
or you can add all at once. To setup git alias, we need to edit the .gitconfig
file and add the aliases there. The path of this file can vary depending on the operating system you are working on.
Generally,
- MacOS:
~/.gitconfig
- Linux:
~/.gitconfig
- Windows: It can be different based on system settings. A starting point
To quickly open, try running git config --global --edit
in terminal and it is highly possible that it will open in your default editor.
There are literally a ton of aliases that you can use and what works for me, might not work for you. Thus, will drop few links here where in you can select the aliases that works for you.
- Must Have Git Aliases: Advanced Examples by Nicola Paolucci
- Sample
.gitconfig
file: Check here or here
The most powerful feature of git alias is positional parameters. Positional parameters, in simple words are the values to your alias functions. Yes, functions.
my_alias = "!f() { 〈your complex command〉 }; f"
You can write alias functions that take arguments (just like normal functions) and run the script with them. The only thing you have to take care of is the order in which you pass parameters while invoking alias.
- $1 to mean the first parameter passed to the command.
- $2 to mean the second parameter passed to the command. (and so on for $3,
$4, etc.)
For example, if you want to delete branch locally and remotely but don’t want to remember the long git script
git branch -d localBranchName && git push origin --delete remoteBranchName
You can create a git alias function
delAll = "!f() { \
git branch -D $1 && git push origin --delete $1; \
}; f"
Now, all you have to do is pass the branch name you want to delete locally and on remote. You can fire this beauty by:
git delall mybranchName
Multiple Git SSH accounts
Special Mentions
Thanks for reading ❤
If this blog was able to bring value, please follow me here! Your support keeps me driven!
Originally published on adityatyagi.com
Want to connect? Follow me on Twitter and LinkedIn or reach out in the comments below!