Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

git - 如何将单个文件的版本从一个git分支复制到另一个git分支?(How do I copy a version of a single file from one git branch to another?)

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中最简单的方法是什么呢?)

  ask by madlep translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.txtmydir)

  • 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...