Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在git中合并特定文件_Git_Git Branch_Git Merge - Fatal编程技术网

如何在git中合并特定文件

如何在git中合并特定文件,git,git-branch,git-merge,Git,Git Branch,Git Merge,假设我在MASTER branch上有一个包含100个php文件的项目。为了在项目中修复bug,我创建了一个单独的分支 git checkout -b bugfix 然后在修复了3个文件(例如index.php、register.php和login.php)中的错误后,我将其合并到主分支中 git checkout master git merge bugfix 上面的代码将合并我所做更改的所有3个文件,但是我是否可以强制GIT只合并2个文件,比如login.php和register.php

假设我在MASTER branch上有一个包含100个php文件的项目。为了在项目中修复bug,我创建了一个单独的分支

git checkout -b bugfix
然后在修复了3个文件(例如index.php、register.php和login.php)中的错误后,我将其合并到主分支中

git checkout master
git merge bugfix

上面的代码将合并我所做更改的所有3个文件,但是我是否可以强制GIT只合并2个文件,比如login.php和register.php?

无法在GIT中记录部分合并,所有合并必须合并所有合并提交的整个根树


当然,您可以选择通过从父级中选择一个文件的未更改副本来解决该合并,但这仍然是一个“完整”合并,在涉及分支之间的后续重新基准或合并时,该文件中的更改将被视为已被合并。

不,没有直接的方法。尽管;您可以从要合并的分支中选择文件,并覆盖主分支中的现有文件。顺序命令应为:

git checkout master
git show bugfix:login.php > login.php
git show bugfix:register.php > register.php
git add .
git commit -m "Select login and register from branch/bugfix"

好的,上面的方法将“销毁”文件历史记录。关于这一点,有一个古老的讨论

从技术角度来看,让历史变得重要的东西(以及 为什么“按日期顺序提交的集合”是无用的)是因为它给了我们 共同的祖先!这是唯一重要的事情

现在,按文件执行历史记录的根本错误是什么

现在,如果你遵循这个论点,你应该说“啊! 显而易见

忽略了大量的段落;您将找到解决方法:

git diff commit..othercommit filename | git-apply --index && git commit

在您的情况下,
commit
=您的主管/主分支的哈希;和
othercommit
=错误修复分支的散列。

有两种方法:


方法01 以下解决方案采用自

事实证明,结帐在这方面也有帮助。您只需从另一个分支调出(签出)这些特定文件:

# switch to the branch you want to be your merge destination
git checkout master

# checkout specific files from specific branch (e.g bugfix)
# format: git checkout <source branch> <file.1> <file.2> ... <file.N>
git checkout bugfix login.php register.php

# check the status
git status

# merge them in
git commit -m "your merge comment"
# get which commit you want to take to the other branch (first 7 characters will do)
git log

# switch to the branch you want to be your merge destination
git checkout master

# bring specific commit to this branch (replace the 63344f2 with your own 7 character commit ID)
git cherry-pick 63344f2

您可以模拟所需的行为,但对第三个文件所做的更改会发生什么情况

第三个文件上的更改仍保留在分支上的提交中,但它们不会出现在合并的提交中,就像该文件在分支上根本没有被修改一样

这就是你可以做到的:

git checkout master
git merge bugfix --no-commit
--no commit
选项告诉
git
合并文件,但在提交之前停止。您现在可以检查文件,更改它们,做任何您想做的事情。要取消对
index.php
所做的更改,可以运行:

git checkout master -- index.php

运行
git status
可以看到
index.php
不再显示为已修改(无论是在索引中还是在工作树中)。您现在可以
git commit
仅此而已。

我认为一种方法是在一次提交中提交这两个文件,在另一次提交中提交最后一个文件,我认为您可以指定一个提交并仅合并该文件。@dutt OPs的问题是文件更改已经提交,我想这可能有助于找到一种方法,您确定吗,你真的想要一个Git没有优化的工作流吗?在我看来,如果你想单独合并它们,你应该改变你的工作流程并创建两个错误修复分支。它不会将分支合并到
master
@axiac中。如果你仔细阅读这个问题,你会意识到问题不是合并分支,而是合并文件(将特定文件从另一个分支带到所需分支的术语是错误的)。上述解决方案有效。它不会将分支合并到
master
。此解决方案类似于更改所有文件,然后尝试手动更正混乱!!阅读以了解此堆栈的内容以及您的解决方案浪费时间和人力的原因。