15

I know how to sync branch with master. I do this so:

git checkout target-branch
git merge master
# resolve conflicts if any and commit
git push

I am trying to figure it out what BitBucket suggests me when automatic sync failed because of conflicts. BitBucket suggests me:

git checkout 326907c583f7
# Note: This will create a detached head!
git merge remotes/origin/master

What's the point to create detached head? I does not make sense to me. Is it bug on Bitbucket side or I am missing something?

stz184
  • 363
  • 3
  • 12
  • Possible duplicate of [Fix a Git detached head?](http://stackoverflow.com/questions/10228760/fix-a-git-detached-head) – René Höhle Jan 11 '16 at 14:11
  • 6
    @Stony, I don't need help with a detached head. I am trying to figure it out why Bitbucket suggests me to create a detached head in order to sync a branch with the master. It clearly does not make sense to me. – stz184 Jan 11 '16 at 14:24
  • How does that commit relate to your branch head? – ams Jan 11 '16 at 15:11
  • 3
    In my experience the given hash in that help message **is** the branch head. I have the same question as the OP. My only guess as to why BB gives these instructions is that using the hash leaves less room for error e.g. checking out local vs. remote. – Paul Bissex May 11 '16 at 13:26
  • Can you add the git output for "git push" command? I'm trying to understand why git offers you to checkout to particular sha1. – Vitalliuss Sep 27 '16 at 15:45
  • Agree this is maddening why does bitbucket tell you to checkout a hash then not tell you how to push that change back to the branch? Ignore their instructions and "git checkout target-branch" instead? – jamshid Jan 15 '21 at 04:33

2 Answers2

2

In case BitBucket can't sync branches for you, that you have some conflicts with your branch changes and the destination branch.

You can use git rebase to sync your local branch and later, push changes.

Assuming that you want to sync with the master branch, you can do these next steps:

First, update your local master branch.

git fetch origin master:master

Then, sync changes of your branch with the master branch

git rebase master

If you have conflicts, you will receive a message with more information about them. Make the necessary changes and use:

git rebase --continue

There you can receive a new conflict, in that case, repeat the previous operation. If not, you can push changes to the remote repository.

Cause you have changes the history with the git rebase command, you will need to force your push.

git push -f
Nurio Fernández
  • 518
  • 5
  • 22
0

Well, I for one think that the message about "creating a detached HEAD" is a little misleading because you can't actually create it. What happens is that git can checkout branches or revisions (let's forget about tags to not make things more complicated). If you choose to checkout a local branch, then git knows that when you commit it has to create the new revision and then make the local branch point to this revision that was just created. But if you choose to checkout a "revision" by its ID instead as provided in the example you posted (or a remote branch, for that matter, it's the same situation from the point of view of your local repository), then there is no local branch that is pointing to the revision (that's why it's called detached HEAD). For git it makes no difference. It's just a revision that you asked to checkout either by asking for a local branch, a remote branch or a revision. But for you it makes quite a difference because if you are working on a detached HEAD then, if you choose to commit, git won't have a local branch pointing to it. It will simply create the revision, make HEAD point to it and that's it. If you at this moment, for example, checked out to a different branch or revision and you had no local branch pointing to the revision you just created in a detached HEAD state, then you lost the revision (well... you can always use git reflog to get it back but you get the point, right?). That's why git warns you when you start working on a detached HEAD:

$ git checkout HEAD~2
Note: checking out 'HEAD~2'.

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>

Notice how it tells you that you can create a local branch at any point by running git checkout -b new-branch-name

eftshift0
  • 26,375
  • 3
  • 36
  • 60