Reputation: 17614
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)-
Upvotes: 4
Views: 11971
Reputation: 534925
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
.
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
Upvotes: 9