繁体   English   中英

将更改从一个提交的分支转移到另一个分支

[英]Transfer changes from one committed branch to another branch

我在feature / project分支做了修改并提交,然后我意识到我在分支做错了,我必须在feature / nfts-list-and-detail分支做我使用了下面的方法,但是这是如果我们没有承诺

$ git stash

$ git checkout feature / nfts-list-and-detail

$ git stash pop

如何将我的更改转移到feature / nfts-list-and-detail

正如其他用户 torek 所解释的那样, “Git 就是关于提交的”。

所以这对于你来说意味着什么? 提交不在分支上或分支中。 一个分支指向一个提交,如果你跟随父提交,你会得到这个分支的历史。 但分支不过是动态标签。 它们可以被创建、重命名、删除。

只要您没有将任何内容推送到远程存储库,就无需担心:cherry-pick 提交到正确的分支,然后回滚您的其他分支。 Cherry-picking 会创建一个新的提交,其中包含与原始提交相同(或相似)的更改。

这里有两种方法(注意分支名不能有空格):

git checkout feature/nfts-list-and-detail # switch to correct branch
git cherry-pick feature/project # create copy of latest commit of wrong branch
git branch -f feature/project feature/project # undo latest commit on wrong branch
git checkout feature/project # switch to wrong branch
git reset --mixed HEAD^ # undo latest commit, keep changes in working tree
git stash
git checkout feature/nfts-list-and-detail
git stash pop
git add files you want to commit
git commit

还有其他不同程度的复杂性和搬起石头砸自己脚的可能性的方法。

如果你已经推到了错误的分支,事情就会变得有点棘手。 但是,如果您不关心显示提交存在的历史记录,那么简单的git revert就可以完成这项工作。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM