DEV Community

Cover image for My Most Used Git Commands
Steffen Pedersen
Steffen Pedersen

Posted on • Updated on

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. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And if everything goes like 💩, you can always reset the project.

Thank you for your time!

If you liked this, then please ❤️ and follow me on Twitter.

Top comments (12)

Collapse
 
circleofawesome profile image
Samiul Anwar


git checkout -- .

= "oops I fucked up, time to undo all my un-staged changes"

Collapse
 
steffenpedersen profile image
Steffen Pedersen

I will try that one out 😉

Collapse
 
brugnara profile image
Daniele

Hi Steffen.

Thanks for sharing this.

Here's my 50 cents: I use git commit -am "message" when I only have edits to commit.

Collapse
 
steffenpedersen profile image
Steffen Pedersen

This is great!

Collapse
 
anduser96 profile image
Andrei Gatej

Thanks for sharing!

git stash apply stash@{n}

can be replaced with
git stash apply n

Collapse
 
steffenpedersen profile image
Steffen Pedersen

Thank you 🤓

Collapse
 
javaguirre profile image
Javier Aguirre

From now on ‘—force-with-lease’ after rebase, thanks man!

Collapse
 
steffenpedersen profile image
Steffen Pedersen

Use it wisely 😉 It can still be dangerous.

Collapse
 
fernandomaia profile image
Fernando Maia

A great place to learn git fast and in a funny way is Oh shit, git!. It shows what git command should be used in some common situations that we have been/will go through.

Collapse
 
steffenpedersen profile image
Steffen Pedersen

I agree! It's been a while since I read it. Maybe I should try it out again 😃

Collapse
 
ramlev profile image
Hasse R. Hansen

git add --all = git add .

Collapse
 
steffenpedersen profile image
Steffen Pedersen

Du har fuldstændig ret 😃