Git:“当前不在任何分支上。”有没有一种简单的方法可以在保留更改的同时返回分支?

发布于 2024-10-13 03:14:36 字数 148 浏览 11 评论 0原文

因此,我已经在存储库中完成了一些工作,当我即将提交时,我意识到我当前不在任何分支上。

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

有没有一种简单的方法可以在保留更改的同时返回分支?

So I've done some work in the repository and when I'm about to commit I realize that I'm not currently on any branch.

This happens a lot when working with submodules and I am able to solve it, but the process is tedious and I've been thinking that there must be an easier way to do this.

Is there an easy way to get back on a branch, while keeping the changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

孤者何惧 2024-10-20 03:14:36

如果您尚未承诺:

git stash
git checkout some-branch
git stash pop

如果您已承诺并且此后没有更改任何内容:

git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}

如果您已承诺并完成了额外工作:

git stash
git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}
git stash pop

If you have not committed:

git stash
git checkout some-branch
git stash pop

If you have committed and have not changed anything since:

git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}

If you have committed and then done extra work:

git stash
git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}
git stash pop
花开柳相依 2024-10-20 03:14:36

这对我有帮助

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

this helped me

git checkout -b newbranch
git checkout master
git merge newbranch
git branch -d newbranch
假情假意假温柔 2024-10-20 03:14:36
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 master

That's result something like this:

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

So, let's do it:

git merge 1e7822f25e376d6a1182bb86a0adf3a774920e1e
紫罗兰の梦幻 2024-10-20 03:14:36

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

您可以通过以下方式提交您的新 master

git branch -f master HEAD
git checkout master

这会强制更新 master 以指向 HEAD (而不会将您置于 master< /code>) 然后切换到 master

One way to end up in this situation is after doing a rebase from a remote branch. In this case, the new commits are pointed to by HEAD but master does not point to them -- it's pointing to wherever it was before you rebased the other branch.

You can make this commit your new master by doing:

git branch -f master HEAD
git checkout master

This forcibly updates master to point to HEAD (without putting you on master) then switches to master.

飘过的浮云 2024-10-20 03:14:36

这里另留一条路

git branch newbranch
git checkout master 
git merge newbranch 

Leaving another way here

git branch newbranch
git checkout master 
git merge newbranch 
辞慾 2024-10-20 03:14:36

或者,您可以设置子模块,以便您签出分支,而不是处于默认的分离头状态。

编辑添加:

一种方法是在使用 -b 标志添加子模块时签出子模块的特定分支:

git submodule add -b master <remote-repo> <path-to-add-it-to>

另一种方法是进入子模块目录并签出它

git checkout master

Alternatively, you could setup your submodules so that rather than being in their default detached head state you check out a branch.

Edited to add:

One way is to checkout a particular branch of the submodule when you add it with the -b flag:

git submodule add -b master <remote-repo> <path-to-add-it-to>

Another way is to just go into the submodule directory and just check it out

git checkout master
猫卆 2024-10-20 03:14:36

我最近又遇到了这个问题。自从我上次使用子模块并且了解了有关 git 的更多信息以来已经有一段时间了,我意识到只需检查您想要提交的分支就足够了。即使您不隐藏工作树,Git 也会保留它。

git checkout existing_branch_name

如果您想在新分支上工作,这应该适合您:

git checkout -b new_branch_name

如果工作树中存在冲突,则签出将会失败,但这应该是很不寻常的,如果发生这种情况,您可以将其隐藏,弹出并解决冲突。

与接受的答案相比,这个答案将为您节省执行两个命令的时间,无论如何,这两个命令实际上不会花费那么长时间来执行。因此,我不会接受这个答案,除非它奇迹般地获得比当前接受的答案更多的赞成票(或至少接近)。

I recently ran into this problem again. It's been a while since I last worked with submodules and having learned more about git I realized that simply checking out the branch you want to commit on is sufficient. Git will keep the working tree even if you don't stash it.

git checkout existing_branch_name

If you want to work on a new branch this should work for you:

git checkout -b new_branch_name

The checkout will fail if you have conflicts in the working tree, but that should be quite unusual and if it happens you can just stash it, pop it and resolve the conflict.

Compared to the accepted answer, this answer will save you the execution of two commands, that don't really take that long to execute anyway. Therefore I will not accept this answer, unless it miraculously gets more upvotes (or at least close) than the currently accepted answer.

不爱素颜 2024-10-20 03:14:36

以下方法可能有效:

git rebase HEAD master
git checkout master

这会将您当前的 HEAD 更改基于 master 之上。然后就可以切换分支了。


另一种方法是首先签出分支:

git checkout master

然后 Git 应该显示分离提交的 SHA1,然后您可以挑选它们,例如

git cherry-pick YOURSHA1

或者您也可以合并最新的:

git merge YOURSHA1

查看来自不同分支的所有提交(以确保您已经有了),运行:git reflog

The following method may work:

git rebase HEAD master
git checkout master

This will rebase your current HEAD changes on top of the master. Then you can switch the branch.


Alternative way is to checkout the branch first:

git checkout master

Then Git should display SHA1 of your detached commits, then you can cherry pick them, e.g.

git cherry-pick YOURSHA1

Or you can also merge the latest one:

git merge YOURSHA1

To see all of your commits from different branches (to make sure you've them), run: git reflog.

ま昔日黯然 2024-10-20 03:14:36

我知道我在 2012 年告诉 babay,我认为有人不太可能没有意识到他们不在分支上并提交。这件事就发生在我身上,所以我想我必须承认我错了,但考虑到直到 2016 年这件事才发生在我身上,你可能会说这实际上不太可能。

无论如何,在我看来,创建一个新分支是矫枉过正的。您所要做的就是:

git checkout some-branch
git merge commit-sha

如果您在检查其他分支之前没有复制 commit-sha,您可以通过运行以下命令轻松找到它:

git reflog

I know I told babay in 2012 that I thought it was unlikely that someone wouldn't realize that they weren't on a branch and commit. This just happened to me, so I guess I have to admit that I was wrong, but considering that it took until 2016 for this to happen to me, you could argue that it is in fact unlikely.

Anyway, creating a new branch is overkill in my opinion. All you have to do is:

git checkout some-branch
git merge commit-sha

If you didn't copy the commit-sha before checking out the other branch, you can easily find it by running:

git reflog
青衫儰鉨ミ守葔 2024-10-20 03:14:36

我发现的最好方法是将已更改的文件复制到单独的文件夹中,
然后删除我计算机上当前的存储库文件夹,
然后克隆主存储库并将文件放回去。

The best way that I found was to copy the files that I have made changes to to a separate folder,
then delete the repository folder I currently have on my computer,
then clone the main repository and put the files back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文