Git aliases that will make your life super easy.
I recently worked on a project in which I was supposed to name my branches in a quite long format and thus it was difficult to switch between branches. For this I was using clippy to store all my recent branches names so that I can use them frequently. But this was not very efficient solution to the problem.
If I am not using any clipboard manager tool, Every time when I commit something and want to push on origin, I will have to copy paste the current branch name before pushing, like so:
How about if I don’t have to copy and paste the branch name and git automatically pushes the latest commits on current branch to origin? Pretty interesting. Let’s see how this is going to look at the end of this story.
For this I have to tell my mac that hey, whenever I run git pto
grab my current branch name and run git push origin {name}
. Enough talking, let’s add git alias for this.
Step 1 — Open git config file
> open ~/.gitconfig
Step 2 — Copy paste these lines.
Now you will be able to use these shortcuts in your system. This snippet contains many aliases that I use everyday to speed up my development. Now let’s go through each of them:
1. git br
A simple shorthand for git branch. This will list all the branches available on your local system.
NOTE: You could add all available parameters (git branch) to this shorthand as well. For eg,
git br -m "team-btn"
to rename the current branch.
2. git st
This command displays the state of the working directory and the staging area with a nice short syntax which look pleasing to my eyes. Here is a comparison between git status
& git st
:
3. git lg
It is a shorthand for git log
with a nicer look. In case of 6 commits it is how both looks, One taking so much space and other one so nice, clean, simple & easy to understand.
Note: If you run
git log
you will have to pressq
to stop viewing the log history. If you usegit lg
it will showlatest 20
logs and you don’t have to press q to stop viewing the logs. You can tweak the command in.gitconfig
if you want to show more commits at once.
4. git ac
5. git oops
It happens with me many times that I have committed something but now I want to add one more line in the previous commit without touching the commit message. What I will do for this is, reset soft the last commit and then commit again. But now, it’s pretty simple.
Now moving forward, these commands are quite simple so I am not explaining these here.
unstash = stash pop
bd = branch -D
ch = checkout
chb = checkout -b
cht = checkout -t
6. git pto
This stands for push to origin
. It will automatically pick the name of current branch and update that branch on origin.
7. git pfo
This stands for pull from origin
. As the name suggests, it update the local branch with the branch on remote server.
8. git rh {n}
This stands for git reset hard.
This command takes a ‘number’ argument and reset hard the current branch by {n} number of commits. Shorthand for:
git rh 1 means=> git reset --hard HEAD~1
git rh 4 means=> git reset --hard HEAD~4
git rs {n} — same for git reset soft.
9. git rhh {hash}
This stands for git reset hard hash
. This will reset the branch upto the hash specified. Shorthand for:
git rhh 51ce361 means=> git reset --hard 51ce361
git rsH {hash} — same for git reset soft
That’s what I have got for now. If you liked this knowledge then please show some love by applauding loud 👏.
Let’s stay connected,
LinkedIn — https://www.linkedin.com/in/thearchitgarg/