0

I need to go back to certain commit which is 7 commits before my current commit. If I do, I want to change all files to exactly look like previous commit. But I don't want to erase git commit history in github. How do I keep the history and go back to previous state?

Thank you.

JoHksi
  • 517
  • 1
  • 7
  • 26
  • You can't do this. If you nuke 7 commits on your local branch, the only way to push this to GitHub would be to do a `git push --force`, which however will also rewrite history on the remote. – Tim Biegeleisen Jun 14 '16 at 05:38
  • @TimBiegeleisen So there is no way to go back to previous commit by keeping the history? I don't care making commits after what I have. But I need to keep those 7 latest commits since I need to inspect those one by one. – JoHksi Jun 14 '16 at 05:39
  • You seem to be confused about how Git works. If you go back 7 commits, either via `git reset` or just by checking out a previous commit in a detached state, then every subsequent commit you make will mean rewriting history and obliterating the original commit history. If you can update your question with what you are really trying to do, maybe someone can help. – Tim Biegeleisen Jun 14 '16 at 05:40
  • @Tim git revert is pretty posssible :), but as asked its just checkout a specific commit – AD7six Jun 14 '16 at 05:44
  • @AD7six Yes, this seems like maybe it is what he wants. – Tim Biegeleisen Jun 14 '16 at 05:44
  • `git reset --hard`. Now the work tree is sha1's tree. The commits after sha1 seem lost but actually they are hidden. A potential risk is that your current branch now refers to the sha1. If the sha1 itself is reachable from your current branch, it's okay. If not, for example it's of another branch but not reachable from the current, your current branch goes to a diverged state. `git checkout -b ` is a safer solution. As long as you know exactly what the HEAD refers to, you can manipulate the branch as you like. – ElpieKay Jun 14 '16 at 06:24
  • @ElpieKay please don't answer questions in a comment - write an answer. – AD7six Jun 14 '16 at 09:02

1 Answers1

1

Try this:

 git checkout <commit>
dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • In this case commit is commit number and not branch name so default behavior is as described in https://git-scm.com/docs/git-checkout – dmitryro Jun 14 '16 at 06:02