I'm trying to find the equivalent of svn Revert
in Git. In svn
, when I rigth-click a file then click revert
, that will undo all the local edits. the file goes back to the last commit
. I can't find the exact same command in Git. Since I'm new, I don't want to mess-up my work.
Let say I made a commit Init. Then I made some changes. Now I'd like to go back to the Init state.
Thanks for helping
An equivalent of "svn update" would be "git pull --rebase".
Reverts any local changes to a file or directory and resolves any conflicted states. svn revert will not only revert the contents of an item in your working copy, but also any property changes.
Summary. The git revert command is a forward-moving undo operation that offers a safe method of undoing changes. Instead of deleting or orphaning commits in the commit history, a revert will create a new commit that inverses the changes specified. Git revert is a safer alternative to git reset in regards to losing work ...
Because of the staging area, it's not quite as simple as an svn revert
. Depending on what you've done, and what your goal is, you need to use either git checkout
or git reset
. You need to be careful with git reset
though, as you can re-write history and lose work.
Let's say you have the commit graph:
A <-- B <-- C <-- D
And you're currently at D
with some changes. To simply discard all changes (staged or not), you can use git reset --hard HEAD
or git reset --hard D
. Both will reset the state of your working copy.
If you want the state of your working copy to be identical to A
, but not lose commits B
and C
, then you should use: git checkout A -- .
from the top of your working copy. That'll set the state of your working copy to the contents of A
. At this point, your working tree is dirty, so you'll need to commit to record the fact that you wanted to go back. The commit graph at this point would be:
A <-- B <-- C <-- D <-- A'
Where A'
is a new commit (with a different id) that brings your work back to the equivalent of commit A
.
If you want to lose commit B
and C
, you can use git reset --hard A
. This will move your branch pointer back to commit A
. You are now re-writing history, so be careful. If you're working with other team members on this branch, you don't want to do this. If it's your own branch, you likely need to use git push -f
, where the -f
stands for --force
, when pushing it to your remote repo. You also need to make sure that you've set push.default
to either current
or upstream
to avoid pushing all matching branches and accidentally rewinding someone's work.
FWIW, I wrote up a blog post about reverting changes in Git several years ago. It's still relevant today. You may find it useful.
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