0

Say I have a git log that looks like this:

commit 8379fef8952c9fde4c4f8bb9b3fb5dab80a61bc9
...

commit cc6c393f707da82aa3f0753db81b148d7eddaaf1
...

commit 6668e9087e3561b7797c91730f4ac0057fa85904
...

commit 2da2d26974d1ef57a02f3dfe2c7b5aa310f2efac
...

commit a625c16a2985d06f10fb72db9e6952fdf51e5000
...

Say I want to revert back to the bottom one a625c16... and that all the work above it is terrible. I basically want to blow away everything I did in the last 4 commits and restart from the bottom commit. When I checkout that commit, I'm on no branch and a detached HEAD state. What does that mean? What needs to be done so I can continue working on this branch but from 4 commits ago?

Jwan622
  • 8,910
  • 11
  • 56
  • 125
  • Assuming you are working on branch `master` then rename it to e.g. `BAD_BRANCH`, then create a new `master` branch from your last good commit. If you decide you do need any of the changes in `BAD_BRANCH` then you can merge those to your new `master` branch later. – Paul R Jan 31 '17 at 17:12

2 Answers2

3

if you haven't pushed this, you can do

git reset --hard a625c16a2985d06f10fb72db9e6952fdf51e5000

This will discard all the changes completely and forget they every happened. However, this will mean that you will have to do git push --force if you've published this anywhere, which might be considered dangerous / antisocial.

If you want to avoid that you can do

git revert 8379fef8952c9fde4c4f8bb9b3fb5dab80a61bc9
...
git revert 2da2d26974d1ef57a02f3dfe2c7b5aa310f2efac

Which will cleanly backout all the changes, and you'll be able to push without --force. This does mean all your changes and reversions will be visible in the history.

Tom Tanner
  • 8,967
  • 2
  • 28
  • 58
0

If you already have the commit you want checked out as detached HEAD, you can simply do git branch -f branchname && git checkout branchname.

Vampire
  • 31,050
  • 2
  • 58
  • 88