需要:更改后更改Git分支

Jac*_*rts 0 git visual-studio-code

有时,在使用Visual Studio Code进行代码更改之前,我忘记更改分支。我尝试在进行更改后更改分支,但收到类似“ Git:对以下文件的本地更改将被签出覆盖”的响应。如何将更改提交到需要使用的分支?

McG*_*lin 6

git stash
git checkout <existing branch>
git stash apply
Run Code Online (Sandbox Code Playgroud)

这将提取您在原始分支上所做的更改,并将其放入您创建的新分支中。该-b标志将立即将您切换到新分支。

编辑:如下所述,git stash/apply在创建新分支(使用git checkout -b <new branch>)时没有必要。但是,如果您尝试不隐藏而签出现有分支,则会收到以下消息:

error: Your local changes to the following files would be overwritten by checkout:
    README.md
Please, commit your changes or stash them before you can switch branches.
Aborting
Run Code Online (Sandbox Code Playgroud)

存放和应用时请小心,因为如果目标分支中存在类似的编辑,则可能会导致合并冲突。

  • -b选项意味着*创建*一个新的分支。没有其他参数,它将在`HEAD`处创建分支,并且不需要隐藏。看来OP已经有一个分支,所以他需要stash-and-no-b版本。 (2认同)