如何将未推送的提交代码移至另一个分支? [英] How do I move unpushed committed code to another branch?

查看:62
本文介绍了如何将未推送的提交代码移至另一个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下情况:

我在本地进行了一些工作,而没有推送到远程存储库.我想将本地代码移到另一个分支,因为如果我拉,将进行一些修改,这些修改将破坏我在本地所做的所有工作.

I committed some work locally, without pushing to the remote repository. I want to move this local code to another branch, because if I pull, there will be modifications that will ruin all the work I put locally.

这是旧分支上的 git status 的输出:

This is the output of git status on the old branch:

On branch <branch_name>
Your branch is ahead of 'origin/<branch_name>' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

这是新创建的分支上 git status 的输出:

And this is the output of git status on the newly created branch:

On branch <branch_name>
nothing to commit, working directory clean

推荐答案

如果在提交后创建了分支,则分支应包含要移动的提交.您可以使用 git log 进行验证,您应该在日志中看到自己的提交.

If you made the branch after you committed it should contain the commit that you are wanting to move. You can verify this with git log, you should see your commit as the first on in the log.

在您不再希望执行提交的分支上, git reset --hard HEAD〜.这将删除分支中的提交并重置分支,这样您现在就可以毫无问题地进行提取了.(确保您的提交在另一个分支上,因为这样做之后您的提交将消失).

On the branch that you no longer want the commit to be do git reset --hard HEAD~. This removes the commit from the branch and reset the branch so that you can now pull without any issue. (Be sure that your commit is on the other branch as after doing this your commit will be gone).

如果提交不在您的另一个分支上,则可以删除该分支,然后使用 git checkout -b< branch name> 从原始分支重新创建该分支,或者可以选择使用 git cherry-pick< commit; 将它插入分支,这将在分支上复制该提交.然后,您可以按照上述步骤重置原始分支.

If the commit is not on your other branch, you can either delete the branch and create it again from the original branch with git checkout -b <branch name> or you can cherry-pick it into your branch with git cherry-pick <SHA of your commit> which will make a copy of the commit on your branch. Then you can reset the original branch with the steps above.

这篇关于如何将未推送的提交代码移至另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆