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

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

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


当前回答

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 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 branch newbranch
git checkout master 
git merge newbranch 

出现这种情况的一种方法是从远程分支执行rebase之后。在这种情况下,新的提交由HEAD指向,但master并不指向它们——它指向在重新基于另一个分支之前它所在的位置。

你可以这样做,把它托付给你的新主人:

git branch -f master HEAD
git checkout master

这将强制将master更新为指向HEAD(而不将您放在master上),然后切换到master。