git:将文件从另一个分支签出到当前分支(不要将 HEAD 切换到另一个分支)

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

git: checkout files from another branch into current branch (don't switch HEAD to the other branch)

gitbranchgit-checkout

提问by Kache

I want to load a different version of the files that exist in another branch into my current branch.

我想将另一个分支中存在的文件的不同版本加载到我当前的分支中。

git help checkoutsays:

git help checkout说:

DESCRIPTION
   Updates files in the working tree to match the version in the index or
   the specified tree. If no paths are given, git checkout will also
   update HEAD to set the specified branch as the current branch.

Is there a way to checkout all those files, but notupdate HEAD?

有没有办法检出所有这些文件,但更新 HEAD?

回答by Kache

checkout by providing the current path, .:

通过提供当前路径结帐,.

git checkout other-branch-name -- .

git checkout other-branch-name -- .

This operation is similar to switching HEAD to another branch without checking out files, but just from the "other direction".

这个操作类似于在不检出文件的情况下将 HEAD 切换到另一个分支,只是从“另一个方向”。

As @??? mentions, this overwrites any uncommitted changes. Remember to either stash or commit them somewhere first if needed.

作为 @???提到,这会覆盖任何未提交的更改。如果需要,请记住先将它们隐藏或提交到某个地方。