My Most Used Git Commands

4 minGit
Git Commands

This is not a full guide to which commands you should learn. I just find it inspiring to read about other developers’ habits and work routines. This I would like to contribute to and tell about my most used Git commands.

I honestly don’t have a specific way on typing these commands. I sometimes write the full command. I sometimes replace git with g. And then I sometimes write the full alias. I think it depends on the goal with the command. It is worth mentioning that I am using ZSH with oh-my-zsh.

Main Commands

These are the commands that I use every day - or almost every day.

git add --all
git commit -m “Add this commit”
git push
bash

If you have heard about Git, then you will probably know these three classic commands. I use them pretty much all the time. It is here that it will be nice to use some aliases. gaa, gcmsg “Add this commit” and gp will come in handy!

git checkout develop
git checkout -b my-new-branch
bash

These will switch to an existing branch or a new branch. There is not much new going on here. I often use the full aliases here too - gcd and gcb “my-new-branch“.

git pull --rebase
bash

I always use the option --rebase when I pull from a repository. This will keep my commits nice and clean on the top of the tree. You could use the alias gup. It was a senior developer who taught me the power of rebase.

git rebase <branch>
bash

This will lead us to rebase itself. Remember to rebase! This is especially important if you are doing feature branches. We do not want a half-dead branch, which is a billion commits behind its default branch with merge conflicts up to the throat. Ouch!

git merge <branch>
bash

It is not that often that I need to merge directly from my terminal. In my team at work we use a branching strategy with feature branches. When a branch should be merged into the default branch (or another), then we use a pull request from GitHub — and the GUI at the website is just fine.

git stash
git stash pop
git stash apply stash@{1}
bash

Has your project manager given you a new task, which need to be done quickly? Just throw your current work to the side and focus on the new stuff. It is awesome! I actually don’t use the full alias for this. I am using g instead of git. Maybe it is because I want to be absolutely sure, that I am actually stashing 😀

git status -s
git log
bash

These are the commands that keeps me updated, and I use them about 500 times a day. I use gss, glg or sometimes glol.

Side Commands

These are the commands that I use occasionally.

git push --force-with-lease
bash

This is one of my strange darlings. But why don’t I just use --force? First of all, it is an extremely dangerous command and a huge no-no when using shared branches. It is because it will overwrite the remote repository with whatever you have locally. This can be dangerous if other contributors of the repository have pushed in the meantime. I have mostly used --force-with-lease after a rebase. This is because it works like a safety belt. This article has a great example.

git for-each-ref --sort=-committerdate refs/heads/
bash

This is actually a command, that I found a few months ago from David Walsh. The command will list the most recently worked on branches from top to bottom. It is so cool!

git reset --hard
bash

And if everything goes like đŸ’©, you can always reset the project.

Thank you for your time!