DEV Community

Cover image for How And When To Use Git Reset

How And When To Use Git Reset

Charlotte on November 26, 2019

Most of us avoid dreaded git reset command — but actually it can be really useful, as long as you know how it works! What does ...
Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard • Edited

My best use case for git reset is when I want to update my pull request, merge master and clean up the git history

  • git fetch and git merge origin/master
  • Resolve conflicts and commit
  • git reset --soft origin/master same exact code in the repository but clean git history
  • commit and git push --force
Collapse
 
char_bone profile image
Charlotte

Interesting use case. So you're merging all of your pull requests commits into one commit. I can see that this makes the history cleaner. I personally like to keep that commit history in projects, especially for a big merge. It makes it easier to step back to a more specific point in the future after the merge. I can definitely see the benefits though :)

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

On GitHub you can do the same thing with merge and squash the commits.
The history in master is kept clean but you have a link to the pull request to see the details

Thread Thread
 
char_bone profile image
Charlotte

Yeah it definitely makes sense, especially if you do lots of tiny commits then you would clutter the main branch :) I guess there is a balancing act though as to leaving enough commits to be able to cherry pick certain functionality in future (rather than looking at a pull request and manually doing things)

Collapse
 
glihm profile image
glihm

Thanks for this article @Charlotte, I want to underline that for your soft example, git rebase is also a very good choice.

Collapse
 
char_bone profile image
Charlotte

Thank you :) rebasing definitely is an option but reset works differently. Reset is much simpler if you just want to go to an earlier commit and forget about the commits after it.

Collapse
 
mihaylov profile image
Petar Petrov

Any other use for soft? Because for that use case I use rebase.

Collapse
 
char_bone profile image
Charlotte

I guess resetting should be thought of more as when you want to totally undo your commits after a certain point and re-write them into a new commit message. With rebase you'd still have all the commits after that point there and you choose what you want to do with them (pick, squash, etc). So if you do not care about those commits, reset it the easiest option.

Collapse
 
mihaylov profile image
Petar Petrov

Ah yeah, makes sense so it is for situations where you want no witnesses of your crimes. :)

Collapse
 
gihrig profile image
Glen Ihrig

I've been using git for years, and I didn't know how git reset worked, maybe even forgot that it existed. I've cleaned up many a "git mess" though. It's always been, um "messy"!

I have had many instances where a knowledge of git reset would have been very useful.

Thanks for sharing!

Collapse
 
gerardino profile image
gerardino

And there's also good old plain git reset which only unstages the staged changes.

Collapse
 
char_bone profile image
Charlotte

The default is mixed :)

Collapse
 
ameyer66 profile image
Andrew Meyer

Good article, learned a bit more about GIT. Especially from the conversations.

Collapse
 
danjconn profile image
Dan Conn

Thanks for this! I have very rarely delved into using reset!