我抱着超然的态度,做了一些改变。我想用 Git 将这些更改推送到这个独立的头部。我不希望我的更改进入开发分支,当然也不想进入主分支。我正在与另一个人一起处理一个文件。
分支示例
develop
master
*(HEAD detached at origin/49792_testMocha)
如何在不影响开发或掌握的情况下推进头脑?
如果您处于独立状态并且想要推送到远程分支
git push origin HEAD:name-of-your-branch
否则你可以创建一个新分支并推送到它(它将自动创建)
git branch new-branch-name
git push -u origin new-branch-name
使用
git checkout -b BRANCH_NAME
创建一个新分支
然后将新分支推送到远程:
git push origin BRANCH_NAME
虽然这里的所有答案都回答了最初的问题(如何从独立的头部推送而不影响其他分支),但都建议创建一个新分支。
以下是如何推送到新的远程分支而不创建新的本地分支:
git checkout --detach # (or anything else that leaves you with a detached HEAD - guillotine anyone?)
[change stuff & commit]
git push origin HEAD:refs/heads/my-new-branch
将
origin
替换为适当的远程名称(您具有写入权限),并将 my-new-branch
替换为您想要调用新分支的任何名称。
您在
HEAD
上的提交将被推送到名为 my-new-branch
的新分支。 🎉
分离头通常意味着您签出的分支没有最新的提交。所以,基本上你需要将当前分支的 HEAD 调整为最新的提交。
通常有2种方法可以做到。
如果您想使用同一个分支 - 您可以使用:
git push origin HEAD:< remote-branch >
您可以创建一个新分支,将代码推送到该分支(这也会拉取您分离的代码)。
git checkout -b < branch-name > < base-branch >
git commit .
git push
git checkout
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
这将检出指向所需提交的新分支。
此命令将签出给定的提交。
此时您可以创建一个分支并从此时开始工作。
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
#in order to be able to update the code.
git checkout <commit-id>
# create a new branch forked to the given commit
git checkout -b <branch name>
注意:在推送之前创建分支更建议使用 git 2.11 或更低版本来避免段错误!
Git 2.12+(2017 年第一季度)不会出现这种情况请参阅
提交 b10731f(2017 年 1 月 7 日),作者:Kyle Meyer (kyleam
)。
gitster
--合并于 commit b85f79c,2017 年 1 月 18 日)
现在的错误将是:“
branch_get_push
:HEAD 分离时不会出现段错误
git <cmd> @{push}
”位于用于段错误的分离 HEAD 上;它有 已通过消息更正错误。
HEAD does not point to a branch
使用 Git 2.12 或更高版本,您可以将分离的 HEAD 推送到远程分支,如
git checkout -b <branch-name> <commit-hash>
。现在您可以将更改推送到新分支:
git push origin <branch-name>
如果您需要清理剩余提交中的其他分支,请务必运行 git reset --hard <branch-name>
。这里有一篇文章解释了
分支和分离头如何工作。
git push --tags