如何将单个文件的版本从一个git分支复制到另一个git分支?

时间:2021-02-18 06:59:24

I've got two branches that are fully merged together.

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

However, after the merge is done, I realise that one file has been messed up by the merge (someone else did an auto-format, gah), and it would just be easier to change to the new version in the other branch, and then re-insert my one line change after bringing it over into my branch.

但是,在完成合并之后,我意识到一个文件已经被合并搞砸了(其他人做了自动格式化,gah),并且更容易在另一个分支中更改为新版本,并且然后重新插入我的一行更改后将其带入我的分支。

So what's the easiest way in git to do this?

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

5 个解决方案

#1


1390  

Run this from the branch where you want the file to end up:

从您希望文件结束的分支运行此命令:

git checkout otherbranch myfile.txt

General formulas:

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

Some notes (from comments):

一些说明(来自评论):

  • Using the commit hash you can pull files from any commit
  • 使用提交哈希,您可以从任何提交中提取文件

  • This works for files and directories
  • 这适用于文件和目录

  • overwrites the file myfile.txt and mydir
  • 覆盖文件myfile.txt和mydir

  • Wildcards don't work, but relative paths do
  • 通配符不起作用,但相对路径可行

  • Multiple paths can be specified
  • 可以指定多个路径

an alternative:

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

#2


66  

I ended up at this question on a similar search. In my case I was looking to extract a file from another branch into current working directory that was different from the file's original location. Answer:

我最后在这个类似搜索的问题上结束了。在我的情况下,我希望从另一个分支中提取一个文件到当前工作目录,该目录与文件的原始位置不同。回答:

git show TREEISH:path/to/file >path/to/local/file

#3


43  

What about using checkout command :

使用checkout命令怎么样:

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

#4


12  

Following madlep's answer you can also just copy one directory from another branch with the directory blob.

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

git checkout other-branch app/**

As to the op's question if you've only changed one file in there this will work fine ^_^

至于op的问题,如果你只改变了一个文件,那么这将很好地工作^ _ ^

#5


7  

1) Ensure you're in branch where you need a copy of the file. for eg: i want sub branch file in master so you need to checkout or should be in master git checkout master

1)确保您在需要文件副本的分支机构中。例如:我想在master中使用子分支文件,因此你需要签出或者应该在master git checkout master中

2) Now checkout specific file alone you want from sub branch into master,

2)现在单独检查你想要从子分支到master的特定文件,

git checkout sub_branch file_path/my_file.ext

here sub_branch means where you have that file followed by filename you need to copy.

这里sub_branch表示你有文件后跟你需要复制的文件名。

#1


1390  

Run this from the branch where you want the file to end up:

从您希望文件结束的分支运行此命令:

git checkout otherbranch myfile.txt

General formulas:

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

Some notes (from comments):

一些说明(来自评论):

  • Using the commit hash you can pull files from any commit
  • 使用提交哈希,您可以从任何提交中提取文件

  • This works for files and directories
  • 这适用于文件和目录

  • overwrites the file myfile.txt and mydir
  • 覆盖文件myfile.txt和mydir

  • Wildcards don't work, but relative paths do
  • 通配符不起作用,但相对路径可行

  • Multiple paths can be specified
  • 可以指定多个路径

an alternative:

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

#2


66  

I ended up at this question on a similar search. In my case I was looking to extract a file from another branch into current working directory that was different from the file's original location. Answer:

我最后在这个类似搜索的问题上结束了。在我的情况下,我希望从另一个分支中提取一个文件到当前工作目录,该目录与文件的原始位置不同。回答:

git show TREEISH:path/to/file >path/to/local/file

#3


43  

What about using checkout command :

使用checkout命令怎么样:

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

#4


12  

Following madlep's answer you can also just copy one directory from another branch with the directory blob.

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

git checkout other-branch app/**

As to the op's question if you've only changed one file in there this will work fine ^_^

至于op的问题,如果你只改变了一个文件,那么这将很好地工作^ _ ^

#5


7  

1) Ensure you're in branch where you need a copy of the file. for eg: i want sub branch file in master so you need to checkout or should be in master git checkout master

1)确保您在需要文件副本的分支机构中。例如:我想在master中使用子分支文件,因此你需要签出或者应该在master git checkout master中

2) Now checkout specific file alone you want from sub branch into master,

2)现在单独检查你想要从子分支到master的特定文件,

git checkout sub_branch file_path/my_file.ext

here sub_branch means where you have that file followed by filename you need to copy.

这里sub_branch表示你有文件后跟你需要复制的文件名。