简体   繁体   English

git head 与 master 分离

[英]git head detached from master

Its really confusing i trying push my all commits to production.我试图将我的所有承诺推向生产,这真的很令人困惑。 But when i push to server 8840f9e pushing this.但是当我推送到服务器 8840f9e 时。

My goal is push all my commits to production.我的目标是将我所有的承诺推向生产。

commit d06a8c7(HEAD)

commit e1467b4

commit 0ed15e7

commit 5886ab9

commit 0f8ee01

commit 8840f9e (production/master, master)

tl;dr tl;博士
You could try git branch tempbranch d06a8c7d9a && git checkout master && git merge tempbranch to get the commit onto your master branch, and then do the push to production.您可以尝试git branch tempbranch d06a8c7d9a && git checkout master && git merge tempbranch将提交提交到您的主分支,然后推送到生产。


Usually, git commits are on some branch.通常,git commits 在某个分支上。 Maybe on multiple, but that's not relevant here.也许是多个,但这在这里无关紧要。
When you usually work, you are supposed to have some branch checked out - yours is called master .当您通常工作时,您应该检查一些分支 - 您的分支称为master
But it can happen that you leave the branch, and create commits of your new changes without telling git on which branch they belong.但是可能会发生您离开分支并创建新更改的提交而没有告诉 git 它们属于哪个分支的情况。 In that case, git will enter something called a "detached head state".在这种情况下,git 将进入一种称为“分离头状态”的状态。

For example:例如:

git init
touch file.txt
git add file.txt
git commit -m "initial commit"

Creates a file and commits it.创建一个文件并提交它。 Let us modify the file and commit again, so we have two commits.让我们修改文件并再次提交,所以我们有两次提交。 If you now explicitly git checkout HEAD~ , ie if you check out the first commit, you are no longer on the master branch.如果你现在明确地git checkout HEAD~ ,即如果你签出第一次提交,你就不再在master分支上。 This is just one example of how to get to that state.这只是如何达到该状态的一个示例。

If you now create a new commit, git will warn you:如果你现在创建一个新的提交,git 会警告你:

 $ git checkout HEAD^ Note: checking out 'HEAD^'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at 24ebade initial commit

To get the new commits back to some branch, you can do what it recommends you.要将新提交返回到某个分支,您可以按照它的建议进行操作。
Create a new branch which points to your latest commit, using git checkout -b tempBranch for example.创建一个指向您最新提交的新分支,例如使用git checkout -b tempBranch

It is possible, that you have some commits on master , that contradict your changes that happened in this detached HEAD state which is now on tempBranch .有可能您在master上有一些提交,这与您在这个分离的 HEAD 状态中发生的更改相矛盾,该状态现在位于tempBranch Because of that, you might need to resolve merge conflicts.因此,您可能需要解决合并冲突。

git checkout master
git merge tempbranch

makes sure both your commit histories are merged together into your master branch.确保您的两个提交历史都合并到您的master分支中。 After resolving merge conflicts, you are back at a state which you're used to, and can push as usual.解决合并冲突后,您又回到了习惯的状态,可以像往常一样推送。
To clean up, since you do not need the temporary branch tempBranch anymore, you can delete it using git branch -d tempbBranch .要清理,由于您不再需要临时分支tempBranch ,您可以使用git branch -d tempbBranch将其删除。

  • Create a new temporary branch , say tempcommit创建一个新的临时分支,比如 tempcommit
  • Checkout new branch结帐新分支
  • add commit to new branch添加提交到新分支
  • merge new branch to master将新分支合并到 master

All in one command is like this一个命令是这样的

git branch tempcommit <commit id> && git checkout master && git merge tempbranch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM