7

We need to go back in time to a particular commit. Some accidental changes were made to master. Attempts to revert it dug too deep, so master is in a bad state. We now want master to go back to 66ada4cc61d62afc.

According to git revert back to certain commit:

$ git reset --hard 66ada4cc61d62afc
HEAD is now at 66ada4c Updated documentation

Then, trying to commit it:

$ git add *.h *.cpp
$ git commit -m "Go back to Commit 66ada4cc61d62afc"
On branch master
Your branch is behind 'origin/master' by 16 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working directory clean

And finally:

$ git push
To https://github.com/weidai11/cryptopp.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/weidai11/cryptopp.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Right now, everything is exactly where I want it to be. I have no idea why Git is having trouble, and what Git is talking about. It sure would be nice if Git did what it was told. But alas, Git makes every simple task difficult, and its going to inflict undue pain and suffering.

How do I commit and push the changes?

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885

3 Answers3

9

The quick solution: git push -f

This forces the push and overwrites the remote history. If there are other people using this remote repository: don't do this!

Other people who pull from the remote repository will run into problems, since their history is no longer a subset of the history of the repository. Most projects forbid forced pushs.

A better solution is to create a new commit for your change. Either

  • git revert hashes or
  • git checkout oldversion .; git add/commit

This then creates a new commit on top of your history that describes the change needed to go back to whichever version you want.

Robin Roth
  • 1,289
  • 10
  • 13
  • 1
    *"A better solution is to create a new commit for your change..."* - that's what we are trying to do. We want the next commit to change things back to where things were based on current state. – jww Jul 06 '16 at 17:32
  • do `git checkout 66ada4cc61d62afc .; git add .; git commit -m "foo"` `git checkout version` brings you info detached head, `git checkout version filename` gets the files from the history – Robin Roth Jul 06 '16 at 17:48
  • 2
    @RobinRoth `git checkout .` will not remove files though. A safer way would be `git checkout && git reset --soft master && git commit` – poke Jul 06 '16 at 18:12
5

git reset won't revert your changes. It will just go back to an older state. Since the latest commits are already in the remote, and you are not making any local changes, you won't be able to push.

Use git revert <hash> or git revert <hash1>..<hash2>. This will create a new commit which reverts the changes of the commit you specified. Then push the code.

Msp
  • 2,493
  • 2
  • 20
  • 34
  • `git revert` was tried, but it managed to undo changes prior to `66ada4cc61d62afc`. I think that's because the PR that accidentally got pushed was against a previous state of the library. – jww Jul 07 '16 at 21:43
-4

To avoid partial solutions that simply exchanged one set of errors for another, I performed the following:

git clone https://github.com/weidai11/cryptopp.git cryptopp-old
git clone https://github.com/weidai11/cryptopp.git cryptopp-new

cd cryptopp-old
git checkout 66ada4cc61d62afc

cd ../cryptopp-new
cp ../cryptopp-old/*.h ../cryptopp-old/*.cpp .

git commit -am "Go back to Commit 66ada4cc61d62afc"
jww
  • 97,681
  • 90
  • 411
  • 885