我有两个完全合并在一起的分支。

然而,在合并完成后,我意识到一个文件已经被合并弄乱了(别人做了一个自动格式化,啊),在另一个分支中更改到新版本会更容易,然后在把它带入我的分支后重新插入我的一行更改。

Git中最简单的方法是什么呢?


当前回答

确保您所在的分支机构需要文件的副本。 例如:我想要子分支文件在master,所以你需要签出或应该在master git签出master 现在检出您想要从子分支到主分支的特定文件, Git checkout sub_branch file_path/my_file.ext 这里的sub_branch指的是你要复制的文件后面跟着文件名。

其他回答

从你想要文件结束的分支运行这个:

git checkout otherbranch myfile.txt

一般公式:

git checkout <commit_hash> <relative_path_to_file_or_dir>
git checkout <remote_name>/<branch_name> <file_or_dir>

一些注释(来自评论):

使用提交散列,可以从任何提交中提取文件 这适用于文件和目录 覆盖myfile.txt和mydir文件 通配符不起作用,但相对路径可以 可指定多条路径

另一个:

git show commit_id:path/to/file > path/to/file

使用checkout命令:

  git diff --stat "$branch"
  git checkout --merge "$branch" "$file"
  git diff --stat "$branch"

请注意,在接受的答案中,第一个选项将从另一个分支中分期整个文件(如git add…已经执行),而第二个选项只会导致复制文件,而不会执行更改(就好像您刚刚手动编辑了文件,并且有突出的差异一样)。

Git从另一个分支复制文件而不登台

阶段性的更改(例如git添加文件名):

$ git checkout directory/somefile.php feature-B

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   directory/somefile.php

未完成的更改(未分期或已提交):

$ git show feature-B:directory/somefile.php > directory/somefile.php

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   directory/somefile.php

no changes added to commit (use "git add" and/or "git commit -a")

按照madlep的回答,您还可以使用目录blob从另一个分支复制一个目录。

git checkout other-branch app/**

至于OP的问题,如果你只改变了一个文件在那里,这将工作良好。

确保您所在的分支机构需要文件的副本。 例如:我想要子分支文件在master,所以你需要签出或应该在master git签出master 现在检出您想要从子分支到主分支的特定文件, Git checkout sub_branch file_path/my_file.ext 这里的sub_branch指的是你要复制的文件后面跟着文件名。