How to bring back changes after a "git reset

I realized that our branch dev is not very stable and reverted some changes to work on debugging them.

git checkout dev
git checkout -b staged_dev
git push origin staged_dev
# go back to dev
git checkout dev
git reset 230086bf429307182ed9b35e19ad2581a3a2baf6 # last known stable state of dev
git reset --soft HEAD@{1}
git commit -m "Revert to 230086bf429307182ed9b35e19ad2581a3a2baf6" # this commit SHA = 270329c
git reset --hard
git push origin dev

This added a new commit on top of dev : 270329c

Since then another person has added some safe commits to dev and I have debugged staged_dev and added some commits. How do I undo 270329c from dev and add the other commits of staged_dev into dev ?

reference stackoverflow question : Revert to a commit by a SHA hash in Git?


git reflog should show you where things were so that you can create an old_dev branch from it.

See reflog, your safety net


Your simplest choice might be just:

git checkout dev
git merge staged_dev
git push origin dev

If you're not happy with the resulting history, you could first rebase your revised changes to the tip of the "dev" branch:

git checkout staged_dev
git rebase dev
git checkout dev
git merge staged_dev

If you want your revised commits in "staged_dev" to take the same place in the history as the original 230086 commit, you're going to have to get fancier, and there is no way to do that without deleting revision history in "dev" that has already been published in "origin" . But if that's what you really need, you could do something like:

git checkout -b new-dev 230086bf  # point where "dev" diverged
git cherry-pick <first commit you want to include*>..staged_dev
git checkout -t origin/dev
git rebase new-dev
git push origin +dev

You may want to announce to everybody that local checkouts they have of "dev" may no longer be in the upstream history.

链接地址: http://www.djcxy.com/p/18846.html

上一篇: 在git重置后提交一个现有的提交

下一篇: 如何在“git重置后恢复更改”