1

Imagine the following commits:

commit 1
commit 2
commit 3

In which I have the files a,b and c. I add the file d, and in the next 3 commits I modify it ( I'm testing some JS on heroku ). I now have three more commits:

commit 4 ( this add the d file, the other 2 commits only modify it )
commit 5
commit 6

How can I return the working directory to the state of commit 3 while keeping the pushed commits 4 5 and 6? ( the file d should not exist at commit 3, or I'd like it at least to appear as untracked )

Geo
  • 93,257
  • 117
  • 344
  • 520

2 Answers2

1

git reset <commit3> where <commit3> is the SHA1 of commit 3 (or HEAD^^^) will reset the working directory to commit 3 while leaving d untracked.

(However, if you were going to then re-add it in a single commit, I suggest you do a git rebase -i HEAD^^^ instead; you can then squash the three latest commits together.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • But, will this also move the HEAD three commits back? I'd like the HEAD to remain where it is. – Geo May 02 '12 at 10:28
  • @Tempus: can you explain why you want to untrack `d`? The first alternative that I can think of is `git checkout HEAD^^^`, but that moves `HEAD` as well. – Fred Foo May 02 '12 at 10:31
  • I've been making a bunch of commits experimenting with a JS api. In the current case it's easy to just remove `d` and commit the modification, but I was thinking what if I'd have made modifications to 100 files, and I only knew the last stable commit? – Geo May 02 '12 at 10:35
  • Perhaps this is it? http://stackoverflow.com/questions/4991594/revert-a-range-of-commits-in-git – Geo May 02 '12 at 10:53
  • @Tempus: it really depends in what state you want to repo history to be afterwards. If you want to keep the full history including the last three commits, then reverting is your best option. – Fred Foo May 02 '12 at 11:32
0

Have a look at "Exploring History" here:

http://schacon.github.com/git/gittutorial.html

Also:

http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html

And use git revert or git reset.

If you just want to examine things from a previous commit, you can git checkout <commit>-<filename> but do read the man pages about all this ;)

CodeClown42
  • 11,194
  • 1
  • 32
  • 67