-1

Say I have a commit history that looks like this:

A <- B <- C

I am currently on C. I hate both my commits B and C and I want to get back to A with a clean staging and no changes.. I want the state of A. How do I do this in the most efficient way. I am currently doing this:

git revert --no-commit B
git revert --no-commit A
git checkout .

This also makes it so there is no revert commit/no new commit showing my revert. Say I want this. What do I do?

Is there a better way?

Jwan622
  • 11,015
  • 21
  • 88
  • 181

2 Answers2

1
git reset --hard A

That will take you back to revision A and it's very close to saying "b and C didn't happen". Also, the current branch will start pointing to A so, use with care.

If what you want is a new revision D that follows C (A <- B <- C <- D) so that it has the same content as A, then you could try this:

git checkout A -- . # take back content of the whole project to how A was
git commit -m "Going back to revision A"

If you want to do it with reverts that don't create a commit:

git revert --no-commit A..C
git commit -m "going back to A"

Or:

git revert --no-edit A..C
git reset --soft HEAD~2 # Moving branch pointer to C keeping everything else as-is
git commit -m "going back to A"
eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

to go back to commit A, follow below command:

git reset --hard A

and if you still have some untracked files on your local, you can run below command:

git clean -f -d

But if your branch is also used by other people, then the best solution would be to use revert command only to avoid conflicts for everyone.

Priyanka Sharma
  • 167
  • 3
  • 7