我在分支 B 上工作。我创建了我的分支并从最新的主提交中 check out 。我的同事在分支 A 工作。他很久以前就从 master 那里 checkout 了,所以他在后面:
--------- A1
/
/
/
--------- M1 --------- M2 --------- M3 --------- M4 --------- M5 --------- B1
在他的分支机构中,他处理了很多文件,而我只需要其中的一部分。我们称它们为 File1.txt、File2.txt 和 File3.txt。我想将这些文件 merge 到我的分支。我的问题是:在这种情况下应遵循什么方法?我应该在他过时的分支之上 merge/ rebase 吗?有没有办法只获取这 3 个文件并将它们 merge 到我当前的工作分支并获得 B2 提交?
最佳答案
您可以使用 git checkout --patch <branch> <filename>
通过使用来自另一个分支的文件来修补当前分支中已经存在的文件。如果当前分支中不存在该文件,请使用 git checkout <branch> <filename>
在你的分支中编辑它。接下来,您可以保存文件并提交更改。
在您的情况下(例如 file1.txt):
去分支机构B
git checkout B
使用
git checkout --patch A file1.txt
或者,如果分支 B 中不存在 file1.txt,则使用git checkout A file1.txt
关于
Apply this hunk to index and worktree
选择y
保存文件并
git add file1.txt
并使用git commit -m 'Your commit message'
提交您的更改
这里是对git checkout --patch
的具体描述取自 git-scm.com :
-p
--patch
Interactively select hunks in the difference between the <tree-ish> (or the index, if unspecified) and the working tree. The chosen hunks are then applied in reverse to the working tree (and if a <tree-ish> was specified, the index).
This means that you can use git checkout -p to selectively discard edits from your current working tree. See the “Interactive Mode” section of git-add[1] to learn how to operate the --patch mode.
Note that this option uses the no overlay mode by default (see also --overlay), and currently doesn’t support overlay mode.
希望这对您有所帮助。
关于git - 将旧分支中的一些特定文件 merge 到我当前的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62508728/