首页 > 解决方案 > 将影响某些文件的更改拆分到不同的分支

问题描述

假设我一直在一个分支中处理文件 a、b、c 和 d。现在我意识到我更喜欢将我对 a 和 b 的更改放在一个分支中,而我对 c 和 d 的更改放在另一个分支中,所以从每个分支的角度来看,就好像我只修改了两个文件一样。

这可能吗?

标签: git

解决方案


索引是 git 用来处理这种需求的工具。

git 会检测到您的所有更改,但不会“暂存”(例如,准备好提交),直到您这么说。

# in case you already added all or part of your changes, undo it
git reset

# if needed, create the new branch (or stay on the one you worked on from start)
git checkout -b new_branch

# now add only parts for branch 1
git add a b
git commit -m "Modified a and b"
# let's call this commit Y

# now for the second part
git checkout -b new_branch_2 master
git add c d
git commit -m "Modified c and d"
# let's say this commit Z

导致这种情况(我在这里假设您的基础分支在所有这些被调用之前 master其最后一次提交是 X

     Y <<< new_branch_1
    /
---X <<< master
    \
     Z <<< new_branch_2 <<< HEAD

最后,非常明确地说,现在您的文件a和.bmasterZ


推荐阅读