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

谁都会走 提交于 2019-12-11 02:18:32

问题


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?


回答1:


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.




回答2:


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.




回答3:


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"


来源:https://stackoverflow.com/questions/38229852/commit-and-push-changes-after-going-back-to-a-particular-revision-in-the-reposit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!