How can I reset or revert a file to a specific revision?
- Hates_
- 2008-10-18 23:34
- 9
I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous version.
I have done a git log
along with a git diff
to find the revision I need, but just have no idea how to get the file back to its former state in the past.
9 Answers
You can quickly review the changes made to a file using the diff command:
git diff <commit hash> <filename>
Then to revert a specific file to that commit use the reset command:
git reset <commit hash> <filename>
You may need to use the --hard
option if you have local modifications.
A good workflow for managaging waypoints is to use tags to cleanly mark points in your timeline. I can't quite understand your last sentence but what you may want is diverge a branch from a previous point in time. To do this, use the handy checkout command:
git checkout <commit hash> git checkout -b <new branch name>
You can then rebase that against your mainline when you are ready to merge those changes:
git checkout <my branch> git rebase master git checkout master git merge <my branch>
git revert file to master, If you didn't commit it to the master branch yet, its easy: get off the master branch (like git checkout -b oops/fluke/dang) commit your changes there (like git add -u; git commit;) go back the master branch (like git checkout master) Your changes will be saved in branch oops/fluke/dang; master will be as it was. If you didn't commit it to the master branch yet, its easy: get off the master branch (like git checkout -b oops/fluke/dang) commit your changes there (like git add -u; git commit;) go back the master branch (like git checkout master) Your changes will be saved in branch oops/fluke/dang; master will be as it was.
Chris Lloyd
2008-12-17 06:59
You can use any reference to a git commit, including the SHA-1 if that's most convenient. The point is that the command looks like this:
git checkout [commit-ref] -- [filename]
git revert file to specific commit, You can do it in 4 steps: revert the entire commit with the file you want to specifically revert - it will create a new commit on your branch soft reset that commit - removes the commit and moves the changes to the working area handpick the files to revert and commit them drop all other files in You can do it in 4 steps: revert the entire commit with the file you want to specifically revert - it will create a new commit on your branch soft reset that commit - removes the commit and moves the changes to the working area handpick the files to revert and commit them drop all other files in
foxxtrot
2014-04-29 12:22
git checkout -- foo
That will reset foo
to HEAD. You can also:
git checkout HEAD^ foo
for one revision back, etc.
git revert commit, How to git revert a previous commit. As you can see from this git revert example, when you git revert a previous commit, the command only removes the changes associated with that previous commit. Prior changes and those made after that previous commit remain. When you git revert a previous commit, the only things plucked out of your development environment are the changes explicitly associated with the reverted commit. In this case, the command only removes the third commit we issued on the How to git revert a previous commit. As you can see from this git revert example, when you git revert a previous commit, the command only removes the changes associated with that previous commit. Prior changes and those made after that previous commit remain. When you git revert a previous commit, the only things plucked out of your development environment are the changes explicitly associated with the reverted commit. In this case, the command only removes the third commit we issued on the
Greg Hewgill
2013-03-18 07:47
And to revert to last committed version, which is most frequently needed, you can use this simpler command.
git checkout HEAD file/to/restore
git revert file to previous commit after push, You can revert individual commits with: git revert <commit_hash> This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits after that. If you want to revert a range of commits, you can do it like this: git revert <oldest_commit_hash>..<latest_commit_hash> You can revert individual commits with: git revert <commit_hash> This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits after that. If you want to revert a range of commits, you can do it like this: git revert <oldest_commit_hash>..<latest_commit_hash>
CDR
2012-01-14 06:15
I had the same issue just now and I found this answer easiest to understand (commit-ref
is the SHA value of the change in the log you want to go back to):
git checkout [commit-ref] [filename]
This will put that old version in your working directory and from there you can commit it if you want.
git revert to previous commit, git reset --hard [previous Commit SHA id here] git push origin [branch Name] -f It will remove your previous Git commit. If you want to keep your changes, you can also use: git reset --soft [previous Commit SHA id here] Then it will save your changes. git reset --hard [previous Commit SHA id here] git push origin [branch Name] -f It will remove your previous Git commit. If you want to keep your changes, you can also use: git reset --soft [previous Commit SHA id here] Then it will save your changes.
bbrown
2017-05-23 11:47
If you know how many commits you need to go back, you can use:
git checkout master~5 image.png
This assumes that you're on the master
branch, and the version you want is 5 commits back.
git remove file from commit, To remove files from commits, use the “git restore” command, specify the source using the “–source” option and the file to be removed from the repository. For example, in order to remove the file named “myfile” from the HEAD, you would write the following command. $ git restore --source=HEAD^ --staged -- <file>. To remove files from commits, use the “git restore” command, specify the source using the “–source” option and the file to be removed from the repository. For example, in order to remove the file named “myfile” from the HEAD, you would write the following command. $ git restore --source=HEAD^ --staged -- <file>.
Ron DeVera
2012-06-01 07:06
I think I've found it....from http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html
Sometimes you just want to go back and forget about every change past a certain point because they're all wrong.
Start with:
$ git log
which shows you a list of recent commits, and their SHA1 hashes.
Next, type:
$ git reset --hard SHA1_HASH
to restore the state to a given commit and erase all newer commits from the record permanently.
git reset a file from remote, git remote --verbose git checkout develop; git commit -m "Saving work."; git branch saved-work; git fetch upstream develop; git reset --hard upstream/develop; git clean -d --force On GitHub, you can also checkout the branch with the same name as the local one, in order to save the work there, although this isn't necessary if origin develop has the same changes as the local saved-work branch. git remote --verbose git checkout develop; git commit -m "Saving work."; git branch saved-work; git fetch upstream develop; git reset --hard upstream/develop; git clean -d --force On GitHub, you can also checkout the branch with the same name as the local one, in order to save the work there, although this isn't necessary if origin develop has the same changes as the local saved-work branch.
jdee
2011-11-20 00:55
This worked for me:
git checkout <commit hash> file
Then commit the change:
git commit -a
revert changes to a specific file git, you can see the list of your changes in that commit select it and then click on buttons right-hand side click reverse file then you can see it on file status tab at the bottom left-hand side then click unstage: you can see the list of your changes in that commit select it and then click on buttons right-hand side click reverse file then you can see it on file status tab at the bottom left-hand side then click unstage:
v2k
2011-08-25 22:12
Assuming the hash of the commit you want is
c5f567
:The git checkout man page gives more information.
If you want to revert to the commit before
c5f567
, append~1
(works with any number):As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).
How to Reset or Revert a File to a Specific Revision, You may need to use the --hard option if you have local modifications. for one revision back, etc. And to revert to last committed version, which is most frequently needed, you can use this simpler command. This will put that old version in your working directory and from there you can commit it if you want. There are cases when you need to revert or reset a file to a specific version. With the help of this tutorial you will easily manage it.
Greg Hewgill
2018-01-19 10:35