吉特如何在错误的分支中保存更改 [英] Git. How to save changes in wrong branch

查看:58
本文介绍了吉特如何在错误的分支中保存更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的git项目中有2个分支(假设它是分支A和B).我在分支A中进行了一些更改,但我在B中而不是在A中进行了更改.如何切换到其他分支?当我尝试"git checkout B"时,git表示我进行了一些更改并且无法在没有提交的情况下进行切换.手动保存文件,然后删除它们,然后更改分支并粘贴修改后的文件,不是一种好方法.

I have 2 branches in my git project( Let it be branch A and B). I made some changes in branch A, but I need it in B, not A. How I can switch to other branch? When I try "git checkout B" git says that I made some changes and cant switch without commit. It is not a good way to save files manually, then remove them, then change branch and paste modified files.

推荐答案

听起来您开始在分支A中工作,进行了一些更改,但随后意识到您确实应该在分支B中工作.不用担心,git stash会为您救救.

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. Don't worry, git stash will come to the rescue to save you.

只需在Bash提示符下键入git stash,就会隐藏当前工作目录中的更改.请注意,Git还会隐藏您的当前舞台,但我将假定您尚未上演任何事情.

Just type git stash from the Bash prompt and the changes from your current working directory will be stashed away. Note that Git also stashes your current stage, but I will assume that you have not yet staged anything.

然后,像往常一样切换到B分支:

Then, just switch to the B branch as you normally would:

git checkout B

并应用隐藏:

git stash apply

现在,如果键入git status,则应该在A分支中看到所做的所有更改.

Now if you type git status, you should see all the changes you made in the A branch.

如果您还想提交这项工作,请git add适当的文件,并在B分支中做一个git commit.

If you then also want to commit this work, then git add the appropriate files and do a git commit into the B branch.

请注意,这里还有另一种选择,包括在A分支中进行格式提交,然后稍后将其移至B分支,但是该解决方案更为复杂,我可能会在这里使用git stash.

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.

这篇关于吉特如何在错误的分支中保存更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆