几个修改过的文件.只提交分支A中的文件A,分支B中的文件B

  • 本文关键字:文件 分支 提交 几个 修改 git
  • 更新时间 :
  • 英文 :


我有一个正在进行的项目。每当我想创建一个新功能时,我都会从它的主分支中进行分支,并创建一个名为FeatureX的新功能,稍后我会将其合并到主分支中。

假设我创建了一个名为featureA的分支,在那里我修改了featureA(在fileA.py中),也修改了feature B(在fileB.py中)。

在branchA上,如果像往常一样提交,我会把fileB中所做的更改包括在branchA中,这是我不想要的。我想提交在分支A中的文件A中所做的更改和在分支B中的文件B中所作的更改。

在branchA中,我怎么能只提交在fileA.py中所做的更改,然后签出到branchB,并在那里只提交对fileB.py的更改?

编辑:

我在最初的消息中没有具体说明这一点,也许我应该澄清一下:fileA和fileB存在于两个分支(branchA和branchB)中。

但是,当我在branchA中提交时,我只想提交在fileA中所做的更改,而不修改fileB。然后签出到branchB,只提交对fileB所做的更改,使其版本的fileA保持不变。

确保您选择了功能A:的正确分支

git checkout featureA

添加更改并提交

git add fileA.py  # use correct path to the file, of course
git commit -m "Glorious updates to fileA"

现在,要将对fileB.py的更改与其他功能分支一起进行,有一个简单而复杂的情况。

简单情况:fileB.py在分支之间没有差异

转到另一个分支,并执行相同的

git checkout featureB
git add fileB.py
git commit -m "Fabulous bugfixes to fileB" 

(略)更复杂的情况:fileB.py不同

如果fileB.py不同,则featureB的签出将失败(Git将中止签出,并显示一条消息,说明未提交的更改将被覆盖)。因此,您可以尝试签出,除非使用-f/--force开关或使用其他类似git clean的东西,否则您不会丢失更改。你会得到这个错误:

error: The following untracked working tree files would be overwritten by checkout:
fileB.py
Please move or remove them before you can switch branches.
Aborting

你得把零钱藏起来一会儿。像这样:

git stash # will save all uncommitted changed into a temporary space
git checkout featureB
git stash pop # will try to apply your saved changes; there may be a conflicts
# if there are conflicts (Git will tell you), go through normal  resolution
git add fileB.py
git commit -m "Incandescent stash of bugfixes to fileB" 

最新更新