- Git Aliases I Use (Because I’m Lazy)
- Originally written by Victor Zhou
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: EmilyQiRabbit
- Proofreader: Imononoke, Weisiwu, Portandbridge
Git alias I use most often (because I’m too lazy)
I really get tired of typing git commands, even if they are very short.
Written on 16 September 2019, updated on 17 September 2019
I finally started using Git a lot a few years ago when I first started building some big personal projects. Now, typing commands like git status and Git push is easy for me, but if you’ve had some experience with Git, you know that some commands can be quite verbose.
For example, I often encounter commands like:
$ git commit --amend --no-edit
Copy the code
This command will merge your temporary changes into your most recent commit and will not modify the information for that commit (so Git won’t open a file editing interface). Its most common use is to modify a commit that has just been committed. Maybe I’m too careless, always finding a typo or forgetting to delete the mode information 😠 less than 30 seconds after committing a commit.
Enter git commit –amend –no-edit and the 28 characters quickly become boring. I’m currently obsessed with tuning projects (even before I should be 🤷), so one day I spent time thinking about how to tune my Git commands…
Git alias I configured
When you do a Google search for something like “Simplify Git commands,” you’ll quickly find information about git aliases. The fact is, the way to abbreviate commands is already built into Git! All you need to do is tell Git about the Git alias you want to configure. For example, you can shorten status to S by copying and pasting the following line of code into your console and executing it:
git config --global alias.s status
Copy the code
This command actually updates your.gitconfig file, which is used to save your global Git configuration:
~/.gitconfig
[alias]
s = status
Copy the code
Now, as soon as you enter the alias s, Git will automatically replace it with status!
Here are some of my most common Git aliases:
~/.gitconfig
[alias]
s = status
d = diff
co = checkout
br = branch
last = log -1 HEAD
cane = commit --amend --no-edit
lo = log --oneline -n 10
pr = pull --rebase
Copy the code
My.gitConfig file
Git alias
git config --global alias.s status
git config --global alias.d diff
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.last "log -1 HEAD"
git config --global alias.cane "commit --amend --no-edit"
git config --global alias.pr "pull --rebase"
git config --global alias.lo "log --oneline -n 10"
Copy the code
If you want to use git aliases, copy and paste these commands into the console.
Finally, here’s a short version of the bash command I often use:
~/.bash_profile
#... Other content
alias g=git
Copy the code
You can use any editor to add this content to your.bash_profile.
This is a Bash alias configuration that does exactly what you’d expect. If you use another shell, you can do this in its similar functionality (such as Zsh alias configuration).
Everything is ready. Now you can use Git like this:
$ g s
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Copy the code
$ g br
* master
Copy the code
$ g co -b new-branch
Switched to a new branch 'new-branch'
Copy the code
$g lo Author: Victor Zhou <[email protected]> Date: Mon Aug 26 01:16:49 2019-0700 Bump Version to 1.1.1Copy the code
Do they actually work…
Maybe it works? It really depends on the individual. If, like me, you need to do something obsessive-compulsive, such as typing “git status” repeatedly, it can save you some time:
— See Victor Zhou (@VictorcZhou) on Twitter September 15, 2019
Personally, I think it’s a small price to pay (about 30 seconds per new device configuration) and you get a great daily experience that’s faster and more efficient. Of course, how much time you actually save is debatable…
A rough calculation
Let’s do a rough calculation of how much time you can actually save by configuring git aliases. I can type about 135 words per minute, and let’s say each word has four letters, so that’s about 135 words per second
A letter.
The following table shows the number of letters saved by my most common shorthand:
The original order | Shorthand command | Number of letters that can be saved |
---|---|---|
git status |
g s |
7 |
git diff |
g d |
5 |
git checkout |
g co |
8 |
git branch |
g br |
6 |
git log -1 HEAD |
g last |
9 |
git commit --amend --no-edit |
g cane |
20 |
Next, I used the history command to look at my last 500 commands. Here’s the data:
The command | Use the number |
---|---|
g s |
155 |
g d |
47 |
g co |
19 |
g br |
26 |
g last |
11 |
g cane |
2 |
Other Git commands | 94 |
The Git command | 146 |
Each “other Git command” saves 2 letters (because I’m abbreviating Git to G), so the total saving is:
The command | Use the number | Number of letters that can be saved | Total number of letters saved |
---|---|---|---|
g s |
155 | 7 | 1085 |
g d |
47 | 5 | 235 |
g co |
19 | 8 | 152 |
g br |
26 | 6 | 156 |
g last |
11 | 9 | 99 |
g cane |
2 | 20 | 40 |
Other Git commands | 94 | 2 | 188 |
So a total of 1,955 letters were saved, on average per Git commandA letter. Let’s say I enter about 100 Git commands in an eight-hour workday550It’s one letter, which is equivalent toYou can save one minute a day(Use the 9 letters per second data INPUT I mentioned earlier).
Okay, so it doesn’t really save much time. 😢
But LET me reiterate: configuring aliases makes you feel more productive, which may give you some hints that you are actually more productive.
What do you think? Will you use Git aliases? Why or why not? What other aliases do you like to use? Feel free to comment in the comments section!
Update: There are some good discussions on lobste.rs’ blog and in the comments section below the original article. I recommend you read it.
conclusion
As I was writing this blog, I realized that there are three other Git commands that I’ve overlooked in common use:
$ git add .
$ git commit -m 'message'
$ git reset --hard
Copy the code
I’ll add them to my Git alias configuration as well!
git aliases
git config --global alias.a "add ."
git config --global alias.cm "commit -m"
git config --global alias.rh "reset --hard"
Copy the code
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.