如何修复提交到错误Git分支的问题?
我刚刚对错误的分支做出了完美的承诺。 如何撤消主分支中的最后一次提交,然后将这些更改保存到升级分支中?如果您有一个干净(未修改)的工作副本 要回滚一次提交(请确保在下一步中记下提交的哈希值): 要将该提交拉入另一个分支,请执行以下操作:如何修复提交到错误Git分支的问题?,git,git-commit,Git,Git Commit,我刚刚对错误的分支做出了完美的承诺。 如何撤消主分支中的最后一次提交,然后将这些更改保存到升级分支中?如果您有一个干净(未修改)的工作副本 要回滚一次提交(请确保在下一步中记下提交的哈希值): 要将该提交拉入另一个分支,请执行以下操作: git checkout other-branch git cherry-pick COMMIT-HASH 如果您已修改或未跟踪更改 还请注意,git reset--hard将杀死您可能进行的任何未跟踪和修改的更改,因此如果您进行了这些更改,您可能会更喜欢:
git checkout other-branch
git cherry-pick COMMIT-HASH
如果您已修改或未跟踪更改
还请注意,git reset--hard
将杀死您可能进行的任何未跟踪和修改的更改,因此如果您进行了这些更改,您可能会更喜欢:
git reset HEAD^
git checkout .
如果尚未推动更改,还可以进行软重置:
git reset --soft HEAD^
这将恢复提交,但将提交的更改放回索引中。假设分支相对而言是最新的,git将允许您签出到另一个分支,然后您可以简单地提交:
git checkout branch
git commit -c ORIG_HEAD
-c ORIG_HEAD
部分有助于不再键入提交消息。如果您已经推送了更改,则需要在重置HEAD后强制下一次推送
git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force
警告:硬重置将撤消工作副本中任何未提交的修改,而强制推送将用本地分支的当前状态完全覆盖远程分支的状态
以防万一,在Windows上(使用Windows命令行,而不是Bash),它实际上是四个而不是一个,所以
git reset --hard HEAD^^^^
在这个话题上晚了4年,但这可能对某人有所帮助 如果在提交和提交master上的所有内容之前忘记创建新分支,则无论您进行了多少次提交,以下方法更容易:
git stash # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop # skip if all changes were committed
现在您的主分支等于
origin/master
,所有新提交都在myu功能上。请注意,my_feature
是一个本地分支,而不是远程分支。因此,如果您的场景是您已提交给master
,但打算提交给另一个分支(该分支可能已经存在,也可能不存在),但尚未推送,那么这很容易修复
// if your branch doesn't exist, then add the -b argument
git checkout -b another-branch
git branch --force master origin/master
现在,您对主机
的所有提交都将在另一个分支上
来源于:如果您想要将更改应用到的分支已经存在(例如分支开发),请按照以下提供的说明进行操作,然后:
git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature
显然,如果您愿意,您可以使用tempbranch或任何其他分支名称来代替my_功能
此外,如果适用,请将隐藏弹出窗口(应用)延迟到在目标分支处合并后再执行。如果遇到此问题,并且您有Visual Studio,则可以执行以下操作:
// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes.
git reset HEAD~1
// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash
// create other-branch (if the other branch doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// take the temporary commit you created, and apply all of those changes to the new branch.
//This also deletes the temporary commit from the stack of temp commits.
git stash pop
// add the changes you want with git add...
// re-commit your changes onto other-branch
git commit -m "some message..."
右键单击您的分支并选择查看历史记录
:
右键单击要返回的提交。并根据需要恢复或重置
我最近也做了同样的事情,我意外地将更改提交给了master,而我本应该提交给其他分支。但我什么都没推
如果您只是承诺了错误的分支机构,并且自那以后没有做任何更改,也没有推动回购,那么您可以执行以下操作:
// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes.
git reset HEAD~1
// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash
// create other-branch (if the other branch doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// take the temporary commit you created, and apply all of those changes to the new branch.
//This also deletes the temporary commit from the stack of temp commits.
git stash pop
// add the changes you want with git add...
// re-commit your changes onto other-branch
git commit -m "some message..."
注意:在上面的示例中,我使用git reset HEAD~1倒带了1个commit。但是如果您想倒带n个提交,那么可以执行git reset HEAD~n
此外,如果您最终提交了错误的分支,并且在意识到提交了错误的分支之前又编写了一些代码,那么您可以使用git stash保存正在进行的工作:
// save the not-ready-to-commit work you're in the middle of
git stash
// rewind n commits
git reset HEAD~n
// stash the committed changes as a single temp commit onto the stack.
git stash
// create other-branch (if it doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// apply all the committed changes to the new branch
git stash pop
// add the changes you want with git add...
// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."
// pop the changes you were in the middle of and continue coding
git stash pop
注:我使用此网站作为参考
若要详细说明答案,您需要从多个提交转移,例如从开发到新分支机构:
git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started
对于错误分支上的多个提交
如果对您来说,大约只有1次提交,那么还有很多其他更容易的重置解决方案可用。对我来说,我有大约10次提交是在master
分支上意外创建的,我们称之为target
,我不想丢失提交历史记录
你所能做的,以及拯救我的,是作为一个参考,使用一个4步骤的过程,这是-
从master
将temp
合并到最初用于提交的分支中,即target
撤消主机上的提交
删除临时分支temp
下面是上述步骤的详细内容-
从主节点创建一个新分支(我在那里意外地做了很多更改)
注意:-b
标志用于创建新分支
为了验证我们是否正确,我将快速执行git分支
,以确保我们在temp
分支上,并执行git日志
以检查提交是否正确
将临时分支合并到最初用于提交的分支中,即target
首先,切换到原始分支,即target
(如果没有,您可能需要git fetch
)
注意:不使用-b
标志
现在,让我们将临时分支合并到当前签出的分支target
git merge temp
如果有冲突的话,你可能需要处理一些冲突。成功合并后,您可以推动(我会)或继续下一步
撤消master
上的意外提交,使用作为参考,首先切换到master
git checkout master
然后使用下面的命令将其全部撤消以匹配远程(或者如果需要,使用适当的命令进行特定的提交)
同样,我会在之前和之后做一个git日志
,以确保预期的更改生效
删除证据,即删除临时分支。为了这个,首先哟
git merge temp
git checkout master
git reset --hard origin/master
git checkout target
git branch -d temp
git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH