How do I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?
How do I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?
Assuming the hash of the commit you want is c5f567:
git checkout c5f567 -- file1/to/restore file2/to/restore
The git checkout man page gives more information.
If you want to revert to the commit before c5f567, append ~1 (where 1 is the number of commits you want to go back, it can be anything):
git checkout c5f567~1 -- file1/to/restore file2/to/restore
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).
There is also a new git restore command that is specifically designed for restoring working copy files that have been modified. If your git is new enough you can use this command, but the documentation comes with a warning:
THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
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>
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 checkout -- foo
That will reset foo to HEAD. You can also:
git checkout HEAD^ foo
for one revision back, etc.
As of git v2.23.0 there's a new git restore method which is supposed to assume part of what git checkout was responsible for (even the accepted answer mentions that git checkout is quite confusing). See highlights of changes on github blog.
The default behaviour of this command is to restore the state of a working tree with the content coming from the source parameter (which in your case will be a commit hash).
So based on Greg Hewgill's answer (assuming the commit hash is c5f567) the command would look like this:
git restore --source=c5f567 file1/to/restore file2/to/restore
Or if you want to restore to the content of one commit before c5f567:
git restore --source=c5f567~1 file1/to/restore file2/to/restore
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.
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.
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.
You have to be careful when you say "rollback". If you used to have one version of a file in commit $A, and then later made two changes in two separate commits $B and $C (so what you are seeing is the third iteration of the file), and if you say "I want to roll back to the first one", do you really mean it?
If you want to get rid of the changes both the second and the third iteration, it is very simple:
$ git checkout $A file
and then you commit the result. The command asks "I want to check out the file from the state recorded by the commit $A".
On the other hand, what you meant is to get rid of the change the second iteration (i.e. commit $B) brought in, while keeping what commit $C did to the file, you would want to revert $B
$ git revert $B
Note that whoever created commit $B may not have been very disciplined and may have committed totally unrelated change in the same commit, and this revert may touch files other than file you see offending changes, so you may want to check the result carefully after doing so.
Amusingly, git checkout foo will not work if the working copy is in a directory named foo; however, both git checkout HEAD foo and git checkout ./foo will:
$ pwd
/Users/aaron/Documents/work/foo
$ git checkout foo
D foo
Already on "foo"
$ git checkout ./foo
$ git checkout HEAD foo
git checkout Last_Stable_commit_Number -- fileName
2.Git revert file to a specific branch
git checkout branchName_Which_Has_stable_Commit fileName
Here's how rebase works:
git checkout <my branch> git rebase master git checkout master git merge <my branch>
Assume you have
---o----o----o----o master \---A----B <my branch>
The first two commands ... commit git checkout git rebase master
... check out the branch of changes you want to apply to the master branch. The rebase command takes the commits from <my branch> (that are not found in master) and reapplies them to the head of master. In other words, the parent of the first commit in <my branch> is no longer a previous commit in the master history, but the current head of master. The two commands are the same as:
git rebase master <my branch>
It might be easier to remember this command as both the "base" and "modify" branches are explicit.
. The final history result is:
---o----o----o----o master \----A'----B' <my branch>
The final two commands ...
git checkout master
git merge <my branch>
... do a fast-forward merge to apply all <my branch> changes onto master. Without this step, the rebase commit does not get added to master. The final result is:
---o----o----o----o----A'----B' master, <my branch>
master and <my branch> both reference B'. Also, from this point it is safe to delete the <my branch> reference.
git branch -d <my branch>
I have to plug EasyGit here, which is a wrapper to make git more approachable to novices without confusing seasoned users. One of the things it does is give more meanings to git revert. In this case, you would simply say:
eg revert foo/bar foo/baz
Note, however, that git checkout ./foo and git checkout HEAD ./foo
are not exactly the same thing; case in point:
$ echo A > foo
$ git add foo
$ git commit -m 'A' foo
Created commit a1f085f: A
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 foo
$ echo B >> foo
$ git add foo
$ echo C >> foo
$ cat foo
A
B
C
$ git checkout ./foo
$ cat foo
A
B
$ git checkout HEAD ./foo
$ cat foo
A
(The second add stages the file in the index, but it does not get
committed.)
Git checkout ./foo means revert path ./foo from the index;
adding HEAD instructs Git to revert that path in the index to its
HEAD revision before doing so.
This is a very simple step. Checkout file to the commit id we want, here one commit id before, and then just git commit amend and we are done.
# git checkout <previous commit_id> <file_name>
# git commit --amend
This is very handy. If we want to bring any file to any prior commit id at the top of commit, we can easily do.
You can do it in 4 steps:
What you need to type in your terminal:
git revert <commit_hash>git reset HEAD~1git add <file_i_want_to_revert> && git commit -m 'reverting file'git checkout .good luck
git revert <hash>
Will revert a given commit. It sounds like you think git revert only affects the most recent commit.
That doesn't solve your problem, if you want to revert a change in a specific file and that commit changed more than that file.
if you commit a wrong file in your last commits follow the instruction :

git log --oneline // you see commits, find commit hash to which you want reset
git diff y0urhash src/main/.../../YourFile.java // to see difference
git reset y0urhash src/main/.../../YourFile.java // revert to y0urhash commit
git status // check files to commit
git commit -m "your commit message"
git push origin
If you're using Git Extensions and you only want to revert to the parent commit for the file, you can select the commit that contains the changes you want to revert, then select the 'Diff' tab in the details pane, right-click the file you want to revert, then 'Reset file(s) to' ...., then 'A' (the parent)
For files 'a.txt', 'b.txt', 'c.txt':
git revert -n <commit> (e.g. git revert -n HEAD to revert the last commit). This will prepare (but not commit!) and stage all the changes to undo a specific commit
git reset. This will unstage all the reverse changes so you can manually add the files you want changed back.
git add a.txt b.txt c.txt. This adds the files you want to undo changes to to your new commit.
git commit -m 'Undo <commit> for a.txt, b.txt, c.txt'. Commit the actual changes
git reset --hard. Get rid of anything you don't want undone.
git reset --soft HEAD^1
git status
Commit and push the file after making the changes.
Previous commit history for the wrong committed files will not be shown
Before pulling from origin always have pruning (Optional Step)
git remote prune origin