问题描述
我试图用另一个分支的目录替换当前分支中存在的目录。 Eclipse 无法删除原始分支中的文件,而不删除其他分支中的文件。
示例:
Current branch: A
Directory: src
在这个分支 src 中有一个名为 NoLongerrequired.java
Another branch B
Directory: src
在这个分支中,src
没有名为 NoLongerrequired.java
的文件
我在分支 A
上,想删除 A's src
目录并将其替换为 B's src
目录
因此,我选择 src
目录并转到 Eclipse 中的 Replace with Branch Tag or Commit
选项并选择分支 B
。
但是,我仍然在 NoLongerrequired.java
目录中看到 src
。
解决方法
看起来实现的动作类似于 git checkout B -- src/
:B
中列出的文件被检出,但 A
中而不是 B
中跟踪的文件是保持原样。
如果您的机器上安装了 2.25 或更高版本的 git,您可以使用 git restore
从命令行执行此操作:
# use --staged to set B's content in the staging area :
git restore --source B --staged -- src/
# --worktree to set the content on disk :
git restore --source B --worktree -- src/
# a word of warning : if you have uncommitted changes in src/,this may delete
# files which aren't committed in git yet.
# you can combine both options in one call :
git restore --source B --staged --worktree -- src/
git restore
为您执行删除操作。