DEV Community

Cover image for 11 Git Commands I Use Every Day
Domagoj Vidovic
Domagoj Vidovic

Posted on

11 Git Commands I Use Every Day

When I started my career, I was always afraid of losing my code changes. Sometimes, I would copy the code to text files just to be sure that I won't miss something.

That's not a great practice. If you know how to use git properly, you won't have these doubts.

Git has everything you need to make you safe. And much more.

Let's dive in.

1. Checking out a new branch

Obviously, I must use a new branch for every task I start:

git checkout -b <new_branch_name>
Enter fullscreen mode Exit fullscreen mode

This command creates a new branch and automatically sets it as active.

2. Selecting files for commit

This is one of the rare cases where I prefer GUI. In VS Code (or any other better IDE/text editor), you can easily see the updated files and select the ones you want to include in the commit.

But in case you want to do it with the CLI:

git add .
Enter fullscreen mode Exit fullscreen mode

This command will stage all changed files.

If you want to select a single file:

git add <path/to/file>
Enter fullscreen mode Exit fullscreen mode

3. Making a commit

After you stage some files, you need to commit them:

git commit -m "Some changes"
Enter fullscreen mode Exit fullscreen mode

In case you have some pre-commit rules turned on which doesn't allow you to make a commit (like linting), you can override them by passing the --no-verify flag:

git commit -m "Some changes" --no-verify
Enter fullscreen mode Exit fullscreen mode

4. Revert all changes

Sometimes, I experiment with the code. A bit later, I realize that it's not the right path and I need to undo all of my changes.

One simple command for that is:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

5. See the latest commits

I can easily see what's going on on my branch by typing:

git log
Enter fullscreen mode Exit fullscreen mode

I can see the commit hashes, messages, authors, and dates.

6. Pulling the changes from the remote branch

When I checkout an already existing branch (usually main or development), I need to fetch and merge the latest changes.

There is a shorthand for that:

git pull
Enter fullscreen mode Exit fullscreen mode

Sometimes, if you're in one of your newly created branches, you'll also need to specify the origin branch:

git pull origin/<branch_name>
Enter fullscreen mode Exit fullscreen mode

7. Undoing a local, unpushed commit

I made a commit. Damn! Something's wrong here. I need to make one more change.

No worries:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

This command will revert your last commit and keep the changes you made.

HEAD~1 means that your head is pointing on one commit earlier than your current - exactly what you want.

8. Undoing a pushed commit

I made some changes and pushed them to remote. Then, I realized it's not what I want.

For this, I use:

git revert <commit_hash>
Enter fullscreen mode Exit fullscreen mode

Be aware that this will be visible in your commit history.

9. Stashing my changes

I'm in the middle of the feature, and my teammate pings me for an urgent code review.

I obviously don't want to trash my changes, neither I want to commit them. I don't want to create a bunch of meaningless commits.

I only want to check his branch and return to my work.

To do so:

// stash your changes
git stash
// check out and review your teammate's branch
git checkout <your_teammates_branch>
... code reviewing
// check out your branch in progress
git checkout <your_branch>
// return the stashed changes
git stash pop
Enter fullscreen mode Exit fullscreen mode

pop seems familiar here? Yep, this works like a stack.

Meaning, if you do git stash twice in a row without git stash pop in between, they will stack onto each other.

10. Reseting your branch to remote version

I messed something up. Some broken commits, some broken npm installs.

Whatever I do, my branch is not working well anymore.

The remote version is working fine. Let's make it the same!

git fetch origin
git reset --hard origin/<branch_name>
Enter fullscreen mode Exit fullscreen mode

11. Picking commits from other branches

Sometimes, I want to apply the commits from the other branches. For this, I use:

git cherry-pick <commit_hash> 
Enter fullscreen mode Exit fullscreen mode

If I want to pick a range:

git cherry-pick <oldest_commit_hash>^..<newest_commit_hash>
Enter fullscreen mode Exit fullscreen mode


Let's be honest, I don't use all of these commands literally every day - but I use them really often.

I prefer the CLI because we'll never be able to cover all the options with a GUI.

Plus, you'll find most of the tutorials only using the CLI. If you're not familiar with it, you'll have a hard time understanding them.

I covered the basics here, but whatever you need to do, just Google it.

I'm certain that you'll find an answer easily.

Top comments (15)

pyguy9x profile image

Hi, I'm newbie and here is my git command

git status : to view exactly what I added
git add .
git commit -m 'Add comment here' .
git status : to view the changed at the end
if everything OK, then
git push : to push all my setting to the server

mustafa7amdi profile image

Don't forget "git pull" before "git push" 😉

husseinmsaqr profile image
Hussein Mohamed • Edited

Hi Mostafa,
I would tell you, you don't need to do that.
By git push origin/< branch-name > -f
You can push without need use pull before, I hope that helpful for you 👌

Thread Thread
mustafa7amdi profile image

Ya, but be careful this will remove others pushed code

leealfred profile image

These are really helpful and well explained. Thank-you!

domagojvidovic profile image
Domagoj Vidovic

Thanks, glad you like it!

darkojoska profile image

Very nice cheat sheet Domagoj, thank you for this! For a long time now, I wanted to make my own but you helped me with it :)

The one command that I would add to the list is:
git fetch --all

This is useful when someone just made a new branch onto origin but your vscode did not detect that and you don't see it when you want to change the branch (through ui).

PS. I find some git commands easier to use with ui that vscode provides :)

tanth1993 profile image

nice bro. on Version 2.2 and later, I use
git switch to switch branch and
git switch -c to create branch instead of using checkout.
I think git checkout is very useful and it can use for multiple purposes, but we can separate the purposes

maqamylee0 profile image
emmilly immaculate

Very useful Thank you

paulcanning profile image
Paul Canning

So basic commands that everyone uses 🙄

Here's a great one for newbies.

git add -p

This will let you pick what hunks of code to stage.

adriano_moreira profile image
Adriano Moreira

Recently I changed “git checkout” to “git switch” on at branch management

klukiyan profile image
Kiril Lukiyan

So glad vscode has all these built into the interface

frankgracy profile image
Grace Frank

I never knew i could use revert this is a much cleaner way to do things. I usually do a git rest in my local and tben run push - - force
Thanks for sharing