Git从另一个分支复制文件而不暂存

use*_*841 6 git

发现了如何将文件从一个分支批量复制到另一个分支:

编辑请注意,如果您认为这个问题与this重复,请注意这是我在上面链接的问题^^,下面解释了我想要的不同功能。

$ git checkout directory/somefile.php feature-B
Run Code Online (Sandbox Code Playgroud)

但是,此解决方案已经对更改进行了分阶段:

$ 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
Run Code Online (Sandbox Code Playgroud)

但我不想添加所有更改。我想做一个add -p并采取大部分但不是全部的变化。我想要的是这个:

$ 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")
Run Code Online (Sandbox Code Playgroud)

当然,我可以git reset取消暂存更改,但我想第一次就得到我想要的东西,没有第二步。

如何在不暂存的情况下从另一个分支复制文件?

Mar*_*ski 16

您可以使用git 恢复

git restore --source feature-B directory/somefile.php

我没有通过任何选项来说明应该在哪里恢复文件,并且默认它是工作目录(没有索引/暂存区域)。来自文档:

> -W
> --工作树
> -S
> --上演
>
> 指定恢复位置。如果两个选项均未指定,则默认情况下恢复工作树。指定 --staged 将仅恢复索引。指定两者都会恢复两者。

我在这个答案中写了更多关于git restore与其他命令(git checkoutgit show)之间的差异的内容。


eft*_*ft0 7

你可以使用 git show 然后重定向......虽然它不是很优雅

git show feature-B:directory/somefile.php > directory/somefile.php
Run Code Online (Sandbox Code Playgroud)