3

I am using VS Code and want to go back to a specific commit A. I am currently at C:

A -> B -> C

Last commit I did was C, now I want to set all files back to their status at the time of commit A. How can I do that in VS Code? (using command line inside VS Code would be also fine if needed)-

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • Your question is not clear, as you've left out information about the branch. Do you want to change where the branch points as well, and throw away B and C? Then use `git reset --hard`. Or do you want to temporarily play around at A? Then use `git checkout`. – Jonathon Reinhart Jan 12 '20 at 18:30
  • @JonathonReinhart I want to keep B, C but work on A permanently. Actually I was lazy and instead of making a branch, after committing A I played around a lot for testing purposes. I want to keep my testing stuff in commits B and C, but continue at the state of A. – stefan.at.kotlin Jan 12 '20 at 18:35

1 Answers1

8

So you wish B and C had been on a secondary branch? Make a new branch right now (with head at C) to preserve B and C, but don’t check it out; stay on master or whatever this is. Now git reset --hard A.

Example

I'll make A, then B, then C, on master:

$ echo "this is A" >> test.txt
$ git init
Initialized empty Git repository in /Users/mattmobile/Desktop/f/.git/
$ git add .
$ git commit -a -m "this is A"
[master (root-commit) 14cfab0] this is A
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ echo "this is B" >> test.txt
$ git commit -a -m "this is B"
[master d55a780] this is B
 1 file changed, 1 insertion(+)
$ echo "this is C" >> test.txt
$ git commit -a -m "this is C"
[master 328183e] this is C
 1 file changed, 1 insertion(+)

Let's check it; yup, A then B then C:

$ git log
commit 328183e34767ed1bb06834e47d9bb3524e768546 (HEAD -> master)

    this is C

commit d55a780bcf1bda20ad7ceacce7c7c3777295fd59

    this is B

commit 14cfab017f3718bc7f3126e73aa187ec62b5d2a3

    this is A

Now I'm hit by regret. If only B and C had been on their own branch mybranch, and I could go back to A on master and keep working. OK:

$ git branch mybranch
$ git reset --hard 14cfab
HEAD is now at 14cfab0 this is A

So now mybranch is at C (and preserves C and B with parent A) but master is at A and I can keep working. Let's prove it. I'll add D:

$ git status
On branch master
nothing to commit, working tree clean
$ echo "this is D" >> test.txt
$ git commit -a -m "this is D"
[master bf8a4d4] this is D
 1 file changed, 1 insertion(+)

And what have we got?

$ git log
commit bf8a4d478ce61a78de94619ffea1dc58d1c9a799 (HEAD -> master)

    this is D

commit 14cfab017f3718bc7f3126e73aa187ec62b5d2a3

    this is A

So master consists of A, then D. Just what we wanted. Meanwhile, B and C are still alive as mybranch:

$ git checkout mybranch
Switched to branch 'mybranch'
$ git log
commit 328183e34767ed1bb06834e47d9bb3524e768546 (HEAD -> mybranch)

    this is C

commit d55a780bcf1bda20ad7ceacce7c7c3777295fd59

    this is B

commit 14cfab017f3718bc7f3126e73aa187ec62b5d2a3

    this is A
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This is why I recently added this answer https://stackoverflow.com/a/59675191/341994 to an already well-answered question. You are having Regret Type 3. :) By the way, sorry I know nothing of VS; just showing command line here. – matt Jan 12 '20 at 19:59
  • haha, very cool, thank you very much. Will check tomorrow and let you know how it worked out :-) – stefan.at.kotlin Jan 12 '20 at 20:01
  • There may be a VS way to do this but of course I've no idea what it is. – matt Jan 12 '20 at 20:01
  • Branch created, master resetted to A, thanks :-) of course accepted as correct answer. – stefan.at.kotlin Jan 13 '20 at 10:42
  • When I check out my new branch and do a git log, I don't see my commit C, only B. However, checking my repo in Azure Devops online, it also shows commit C in the branch. Any idea what's going on there? – stefan.at.kotlin Jan 13 '20 at 11:12