简体   繁体   English

吉特 如何在错误的分支中保存更改

[英]Git. How to save changes in wrong branch

I have 2 branches in my git project( Let it be branch A and B). 我的git项目中有2个分支(让它成为分支A和B)。 I made some changes in branch A, but I need it in B, not A. How I can switch to other branch? 我在分支A中进行了一些更改,但我在B中而不是在A中进行了更改。如何切换到其他分支? When I try "git checkout B" git says that I made some changes and cant switch without commit. 当我尝试“ git checkout B”时,git表示我进行了一些更改,并且没有提交就无法切换。 It is not a good way to save files manually, then remove them, then change branch and paste modified files. 手动保存文件,然后删除它们,然后更改分支并粘贴修改后的文件,不是一种好方法。

It sounds like you started working in branch A , made some changes, but then realized that you really should have been working in branch B . 听起来您开始在分支A工作,做了一些更改,但随后意识到您确实应该在分支B工作。 Don't worry, git stash will come to the rescue to save you. 别担心, git stash会来拯救您。

Just type git stash from the Bash prompt and the changes from your current working directory will be stashed away. 只需在Bash提示符下键入git stash ,即可隐藏当前工作目录中的更改。 Note that Git also stashes your current stage, but I will assume that you have not yet staged anything. 请注意,Git还会隐藏您的当前舞台,但我将假定您尚未上演任何事情。

Then, just switch to the B branch as you normally would: 然后,只需像通常那样切换到B分支:

git checkout B

And apply the stash: 并应用隐藏:

git stash apply

Now if you type git status , you should see all the changes you made in the A branch. 现在,如果您输入git status ,则应该看到在A分支中所做的所有更改。

If you then also want to commit this work, then git add the appropriate files and do a git commit into the B branch. 如果您随后还想提交此工作,则git add适当的文件,并在B分支中执行git commit

Note that there is another option here involving making a format commit in the A branch, then moving it later to the B branch, but that solution is more complex, and I would probably use git stash here. 请注意,这里还有另一种选择,包括在A分支中进行格式提交,然后稍后将其移至B分支,但是该解决方案更为复杂,在这里我可能会使用git stash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM