Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit and push changes after going back to a particular revision in the repository?

Tags:

git

git-commit

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?

like image 680
jww Avatar asked Jul 06 '16 17:07

jww


1 Answers

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.

like image 54
Robin Roth Avatar answered Nov 08 '22 18:11

Robin Roth