When I'm working on a branch and I want to completely remove all unstaged changes, I tend to use git stash
and then git stash drop
. Assuming I don't have any staged changes, is there a functional difference between that and git reset HEAD
?
To answer your question simply.
git stash
Deletes all the tracked files, which can be restored using git stash pop
for the recent stash only. If you believe you have done git stash multiple times you check using git stash list
For the question on git stash drop
it was well explained here Difference between git stash pop and git stash apply
If you want to stash untracked files too you can use git stash --all
On the other hand git reset HEAD
is a completely different operation. If you did not do git commit
at least in your local machine and done a git reset
all your staged files will be unstaged again. Means if you do a git add file.test
and did a git reset HEAD
it will unstage all the files. Pointing to the HEAD commit in you local machine. So think before working on this command.
It's not a dangerous command but you can see the difference using the git reflog
, which helps in understanding where the HEAD is pointing in your local and origin.
man git-stash
Save your local modifications to a new stash, and run git reset --hard to revert them.
There is no difference as it does exactly the same.
Well... first of all, git reset HEAD
won't actually remove your unstaged changes. git reset --hard HEAD
would (though it still won't remove untracked files). The important point is, reset
s primary focus is on updating the index - i.e. you would use it to unstage changes.
The most direct procedure to clear unstaged changes would be
git checkout -- .
git clean -f
The first command discards unstaged changes from tracked files, and the second removes untracked files (though you may want to do a git clean -n
first, to make sure you're not forgetting a file; because once you delete an untracked file, git
can't do anything to help you recover it).
I would say that using stash
is a hack - it doesn't reflect the intended semantics of the command - but it generally will work. You could even give the --keep-index
command to avoid accidentally discarding staged changes.
The practical difference is small. You're making git do extra work to store objects in the database, and then telling it to disregard those objects. If done often, this could clutter the database, but the added objects will eventually get cleared away by gc
, so in practice I wouldn't expect it to cause a real problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With