简体   繁体   English

如何在非连续提交之间移动更改?

[英]How to move changes between not-consecutive commits?

So, the situation is like this: 因此,情况如下:

Commit A
**Commit B**
Commit C
Commit D
**Commit E**

Is it possible to pick one file-change from Commit E and move it to Commit B ? 是否可以从提交E中选择一个文件更改并将其移动到提交B中 Commits C and D don't have anything related to that specific file so I am hoping this is doable. 提交C和D与该特定文件没有任何关系,因此我希望这是可行的。

You can checkout the file as it is in COMMIT-E 您可以像在COMMIT-E中一样检出文件
Note that this will undo any changes you did to the file in your current branch. 请注意,这将撤消您对当前分支中的文件所做的任何更改。 You can however edit out the unwanted changes to that file easily because they will be in your commit diff. 但是,您可以轻松编辑出对该文件的不需要的更改,因为它们将在您的提交差异中。

# checkout COMMIT-B, ether check out the commit directly ( headless mode ), or checkout the relevan branch.
git checkout COMMIT-B 
git checkout COMMIT-E ./dir/myfile.ext

The file is now in your working directory, you can commit it individually or as part of an other commit. 该文件现在位于您的工作目录中,您可以单独提交,也可以作为其他提交的一部分提交。 If you checked out COMMIT-B directly ( without a branch ) make sure you create a branch before you navigate away. 如果直接签出COMMIT-B(不带分支),请确保在导航之前创建分支。

Note that if you don't specify the path for the checkout command, your HEAD will change, which you don't want. 请注意,如果您未指定checkout命令的路径,则您的HEAD将会更改,而您不需要这样做。

If you made any changes to the specific file in your current branch, and you want to preserve those without having to edit the file ( as mentioned above ), you will need to cherry pick the change in commit-E. 如果您对当前分支中的特定文件进行了任何更改,并且想要保留这些更改而不必编辑文件(如上所述),则需要在commit-E中选择更改。

# checkout COMMIT-B, ether check out the commit directly ( headless mode ), or checkout the relevan branch.
git checkout COMMIT-B 
git cherry-pick -n COMMIT-E
# clean up the changes you don't want, then commit the changes you are interested in.

If there are multiple changes to the file in the other branch, spread out over multiple commits, it might be tricky to cherry pick all those changes. 如果另一个分支中的文件有多个更改,并且分布在多个提交中,那么挑选所有这些更改可能很棘手。 You could try to rebase the branch on top of your current branch and then checkout the file again as demonstrated above. 您可以尝试将分支重新设置为当前分支的基础,然后再次如上所述检出文件。 Or you could create a temporary branch at your current branch, merge the other branch in your temporary branch, checkout COMMIT-B, checkout the file from the temporary branch, commit the file ( clean up by deleting the temporary branch and reset the merged-in branch back to where it came from ) 或者,您可以在当前分支上创建一个临时分支,合并该临时分支中的另一个分支,签出COMMIT-B,从该临时分支签出该文件,提交该文件(通过删除该临时分支进行清理,然后重置合并的-在分支回到它来自哪里)
All tough rebasing is probably a more straightforward option, the end result should be similar for the targeted file. 所有艰难的重新部署可能是一个更直接的选择,最终结果应与目标文件相似。

Try with git-rebase : 尝试使用git-rebase

git rebase -i commitA~1

This will open your default editor to choose what commit from commitA you want to redo. 这将打开您的默认编辑器,以选择要重做的commitA提交。

You will see something like this: 您将看到如下内容:

pick SHA1 commitA
pick SHA1 commitB
pick SHA1 commitC
pick SHA1 commitD
pick SHA1 commitE

Change it as you wish. 根据需要更改它。 In your case: 在您的情况下:

pick SHA1 commitA
pick SHA1 commitE
pick SHA1 commitC
pick SHA1 commitD
pick SHA1 commitB

This will generate a new history of commit with different SHA1 but with the same changes in the new order. 这将生成具有不同SHA1但在新顺序中具有相同更改的新提交历史记录。 Depending on the order, or if you remove some commits, you can have conflicts. 根据顺序,或者如果您删除一些提交,则可能会发生冲突。 Be careful. 小心。

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

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