Head and origin/master not the same? Multiple master?

I have spent so much time trying to figure this out on my own that I think I have made the problem more difficult, so please forgive me if this is a stupid question. I have used Git a bit, but never needed to revert to a prior commit. Here is what I think I have done:

  • moved HEAD back to commit I would like to start from
  • added and commited with message “NEW repair branch” and name this branch “repair-11-8”
  • MAYBE named this branch two names (?) the first being “repair-11-8” and the second “master”? Current head has a green name “master” but can there be 2?
  • tried git push --set-upstream origin master to try to push current head to origin/master, but don’t know if that actually did anything

I definitely subscribe to RTFM, but not really sure what I am trying to do other than have my remote origin/master === HEAD, so not really sure what documentation I’m looking for. Below is a screenshot that might help.

Hello @jasonbunnell and welcome to the community!

There are a few concepts here that I believe will be helpful to understand. Please forgive me if I’m repeating things you already know because I’m also thinking of other people that might read this in the future.

  • In git there are things called “references” or simply “refs” that are pointers to a specific commit
  • Branches are a type of ref
  • HEAD is also a ref
  • HEAD is the commit that your working directory is based on
  • When HEAD is pointing to the same commit as a branch ref, for example master, you are considered to be “on” that branch
  • When you are on a branch and you create a new commit, both HEAD and the branch you are on are moved to point to the new commit
  • There can be an infinite number of branches all pointing to one commit
  • You can only be on one branch at a time

Given the above, your description, and the screenshot, here’s what I suspect happened:

  1. You were on master at commit c4060db when you realized there was a problem
  2. You used git reset to move back to commit 94e5e57
  3. You created a new branch repair-11.8 (which would point at your current commit 94e5e57) but did not switch to that branch, instead remaining on master
  4. You created a new commit 5675ea1, leaving you in the current state as of the screenshot:
    • HEAD and master pointing to 5675ea1
    • repair-11.8 still pointing to 94e5e57
    • origin/master pointing to c4060db

It sounds like what you want is the following:

  1. HEAD and repair-11.8 to point to 5675ea1
  2. master and origin/master to point to c4060db

If that’s true, you can execute the following commands, assuming that the current state is still exactly as it is in the screenshot. Also, please note that these commands won’t necessarily work if you have any uncommitted changes in your working directory, you’ll probably get errors saying you can’t do some of these.

# Delete the current `repair-11.8` branch
git branch -D repair-11.8

# Recreate the `repair-11.8` branch pointing where you want it to point
git branch repair-11.8

# Move `HEAD` and `master` to point to the same commit as `origin/master`
git reset origin/master

# Move `HEAD` to point to the same commit as `repair-11.8`
git checkout repair-11.8

If you want to understand better how git works, the reference that really helped me understand all of this was jwiegley’s Git from the Bottom Up. It will walk you through step-by-step to better understand why git works the way it works so you can reason your way out of these situations :grinning:

But please don’t hesitate to ask if you have any questions!

1 Like

Lee,

I looked through that book and that looks really helpful. I had been going through a Udemy course, but not as helpful.

What I want is:

  • the HEAD looks right.
  • to move the origin/master to 567ea1 (where the HEAD is now). origin/master is currently on the branch with the issue
1 Like

That’s possible, but I am not sure if that’s what you really want, because in that case you would lose the four commits that are currently only on the origin/master branch. :warning:

If losing everything that’s only on the remote branch is really what you want, you’ll need to force push the local master branch to Github. Force-pushing will overwrite the origin/master branch with your local master.

git push --force origin master

If you want to bring the two branches together instead (that is, keep changes from both), there are a number of options:

  • Merge the branches. That will create a merge commit with the tips of both branches as ancestors, combining the changes on both branches. You may have to resolve conflicts, if any. Afterwards you can do a regular push.
  • Rebase one branch on the other. Rebasing means to recreate the commits that are only on one branch starting from the latest commit on the other. If you want this to work without force-pushing you’ll have to rebase your local master on top of origin/master.
  • If you only want to keep some of the commits in origin/master, you can use git cherry-pick to apply them on your local master before force-pushing.
1 Like