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 versio
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.
Obviously someone either needs to write an intelligible book on git, or git needs to be better explained in the documentation. Faced with this same problem I guessed that
cd <working copy>
git revert master
would undo the last commit which is seemed to do.
Ian
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.
For me none of the reply seemed really clear and therefore I would like to add mine which seems super easy.
I have a commit abc1
and after it I have done several (or one modification) to a file file.txt
.
Now say that I messed up something in the file file.txt
and I want to go back to a previous commit abc1
.
1.git checkout file.txt
: this will remove local changes, if you don't need them
2.git checkout abc1 file.txt
: this will bring your file to your wanted version
3.git commit -m "Restored file.txt to version abc1"
: this will commit your reversion.
git push
: this will push everything on the remote repository Between the step 2 and 3 of course you can do git status
to understand what is going on. Usually you should see the file.txt
already added and that is why there is no need of a git add
.
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.
In the case that you want to revert a file to a previous commit (and the file you want to revert already committed) you can use
git checkout HEAD^1 path/to/file
or
git checkout HEAD~1 path/to/file
Then just stage and commit the "new" version.
Armed with the knowledge that a commit can have two parents in the case of a merge, you should know that HEAD^1 is the first parent and HEAD~1 is the second parent.
Either will work if there is only one parent in the tree.