且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-27 23:52:34

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

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

git checkout otherbranch myfile.txt

通用公式:

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

一些笔记(来自评论):

Some notes (from comments):

  • 使用提交哈希,您可以从任何提交中提取文件
  • 这适用于文件和目录
  • 覆盖文件 myfile.txtmydir
  • 通配符不起作用,但相对路径起作用
  • 可以指定多个路径

另一种选择:

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