因此,我在存储库中做了一些工作,当我准备提交时,我意识到我目前不在任何分支上。

当使用子模块时,这种情况经常发生,我能够解决它,但这个过程很乏味,我一直在想,一定有一种更简单的方法来做到这一点。

是否有一种简单的方法可以在保持更改的同时回到分支上?


当前回答

这帮助了我

git checkout -b newbranch
git checkout master
git merge newbranch
git branch -d newbranch

其他回答

另一种方式

git branch newbranch
git checkout master 
git merge newbranch 
git checkout master

结果是这样的:

Warning: you are leaving 2 commits behind, not connected to
any of your branches:

1e7822f readme
0116b5b returned to clean django

If you want to keep them by creating a new branch, this may be a good time to do so with:
git branch new_branch_name 1e7822f25e376d6a1182bb86a0adf3a774920e1e

让我们开始吧:

git merge 1e7822f25e376d6a1182bb86a0adf3a774920e1e

我发现最好的方法是把我修改过的文件复制到一个单独的文件夹, 然后删除我电脑上现有的存储库文件夹, 然后克隆主存储库并放回文件。

这帮助了我

git checkout -b newbranch
git checkout master
git merge newbranch
git branch -d newbranch

我最近又遇到了这个问题。距离我上次使用子模块已经有一段时间了,在了解了更多关于git的知识后,我意识到只要检查出你想提交的分支就足够了。Git将保留工作树,即使您不隐藏它。

git checkout existing_branch_name

如果你想在一个新的分支上工作,这应该对你有用:

git checkout -b new_branch_name

如果你在工作树中有冲突,签出将失败,但这应该是相当不寻常的,如果发生这种情况,你可以隐藏它,弹出它并解决冲突。

与公认的答案相比,这个答案将节省您执行两个命令的时间,这两个命令执行起来并不需要太长时间。因此,我不会接受这个答案,除非它奇迹般地获得了比目前接受的答案更多的赞(或至少接近)。