1

I have the following Git log. I wish to go back to 2c2996639ec10d659a771c4398759deb323eb08c which is one before the branch master. I wish to keep a history, however, of the last commit in the repository. Lastly, I don't wish to create a fork, but just want one branch.

So I suppose I could do something like:

git checkout 2c2996639ec10d659a771c4398759deb323eb08c

But if I do so, what will happen when I later push it to the repository?

[Michael@devserver testing]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 43 commits.
#
nothing to commit (working directory clean)
[Michael@devserver testing]$ git log
commit 0fb5d27e1bc57b120e0f361f8f71a947bb264448
Author: Michael Smith <example@gmail.com>
Date:   Thu Feb 4 05:20:31 2016 -0800

    changed to using account to links

commit 2c2996639ec10d659a771c4398759deb323eb08c
Author: Michael Smith <example@gmail.com>
Date:   Mon Feb 1 07:48:20 2016 -0800

    Finished implementation of new projects

......
......

commit 0ad8e4a14f01ccb964b945afaafe98f14f478f98
Author: Michael Smith <example@gmail.com>
Date:   Wed Apr 23 06:21:04 2014 -0700

    Changed link locations to reflect changing /lib/plugins to /lib/plugins_3rd and /lib/plugins_my to /lib/plugins

commit dde33715730c58f100c180d6871251baef0d25ea
Author: Michael Smith <example@gmail.com>
Date:   Mon Apr 21 00:55:52 2014 -0700

    Initial commit
[Michael@devserver testing]$ git log
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • @raina77ow I was thinking maybe `git revert HEAD~1`. Never used revert before, but thought maybe it would create a new commit which goes back to the previous state. This would mean I would have two commits with the identical content, and `0fb5d27e1bc57b120e0f361f8f71a947bb264448` between them. I suppose that would be fine as long as that is how revert works. – user1032531 Feb 04 '16 at 13:44
  • Possible duplicate of [How do you undo the last commit?](http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit) – LeGEC Feb 04 '16 at 13:50
  • Sorry, I didn't see your comment before I answered. Yes, that's how it works, but you want to `revert HEAD` not `HEAD~1`. With `revert`, you specify the commit to undo. – N3dst4 Feb 04 '16 at 13:50
  • @LeGEC I don't think the "duplicated question" preserves history of the reverted commit. – user1032531 Feb 04 '16 at 13:52

1 Answers1

1

git revert HEAD while you have the most recent commit checked out will create a new commit on top of your current HEAD which undoes the changes in it. This will leave the code in the same state it was before that commit, but with all your history in tact.

o  (HEAD) Revert - gets back to state A
|
o  Commit that we want to preserve but undo
|  
o  Last good commit, call it state A
|
|  (previous history)
N3dst4
  • 6,360
  • 2
  • 20
  • 34
  • Thanks N3dst4. Why `git revert HEAD` and not `git revert HEAD~1`? Looking at https://git-scm.com/docs/git-revert and trying to figure out how it works. – user1032531 Feb 04 '16 at 13:50
  • Haha, there's a race condition in this conversation :) With `revert`, you specify the commit to undo, not the commit you want to get back to. – N3dst4 Feb 04 '16 at 13:51
  • Haha, yes! So, I am reverting the "HEAD" commit which goes back to the second one (which is the one I want!)? But if I wanted to go to the third, then I would use `git revert HEAD~1`? – user1032531 Feb 04 '16 at 13:57
  • To get back to the third one, you'd revert the last two, so you'd give `revert` a range: `git revert HEAD..HEAD~1`. – N3dst4 Feb 04 '16 at 13:59