简体   繁体   English

将未完成的更改从主服务器移到分支

[英]Move uncomitted changes from master to branch

I have started making some changes recently to my master branch to add featureX. 我最近开始对我的master分支进行一些更改以添加featureX。 I have created 1 new file and edited 6 others. 我创建了1个新文件,并编辑了6个其他文件。 I have not committed any of the changes. 我尚未进行任何更改。 For the time being, I would like to work on featureX on a separate branch and leave the master untouched until I am sure featureX works correctly. 暂时,我想在一个单独的分支上使用featureX,并保持原版不变,直到我确定featureX可以正常工作为止。

I know I should have started a new branch before I started making these changes for featureX, but is there anyway to get the uncommitted changes I've made to the master branch into a featureX branch and restore the master to its pre featureX state? 我知道在开始对featureX进行这些更改之前应该已经开始了一个新的分支,但是是否有将我对master分支进行的未提交的更改转换为FeatureX分支并将master还原到其featureX之前状态的方法?

Creating a new branch ( git checkout -b your_new_feature ) should do the job - otherwise, you could try this: 创建一个新分支( git checkout -b your_new_feature )可以完成这项工作-否则,您可以尝试以下操作:

git stash
git checkout -b your_new_feature
git stash apply

What you need is to stash away the changes, then apply it somewhere else: 您需要stash所有更改,然后将其应用到其他地方:

git stash
git checkout -b other_branch
git stash apply

Read more on git stash here . 此处阅读有关git stash更多信息


As I just learned (and tried), you can actually just checkout to your branch and git will remember what files were changed. 正如我刚刚了解(并尝试)的那样,实际上您可以签出您的分支,而git会记住更改了哪些文件。 So 所以

git checkout -b other_branch

should be enough. 应该足够了。


A note: It is important to also learn git stash . 注意:学习git stash也很重要。 In this case, there is no problem. 在这种情况下,没有问题。 However, if the other_branch already exists, it may have its own changes and therefore there could be merge conflicts. 但是,如果other_branch已经存在,则可能会有其自己的更改,因此可能存在合并冲突。 In that case simply writing 在这种情况下,只需写

git checkout other_branch

may fail with a message like this: 可能会失败,并显示以下消息:

error: You have local changes to 'whatever'; 错误:您对“任何内容”进行了本地更改; cannot switch branches. 无法切换分支。

In such a case, you should use the method above and after git stash apply , resolve the possible conflicts. 在这种情况下,您应该使用上面的方法,并在git stash apply ,解决可能的冲突。

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

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