简体   繁体   English

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

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

I made changes to the feature / project branch and committed it, and then I realized that I did something wrong in the branch, I had to do it in the feature / nfts-list-and-detail branch I used the following method, but this is if we do not have a committed我在feature / project分支做了修改并提交,然后我意识到我在分支做错了,我必须在feature / nfts-list-and-detail分支做我使用了下面的方法,但是这是如果我们没有承诺

$ git stash

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

$ git stash pop

How can I transfer my changes to the feature / nfts-list-and-detail ?如何将我的更改转移到feature / nfts-list-and-detail

"Git is all about commits" as explained by fellow user torek .正如其他用户 torek 所解释的那样, “Git 就是关于提交的”。

So what does this mean for you?所以这对于你来说意味着什么? A commit is not on or in a branch.提交不在分支上或分支中。 A branch points to a commit and if you follow the parent commits, you get the history of this branch.一个分支指向一个提交,如果你跟随父提交,你会得到这个分支的历史。 But branches no more than dynamic labels.但分支不过是动态标签。 These they can be created, renamed, deleted.它们可以被创建、重命名、删除。

As long as you have not pushed anything to a remote repository, there is nothing to worry about: cherry-pick the commit to the correct branch and then roll back your other branch.只要您没有将任何内容推送到远程存储库,就无需担心:cherry-pick 提交到正确的分支,然后回滚您的其他分支。 Cherry-picking creates a new commit with the same (or similar) changes than your original one. Cherry-picking 会创建一个新的提交,其中包含与原始提交相同(或相似)的更改。

Here are two ways to do it (note that branch names cannot contain blanks):这里有两种方法(注意分支名不能有空格):

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

There are other ways with varying degrees of complexity and possibilities to shoot yourself in the foot.还有其他不同程度的复杂性和搬起石头砸自己脚的可能性的方法。

If you have already pushed to the wrong branch, things become a bit more tricky.如果你已经推到了错误的分支,事情就会变得有点棘手。 But if you don't care for the history showing that the commit was there, a simple git revert will do the job.但是,如果您不关心显示提交存在的历史记录,那么简单的git revert就可以完成这项工作。

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

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