我可以从另一个分支添加对暂存区的更改吗?
Can I add changes to staging area from another branch?
我有两个分支,
- 硕士
- 新功能
新功能有一个提交我想添加到 master 但我想要控制我想合并多少。
我希望 add/reject 选项对每行进行更改。类似的我可以用 git add -p
我搜索了很多可能是我搜索了错误的术语。这似乎是很明显的任务。
我觉得你可以尝试使用补丁文件。
创建补丁文件运行:
git checkout master
git diff ..new-features > patch.diff
在文件 patch.diff
中,master 和 new-features 分支之间存在差异。
现在可以应用补丁文件了,运行:
git apply patch.diff
现在您可以按任何需要的方式管理您的更改。
使用 git cherry-pick
和 -n|--no-commit
选项,然后交互 select 要提交的内容:
git cherry-pick
...
-n
, --no-commit
Usually git cherry-pick
automatically creates a sequence of commits. This
flag applies the changes necessary to cherry-pick each named commit to your
working tree and the index, without making any commit. In addition, when
this option is used, your index does not have to match the HEAD commit. The
cherry-pick is done against the beginning state of your index.
This is useful when cherry-picking more than one commits' effect to your index
in a row.
所以命令的顺序如下:
git cherry-pick -n <commitid> # merge commitid into the index and working-tree
git reset # clear the index
git add -p # selectively add merged changes to the index
或者,您可以使用 git reset -p
从暂存区中删除 不需要的帅哥:
git cherry-pick -n <commitid> # merge commitid into the index and working-tree
git reset -p # interactively remove from the index changes brought by commitid
我有两个分支,
- 硕士
- 新功能
新功能有一个提交我想添加到 master 但我想要控制我想合并多少。
我希望 add/reject 选项对每行进行更改。类似的我可以用 git add -p
我搜索了很多可能是我搜索了错误的术语。这似乎是很明显的任务。
我觉得你可以尝试使用补丁文件。
创建补丁文件运行:
git checkout master
git diff ..new-features > patch.diff
在文件 patch.diff
中,master 和 new-features 分支之间存在差异。
现在可以应用补丁文件了,运行:
git apply patch.diff
现在您可以按任何需要的方式管理您的更改。
使用 git cherry-pick
和 -n|--no-commit
选项,然后交互 select 要提交的内容:
git cherry-pick
...
-n
,--no-commit
Usually
git cherry-pick
automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.This is useful when cherry-picking more than one commits' effect to your index in a row.
所以命令的顺序如下:
git cherry-pick -n <commitid> # merge commitid into the index and working-tree
git reset # clear the index
git add -p # selectively add merged changes to the index
或者,您可以使用 git reset -p
从暂存区中删除 不需要的帅哥:
git cherry-pick -n <commitid> # merge commitid into the index and working-tree
git reset -p # interactively remove from the index changes brought by commitid