简体   繁体   English

如何恢复到以前的Git提交,然后将其保存在单独的分支中?

[英]How do I revert back to a former Git commit and then save it in a separate branch?

I have over-engineered myself into a corner. 我把自己过度设计成了一个角落。 The two things that need to happen are: 需要做的两件事是:

  1. How do I revert back to an earlier working version of my code?...and simultaneously , 如何恢复到我的代码的早期工作版本?... 同时

  2. How do I then create a separate branch to save that code while still having a saved version of my new code to draw from? 然后我如何创建一个单独的分支来保存该代码,同时仍然保存我的新代码的保存版本?

Any help is greatly appreciated 任何帮助是极大的赞赏

If you know the commit SHA you want to go back to: 如果您知道要返回的提交SHA:

git reset <SHA>
git checkout -b <new_branch_name>

Before switching branches, if you have pending changes, commit them. 在切换分支之前,如果您有挂起的更改,请提交它们。

If you know the commit SHA you want to go back to, you can create a new branch from it and switch to it: 如果您知道要返回的提交SHA,可以从中创建一个新分支并切换到它:

git checkout -b <new_branch_name> SHA

Note down the SHA of the last commit (say e123asd) 记下最后一次提交的SHA(比如e123asd)

To remove last commit from branch 1 从分支1中删除最后一次提交

git reset --hard HEAD^

Switch to branch 2 切换到分支2

git cherry-pick e123asd

This will bring the last commit to this new branch. 这将使最后一次提交到这个新分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Git中恢复到较早的提交? - How do I revert back to an older commit in Git? 如何删除我创建的分支并将所有内容还原到master分支的最后一次提交? - How do I delete a branch I created and revert everything back to the master branch's last commit? 恢复到git分支中的最后一次提交 - revert back to last commit in git branch 恢复git上次提交并将其保存在不同的分支中 - revert the git last commit and save it in a different branch Git:如何签出提交并将其合并回我的分支? - Git: How do I checkout a commit and merge it back into my branch? 在git中,如何从分支中删除提交但是将该提交保留在单独的分支中? - In git, how do I remove a commit from a branch but keep that commit in a separate branch? Git:如何撤消提交*和*恢复到最后一个分支 - Git: How to Undo commit *and* revert to last branch git恢复为分支历史中已存在但已撤消的特定提交 - git revert back to a specific commit that is already in the branch history but was undone 如何恢复我的推或git上的提交 - how to revert back my push or commit on git Eclipse Git(EGit)-在与本地分支进行错误合并和提交之后,如何将本地主服务器恢复为远程主服务器 - Eclipse Git (EGit) - How to revert local master back to remote master after a foul merge & commit with a local branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM