ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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.

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

@BobbyA 我不这么认为。 1:问题和答案是关于其他的(分支和文件/文件夹名称冲突)。 2:这个问题比较老。
我标记了错误的问题(多个选项卡),立即意识到,并收回了标记。虽然我没有意识到有评论。我被抓住了!道歉:) 现在删除该评论,因为它具有误导性

K
Kache

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

git checkout other-branch-name -- .

此操作类似于switching HEAD to another branch without checking out files,但只是从“其他方向”。

正如@김민준 所提到的,这会覆盖任何未提交的更改。如果需要,请记住先将它们存储或提交到某个地方。


请注意,这将丢弃您所有未提交的工作。事后看来很明显,但被咬得很厉害:'(
这是从您不想保留所有详细信息的功能分支执行合并的最简单方法。 -- . 有点难找,谢谢!
你能解释一下什么——。实际上是在做什么? “。”就在这里 - 没有参数意味着“全部”?
据我所知,Git 使用 -- 作为左侧命令和右侧文件 glob 之间的分隔符。
这对我来说并不完全适用,使用 Windows(也许还有其他差异?)我不得不使用 git checkout <other-branch-name> -- C:\path\to\changes\*
u
user5532169

@Kache's answer 类似,但使用较新的 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 .

引入这个新命令是为了将“签出分支”和“签出文件”从单个 git checkout 命令中分离出来。阅读更多:What is the git restore command