It has been a year since the day I started working on my actual job, and it was the first work that I need to use git, and from this day on I never stopped using it, it is just so amazing.
But one thing that frustrated me is that in some cases git commands can be very long or not intuitive. Also, some commands would show a lot of information that I did not want to see.
For example, the git status and git log commands would show consecutively something like this:
So I wonder if there is a better way of showing these pieces of information.
Let’s start with the simpler one which is git status, but first, we need to understand how to set git alias.
Ways of setting git alias
There are two ways of setting git alias, one of them is by commands in the terminal and another is by editing the ~/.gitconfig file.
For example, setting git status improvements with terminal commands would be:
But if you want to set the git alias with a text editor:
This command will open the default editor for git.
You can also open the ~/.gitconfig file with vim ~/.gitconfig
, code ~/.gitconfig
, or any other text editor.
Now allow me to go back to where I was.
So with this new alias set, how can we use it?
Which will show a reduced git status
output.
You may be asking yourself what is the -sb
flag in the git status command is.
- s: Give the output in the short format.
- b: Show the current branch
Therefore with just a little bit of change in the git status command, you can have a better output.
With the git log improvement, we will go a little bit forward and I will show you two types of an alias that I currently have.
First type of git log alias
Alias:
The output would be something like this:
This is my favorite of the two because it shows the HASH, Email, Date, and commit message.
You would be asking yourself what is the --pretty=format:
flag in the git log command. Basically, this command demands an option that can be a placeholder, which is currently our option.
Every placeholder starts with a % sign. And the %C placeholder stands for color, as you can see inside the parenthesis there is always a color name.
List of placeholders used in this command:
- %C(): Change the color of the text.
- %h: Show the hash of the commit.
- %ae: Show the author’s email.
- %cr: Show the committer date.
- %d: Show to you which commit is the head of the local and origin.
- %s: Show the subject of the commit message.
Second type of git log alias
The output would be something like this:
This is not my favorite git log alias, but it is a very good example of an alias because before I would need to write the full command and now I just need to write git lg.
Explaining some of the git aliases that I have
Shell script in a git alias
When a git alias starts with a ! this means that the command next is Bash or Shell. With this, you can create functions and use operators like && and ||.
Where I work we use the staging and main as central branches, so every time I need to start working on something new I need to check out from one or another and this command helps me do this quickly.
This command deserves attention because it is one of the most used commands for me. Most of the time I commit some changes and think that everything is in place, but suddenly I discover that I forgot a console.log, commentaries, and so on.
Basically what he does is to add all changes to the last commit without changing the commit message.
Rename and save work together for me, the thing is that when you have changes they are not really saved and when you commit these changes they are now in a safer place. When working I use save to store information when I am going to leave for some time or stop working, but I always return and rename the last commit because you should never push a commit message ‘SAVEPOINT’.
If I have multiple saves I just do a git rebase -i HEAD~N
and squash the ‘SAVE POINTS’ commits.
Restore will simply uncommit the changes, but won’t throw away the changes.
Wipe I do not use a lot, but it is very useful. Wipe will add everything to a commit with a message of ‘WIPE SAVEPOINT’ and then reset to the commit before the wipe.
If you run git ll, you will not see the commit:
Then, if is not here, where can I find it? You can find typing git log — reflog
And to see the commit content:
And to add the commit content to your working branch:
As you can see git alias is very useful to short and to create new commands, the commands above are aliases that I personally use, but this should not prevent you from improving or creating your own git aliases.
I hope I could make you more productive with the git alias and understand better what it is, how to use it, and edit it.
Thank you so much and I am looking forward to seeing you soon. 😉
And if you like content about Git, Linux, Productivity tips, Typescript, and Python please follow me Marco Antonio Bet, and pay me a coffee.
All commands:
Github with the full .gitconfig: https://gist.github.com/itsbetma/a350dca5eb20e07e4b90d7372b64da45
To learn more: