从分离的头部进行 Git 推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35736116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:58:12  来源:igfitidea点击:

Making a Git push from a detached head

gitgit-push

提问by Winnemucca

I am on a detached head and made some changes. I want to push up these changed to this detached head with Git. I do not want my changes to go onto the develop branch and certainly not on the master branch. I am working on a file with another individual.

我在一个独立的头上并做了一些改变。我想用 Git 将这些更改推到这个分离的头上。我不希望我的更改进入 develop 分支,当然也不在 master 分支上。我正在与另一个人一起处理文件。

Example branches

示例分支

   develop
   master
   *(HEAD detached at origin/49792_testMocha)

How do I push into head without affecting develop or master?

如何在不影响开发或掌握的情况下推入头部?

采纳答案by J. Titus

Create a new branch using git checkout -b BRANCH_NAME

使用创建一个新分支 git checkout -b BRANCH_NAME

Then push the new branch to remote: git push origin BRANCH_NAME

然后将新分支推送到远程: git push origin BRANCH_NAME

回答by Mohamed Salem Lamiri

If you are on a detached head and you want to push to your remote branch

如果你在一个独立的头上并且你想推送到你的远程分支

git push origin HEAD:name-of-your-branch

otherwise you can create a new branch and push to it ( it will be created automatically )

否则你可以创建一个新分支并推送到它(它将自动创建)

git branch new-branch-name
git push -u origin new-branch-name

回答by Matt

While all the answers here sort of answer the original question (how to push from a detached head without affecting other branches) all suggest creating a new branch.

虽然这里的所有答案都回答了最初的问题(如何在不影响其他分支的情况下从分离的头部推动),但都建议创建一个新分支。

Here's how to push to a new remote branch withoutcreating a new local branch:

以下是在创建新本地分支的情况下推送到新远程分支的方法:

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

Replace originwith the appropriate remote name (that you have write access to), and my-new-branchwith whatever you want the new branch to be called.

替换origin为适当的远程名称(您具有写入权限),以及my-new-branch您希望调用新分支的任何内容。

Your commit(s) on HEADwill be pushed to a new branch named my-new-branch.

您的提交HEAD将被推送到名为 的新分支my-new-branch

回答by CodeWizard



git checkout

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

This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point you can create a branch and start to work from this point on.

这将签出指向所需提交的新分支。
此命令将检出给定的提交。
此时您可以创建一个分支并从这一点开始工作。

# 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>

回答by VonC

Note: making a branch beforepushing is all the more recommended that git 2.11 or less used to segfault!

注意:git 2.11 或更低版本用于段错误时,更推荐推送之前创建一个分支!

This won't be the case with Git 2.12+ (Q1 2017)

Git 2.12+(2017 年第一季度)不会出现这种情况

See commit b10731f(07 Jan 2017) by Kyle Meyer (kyleam).
(Merged by Junio C Hamano -- gitster--in commit b85f79c, 18 Jan 2017)

请参阅Kyle Meyer ( )提交的 b10731f(2017 年 1 月 7 日(由Junio C Hamano合并-- --提交 b85f79c,2017 年 1 月 18 日)kyleam
gitster

branch_get_push: do not segfault when HEAD is detached

"git <cmd> @{push}" on a detached HEAD used to segfault; it has been corrected to error out with a message.

branch_get_push: 分离 HEAD 时不要出现段错误

" git <cmd> @{push}" 在用于段错误的分离 HEAD 上;它已被更正为错误消息。

The error now will be:

现在的错误将是:

HEAD does not point to a branch

With Git 2.12 or more, you can then push your detached HEAD to a remote branch, as shown in Matt's answer.

使用 Git 2.12 或更高版本,您可以将分离的 HEAD 推送到远程分支,如Matt回答所示

回答by Nesha Zoric

Create a new branch for that commit and checkout to it: git checkout -b <branch-name> <commit-hash>. Now you can push your changes to the new branch: git push origin <branch-name>

创建该提交一个新的分支,结帐吧:git checkout -b <branch-name> <commit-hash>。现在您可以将更改推送到新分支:git push origin <branch-name>

In case you need to clean up your other branch from leftover commits be sure to run git reset --hard <branch-name>.

如果您需要从剩余的提交中清理其他分支,请务必运行git reset --hard <branch-name>.

Here is an article that explains how branching and detached headworks.

这是一篇解释分支和分离头如何工作的文章。