1

Possible Duplicate:
git undo changes in some files

I am a solo developer (well, student actually) and find myself often wishing to roll back files to a prior state and undo changes that introduced bugs to the program. I wish to do this to individual files most of the time, but may wish to do this as well as to the entire folder at other times. This could theoretically extend to restoring files that I have deleted. Assume that I have the prior state of the files and folder saved with a prior commit. I am not trying to erase the prior commit, just to roll back and have a log of what I have done. I am the only person working on the files, this is not a project cloned from a hosting site.

I know how to do the extreme basics in Mercurial, and find that hg revert and hg update # do what I want to do in the mercurial world but have yet to understand how git does it. I may wish to ultimately host a project on Github and was also interested in learning about git since it is becoming the dominant player in the version control universe. I have no background in subversion and do not need references as to how things are different from subversion.

I may wish to learn how to fork my own project to a couple of versions and later merging them, but I will explore this later. My question at the moment applies to rolling back changes to the only copy of files and folders on my own hard drive.

Any suggestions as to a newbie friendly intro to git? Either online or book form is fine. Please do not guide me to any resources written by Scott Chacon; I find his style and books/Peepcode PDF extremely confusing. I also have the Pragmatic Guide to Git book by Swicegood, but have not found it super helpful. It should not be so difficult to figure out how to undo changes in git. I have spent hours so far searching.

Please note that I am a newbie with version control so please be gentle.

Thanks.

Community
  • 1
  • 1
haziz
  • 12,994
  • 16
  • 54
  • 75

4 Answers4

2

First do a git log to see your history so that you can decide which version you want to "roll back" to. Let's call it A. Now, you have a few choices.

  1. You can cut a branch from A and work over there. This is usually the right thing to do since it's often the case that you don't want to totally revert but just want to try something else. This can be done using git checkout -b retry A (creates and switches to a branch called retry starting from commit A). Now you can work there as if the rest of the changes since A are not there.

  2. Assuming you're on the master branch, you can do a git reset --hard A to discard all the commits since A and get back to that state. This is usually not a good idea since it's hard to track all changes since then (if you need them).

  3. A more brain dead version to simply replace the contents of a single file (say foo.c) with it's contents as of commit A is to say git cat-file -p A:foo.c > foo.c. Then you can commit this "new version" of the file. This is not a good idea really since git is not aware that you're rolling back. It just thinks of this as a new commit.

I'm not sure if all this is clear to you but if not, do ask and I'll try to clarify.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
1

If you want to revert a commit, use git revert hash_of_the_commit. This is tracked - the revert is a new commit in itself.

If you want to revert only part of a commit, one of the things you can do is use git diff:

$ git diff HEAD~3 file.txt
# will display a diff of file.txt between the current head and three
# revisions prior to that
$ git diff HEAD~3 file.txt | patch -R
$ git add file.txt
$ git commit -m "super commit message"
# will revert that file to what it was in HEAD~3
Mat
  • 202,337
  • 40
  • 393
  • 406
0

You said that don't like any resources from Scott Chacon but may be it will be helpful.

I found his article about "reset" command very useful and clear to understand. Only after it I have understand its usage. Link: http://progit.org/2011/07/11/reset.html

At least it can be useful for others that will read this question.

Alexey Morozov
  • 1,259
  • 10
  • 12
0

You need git checkout. It can checkout individual files or paths from specified commits:

git checkout commit_SHA1 -- your_file_name.here

It can be confusing because in most cases git checkout is used to switch branches.

You can use whatever method to identify commit here, I suggest you to do git log or use some gui and find your commit's SHA1 id.

max
  • 33,369
  • 7
  • 73
  • 84