It has been a year from 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 made me very frustrated is that for 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:
➜ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: added_file
new file: delete_file
new file: renamed_file
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: delete_file
deleted: renamed_file
Untracked files:
(use "git add <file>..." to include in what will be committed)
fourth_file
new_file
now_renamed_file
➜ git log
commit 895e76a4e71ecc4cc65de27e4429ffce165cccff (HEAD -> main)
Author: Marco Antonio Bet <example@example.com>
Date: Fri Jan 7 15:52:36 2022 -0300
third file
commit 86deb1bb03edabb56e4cb5d84289fe7ab3025607
Author: Marco Antonio Bet <example@example.com>
Date: Fri Jan 7 15:52:10 2022 -0300
second commit
commit d8fb730fe75482bdcc4cfd923ffdbdec95953a5a
Author: Marco Antonio Bet <example@example.com>
Date: Fri Jan 7 15:52:02 2022 -0300
first commit
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:
➜ git config --global alias.ss status -sb
But if you want to set the git alias with a text editor:
➜ git config --global --edit
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.
➜ git config --global alias.ss status -sb
So with this new alias set, how can we use it?
➜ git ss
Which will show a reduced git status
output.
➜ git_alias git:(main) ✗ git ss
## main
A added_file
AD delete_file
AD renamed_file
?? fourth_file
?? new_file
?? now_renamed_file
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:
➜ git config --global alias.ll !git log --pretty=format:'%C(blue)%h %C(cyan)%ae %C(green)%cr%C(red)%d %C(white)%s’
The output would be something like this:
➜ git ll
895e76a example@example.com 38 minutes ago (HEAD -> main) third file
86deb1b example@example.com 39 minutes ago second commit
d8fb730 example@example.com 39 minutes ago first commit
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
➜ git config --global alias.lg log --all --graph --decorate --oneline --abbrev-commit
The output would be something like this:
➜ git lg
* 895e76a (HEAD -> main) third file
* 86deb1b second commit
* d8fb730 first commit
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 ||.
staging = !git fetch && git checkout origin/staging
main = !git fetch && git checkout origin/main
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.
amend = !git add -A && git commit -a --amend --no-edit
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 = !git add -A && git commit -a --amend
save = !git add -A && git commit -m 'SAVEPOINT'
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 = reset HEAD~1 --mixed
Restore will simply uncommit the changes, but won’t throw away the changes.
wipe = !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard
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:
895e76a example@example.com 38 minutes ago (HEAD -> main) third file
86deb1b example@example.com 39 minutes ago second commit
d8fb730 example@example.com 39 minutes ago first commit
Then, if is not here, where can I find it? You can find typing git log — reflog.
➜ git log --reflog
commit 06dae1107b534d48d0002e40ccd0a1cf8ac45c6f
Author: Marco Antonio Bet <example@example.com>
Date: Wed Jan 12 15:58:57 2022 -0300
WIPE SAVEPOINT
commit 895e76a4e71ecc4cc65de27e4429ffce165cccff (HEAD -> main)
Author: Marco Antonio Bet <example@example.com>
Date: Fri Jan 7 15:52:36 2022 -0300
third file
commit 86deb1bb03edabb56e4cb5d84289fe7ab3025607
Author: Marco Antonio Bet <example@example.com>
Date: Fri Jan 7 15:52:10 2022 -0300
second commit
commit d8fb730fe75482bdcc4cfd923ffdbdec95953a5a
Author: Marco Antonio Bet <example@example.com>
Date: Fri Jan 7 15:52:02 2022 -0300
first commit
And to see the commit content:
➜ git checkout 06dae1107b534d48d0002e40ccd0a1cf8ac45c6f
And to add the commit content to your working branch:
➜ git cherry-pick 06dae1107b534d48d0002e40ccd0a1cf8ac45c6f
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 git alias and understand better what is, how to use it, and edit it.
Thank you so much and I am looking forward to see you soon. 😉
If you like this article you can give a ❤️, and 🔖 for later in the save button.
And if you like content about Git, Linux, Productivity tips, Typescript, and Python please follow me Marco Antonio Bet
All commands:
[alias]
ss = !git status -sb
ll = !git log --pretty=format:'%C(blue)%h %C(cyan)%ae %C(green)%cr%C(red)%d %C(white)%s'
lg = log --all --graph --decorate --oneline --abbrev-commit
ac = !git add -A && git commit -m
ck = checkout
ckb = checkout -b
staging = !git fetch && git checkout origin/staging
main = !git fetch && git checkout origin/main
amend = !git add -A && git commit -a --amend --no-edit
rename = !git add -A && git commit -a --amend
save = !git add -A && git commit -m 'SAVEPOINT'
restore = reset HEAD~1 --mixed
wipe = !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard
Github with the full .gitconfig: https://gist.github.com/itsbetma/a350dca5eb20e07e4b90d7372b64da45
To learn more:
Top comments (0)