My Most Used 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. I would like to contribute to this by sharing my most used Git commands.
I honestly don’t have a specific way of typing these commands. Sometimes I write the full command, sometimes I replace git
with g
, and sometimes I use the full alias. I think it depends on the goal of 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
bashIf you have heard about Git, then you will probably know these three classic commands. I use them pretty much all the time. This is where aliases come in handy. gaa
, gcmsg “Add this commit”
, and gp
are very useful!
git checkout develop
git checkout -b my-new-branch
bashThese commands switch to an existing branch or create a new branch. There is not much new here. I often use the full aliases too - gcd
and gcb “my-new-branch“
.
git pull --rebase
bashI always use the --rebase
option when I pull from a repository. This keeps my commits nice and clean at the top of the tree. You could use the alias gup
. A senior developer taught me the power of rebase.
git rebase <branch>
bashThis brings us to rebase itself. Remember to rebase! This is especially important if you are working with feature branches. We do not want a half-dead branch that is a billion commits behind its default branch with merge conflicts up to the throat. Ouch!
git merge <branch>
bashIt 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), we use a pull request from GitHub — and the GUI on the website is just fine.
git stash
git stash pop
git stash apply stash@{1}
bashHas your project manager given you a new task that needs to be done quickly? Just throw your current work to the side and focus on the new task. It is awesome! I actually don’t use the full alias for this. I use g
instead of git
. Maybe it is because I want to be absolutely sure that I am actually stashing.
git status -s
git log
bashThese are the commands that keep 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
bashThis 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 will overwrite the remote repository with whatever you have locally. This can be dangerous if other contributors have pushed in the meantime. I mostly use --force-with-lease
after a rebase, as it works like a safety belt. This article has a great example.
git for-each-ref --sort=-committerdate refs/heads/
bashThis is actually a command that I found a few months ago from David Walsh. The command lists the most recently worked on branches from top to bottom. It is so cool!
git reset --hard
bashAnd if everything goes like đź’©, you can always reset the project.
Thank you for your time!