如何将单个文件的版本从一个 git 分支复制到另一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I copy a version of a single file from one git branch to another?
提问by madlep
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 中最简单的方法是什么?
回答by madlep
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
andmydir
- Wildcards don't work, but relative paths do
- Multiple paths can be specified
- 使用提交哈希,您可以从任何提交中提取文件
- 这适用于文件和目录
- 覆盖文件
myfile.txt
并mydir
- 通配符不起作用,但相对路径起作用
- 可以指定多个路径
an alternative:
替代:
git show commit_id:path/to/file > path/to/file
回答by lkraav
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
回答by RzR
What about using checkout command :
使用 checkout 命令怎么样:
git diff --stat "$branch"
git checkout --merge "$branch" "$file"
git diff --stat "$branch"
回答by Mohideen bin Mohammed
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)现在将您想要的特定文件从子分支单独签出到主分支,
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
意思是你有那个文件,然后是你需要复制的文件名。
回答by 6ft Dan
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 ^_^
至于操作员的问题,如果您只更改了其中的一个文件,这将正常工作^_^
回答by Mariusz Pawelski
I would use git restore
(available since git 2.23)
我会使用git restore
(自 git 2.23 起可用)
git restore --source otherbranch path/to/myfile.txt
git restore --source otherbranch path/to/myfile.txt
Why it is better than other options?
为什么它比其他选择更好?
git checkout otherbranch -- path/to/myfile.txt
- It copy file to working directory but also to staging area (similar effect as if you would copy this file manually and executed git add
on it). git restore
doesn't touch staging area (unless told it to by --staged
option).
git checkout otherbranch -- path/to/myfile.txt
- 它将文件复制到工作目录和暂存区(类似于手动复制此文件并git add
在其上执行的效果)。git restore
不接触暂存区(除非通过--staged
选项告诉它)。
git show otherbranch:path/to/myfile.txt > path/to/myfile.txt
uses standard shell redirection. If you use Powershell then there might be problem with text enconding or you could get broken file if it's binary. With git restore
changing files is done all by git
executable.
git show otherbranch:path/to/myfile.txt > path/to/myfile.txt
使用标准外壳重定向。如果您使用 Powershell,那么文本编码可能会出现问题,或者如果它是binary ,您可能会得到损坏的文件。随着git restore
不断变化的文件被完成的所有git
可执行文件。
Another advantage is that you can restore whole folder with:
另一个优点是您可以使用以下方法恢复整个文件夹:
git restore --source otherbranch path/to
git restore --source otherbranch path/to
or with git restore --overlay --source otherbranch path/to
if you want to avoid deleting files. For example if there is less files on otherbranch
than in current working directory (and these files are tracked) without --overlay
option git restore
will delete them. But this is good default bahaviour, you most likely want the state of directory to be the same like in otherbranch
, not "the same but with additional files in my current branch"
或者git restore --overlay --source otherbranch path/to
如果你想避免删除文件。例如,如果文件otherbranch
比当前工作目录中的文件少(并且这些文件被跟踪),则没有--overlay
选项git restore
将删除它们。但这是很好的默认行为,您很可能希望目录的状态与 中的相同otherbranch
,而不是“相同但在我当前的分支中有其他文件”
回答by user151841
Please note that in the accepted answer, the first option stagesthe entire file from the other branch (like git add ...
had been performed), and that the second option just results in copying the file, but doesn't stage the changes (as if you had just edited the file manually and had outstanding differences).
请注意,在接受的答案中,第一个选项会暂存来自另一个分支的整个文件(就像git add ...
已执行的那样),而第二个选项只会导致复制文件,但不会暂存更改(就像你有只是手动编辑了文件并且有显着差异)。
Git copy file from another branch without staging it
Changes staged(e.g. git add filename)
:
阶段性变化(例如git add filename)
:
$ 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
Changes outstanding(not staged or committed):
未完成的更改(未上演或提交):
$ 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")