结帐第二个Git分支而不会丢失本地更改

时间:2023-01-24 15:06:36

I have checked out a particular branch from my master. I created new classes propery files etc. in my local Eclipse and did the testing. I have not added/committed those classes/files to my local git repo. So after this successful prototype testing if I want to switch to another branch, how can I do it without losing local changes.

我从我的主人那里检查了一个特定的分支。我在我的本地Eclipse中创建了新的类文件等,并进行了测试。我没有将这些类/文件添加/提交到我的本地git仓库。因此,在成功进行原型测试之后,如果我想切换到另一个分支,我怎么能这样做而不会丢失本地更改。

Once I check out the second branch my local changes will be overwritten? I am not sure if my changes will have to be discarded before the checkout of second branch to be possible.

一旦我签出第二个分支,我的本地更改将被覆盖?我不确定在结束第二个分支之前是否必须丢弃我的更改。

2 个解决方案

#1


0  

Well it is not certain you would lose changes (and in fact if you were to, git checkout would warn you), but it would be best to either stash your changes with git stash or just go ahead and commit your changes before checking out the other branch.

好吧,你不确定你会失去更改(事实上,如果你这样做,git checkout会警告你),但最好是用git stash存储你的更改,或者只是继续并在签出之前提交你的更改其他分支。

#2


1  

Use git stash.

使用git stash。

git-stash - Stash the changes in a dirty working directory away

git-stash - 将更改存储在脏工作目录中

See more in the git docs.

在git文档中查看更多内容。

If you have untracked new files, be sure to use the -u flag, as includes files git hasn't tracked yet.

如果你有未跟踪的新文件,请务必使用-u标志,因为git尚未跟踪包含文件。

Once you're ready to bring your stashed changes back, use git stash pop.

一旦你准备好恢复你的存储,你就可以使用git stash pop。

$ git stash -u
$ git checkout <branch>
$ # do work
$ git checkout <oldbranch>
$ git stash pop

#1


0  

Well it is not certain you would lose changes (and in fact if you were to, git checkout would warn you), but it would be best to either stash your changes with git stash or just go ahead and commit your changes before checking out the other branch.

好吧,你不确定你会失去更改(事实上,如果你这样做,git checkout会警告你),但最好是用git stash存储你的更改,或者只是继续并在签出之前提交你的更改其他分支。

#2


1  

Use git stash.

使用git stash。

git-stash - Stash the changes in a dirty working directory away

git-stash - 将更改存储在脏工作目录中

See more in the git docs.

在git文档中查看更多内容。

If you have untracked new files, be sure to use the -u flag, as includes files git hasn't tracked yet.

如果你有未跟踪的新文件,请务必使用-u标志,因为git尚未跟踪包含文件。

Once you're ready to bring your stashed changes back, use git stash pop.

一旦你准备好恢复你的存储,你就可以使用git stash pop。

$ git stash -u
$ git checkout <branch>
$ # do work
$ git checkout <oldbranch>
$ git stash pop