简体   繁体   English

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

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

I want to load a different version of the files that exist in another branch into my current branch.我想将另一个分支中存在的文件的不同版本加载到我的当前分支中。

git help checkout says: 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 not update HEAD?有没有办法检出所有这些文件,但更新 HEAD?

checkout by providing the current path, .通过提供当前路径结帐, . :

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.如果需要,请记住先将它们隐藏或提交到某个地方。

Similar to @Kache's answer , but using the newer git restore command (requires Git version 2.23 or above):类似于@Kache 的回答,但使用较新的git restore命令(需要 Git 版本 2.23 或更高版本):

git restore --source=<other-branch/tag/commit> <pathspec>
# or
git restore -s <other-branch/tag/commit> <pathspec>

# example: to load all files from branch "other"
git restore -s other .

This new command was introduced to split "checking out a branch" and "checking out files" out of the single git checkout command.引入这个新命令是为了将“签出分支”和“签出文件”从单个git checkout命令中分离出来。 Read more: What is the git restore command .阅读更多:什么是git restore命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM