如何在 Git 中将一些文件(不是全部)从一个分支移动到另一个分支?

How can I move some files (not all) from one branch to another in Git?

碰巧在我错误地进行代码修复的分支中,我开始进行一些与另一个分支相对应的更改(修改和创建文件)。您如何将一些特定文件从一个分支传递到另一个分支?谢谢!

最简单的方法是 stash 更改,移动到正确的分支,然后应用隐藏的更改。请注意 stash 默认为 push 子命令,因此 git stashgit stash push 是等价的。

git stash [<pathspec>...] # store the changes in the current tree
git switch <correct_branch>  # move to the proper branch (use checkout if git < 2.23)

# apply the changes to the correct branch
git stash apply
git stash drop

您可以将最后两行组合成 git stash pop,唯一的区别是存储会自动删除,如果您出于某种原因需要再次使用它,您将无法访问它。