git 如何仅挑选一个文件的更改,而不是整个提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16068186/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:58:46  来源:igfitidea点击:

How to cherry pick only changes for only one file, not the whole commit

git

提问by ashim

I need to apply changes introduced in one branch to another branch. I can use cherry pick to do that. However, in my case I want to apply changes which are relevant only for one file, I don't need to cherry pick whole commit. How to do that?

我需要将一个分支中引入的更改应用到另一个分支。我可以用樱桃来做到这一点。但是,在我的情况下,我想应用仅与一个文件相关的更改,我不需要挑选整个提交。怎么做?

回答by poke

You have different options based on what you want to achieve:

根据您想要实现的目标,您有不同的选择:

If you want the contents of the file to be the same as on the target branch, you can use git checkout <branch> -- <filename>. This will however not “cherry-pick” the changes that happened in a single commit, but just take the resulting state of said file. So if you added a line in a commit, but previous commits changed more, and you only want to add that line without those other changes, then a checkout is not what you want.

如果您希望文件的内容与目标分支上的内容相同,则可以使用git checkout <branch> -- <filename>. 然而,这不会“挑选”在单个提交中发生的更改,而只是获取所述文件的结果状态。因此,如果您在提交中添加了一行,但之前的提交更改了更多,并且您只想添加该行而不进行其他更改,那么结帐不是您想要的。

Otherwise if you want to apply the patch introduced in a commit to only a single file, you have multiple options. You could run git cherry-pick -n, i.e. without committing it, edit the commit (for example reset all files using git reset -- .and only add the file you actually want to change using git add <filename>). Or you could create the diff for the file and apply the diff then:

否则,如果您只想将提交中引入的补丁应用于单个文件,则有多种选择。您可以运行git cherry-pick -n,即不提交,编辑提交(例如使用重置所有文件git reset -- .并仅添加您实际想要更改的文件git add <filename>)。或者您可以为文件创建差异并应用差异然后:

git diff <branch>^..<branch> -- <filename> | git apply

回答by Sailesh

Create a patch fileand apply it.

创建patch file并应用它。

git diff branchname -- filename > patchfile
git apply patchfile

EDIT:

编辑:

Since you need to take the changes from a commit, create the patch like this:

由于您需要从提交中获取更改,因此创建补丁如下:

git show sha1 -- filename > patchfile

回答by 0x90

Another handy thing to do is get the patch locally and then use:

另一件方便的事情是在本地获取补丁,然后使用:

git checkout {<name_of_branch>, commit's SHA} <path to the file> 

That's not a cherry-picking though.

不过,这不是樱桃采摘。

回答by Raghotham S

Git has everything ready :)

Git 已准备好一切:)

Just use git checkout <sha> <path-to-file>

只需使用 git checkout <sha> <path-to-file>

回答by Ismail Iqbal

git checkout the_branch_with_the_change the/path/to/the/file/you/want.extension

this works if you want a single file from another branch to be copied to the current working branch

如果您希望将另一个分支中的单个文件复制到当前工作分支,则此方法有效

回答by Alex

What I tend to do is to use git ls-tree

我倾向于做的是使用 git ls-tree

just do:

做就是了:

git ls-tree -r <commit-id> |grep 'your filename'
# there will be output that shows a SHA1 of your file
git show ${SHA1 of your file} > 'your filename'

This will print all filenames matching your grep and gives SHA1 keys of the files in the commit.

这将打印与您的 grep 匹配的所有文件名,并提供提交中文件的 SHA1 密钥。

Git show can be used on files outside of your branch as well. using >from your shell to pipe the output into a file gives you the result you are looking for.

Git show 也可以用于分支之外的文件。使用>从您的外壳将输出通过管道传输到文件中,可以为您提供所需的结果。

回答by Ruslan Osipov

This is what are you looking for:

这就是你要找的:

git checkout target-branch sha1 path/to/file

sha1 is optional

sha1 是可选的

回答by arashb31

You can try doing this:

你可以尝试这样做:

git show COMMIT_ID -- path/to/specific-file | git apply

For example:

例如:

git show 3feaf20 -- app/views/home/index.html | git apply

回答by Venkata

git reset HEAD~1

moves the files into pre-commit stage

将文件移动到预提交阶段

git stash

removes from memory

从记忆中删除

Now your branch is pretty clean (reverted to previous commit)

现在你的分支非常干净(恢复到以前的提交)