0

All I want to do is make my master branch exactly like my develop branch.

I tried to merge and rebase my master from my develop branch. Neither solution worked.

My develop branch has a different directory structure and it is proving to be more difficult than I anticipated.

Previous Rebase

The rebase I tried in the link above worked for the test branch, but not the master. The master was the initial commit and hadn't been updated since, so it was far behind.

Although both the test branch and the master branch had the same directory structure. I am not sure why master isn't working.

During rebase, I got an error that stated to look in the patch file, but I have no idea what to look for.

I was researching this issue and came across a few threads in which the question was how to change what branch the HEAD is on.

So, can I change the HEAD to develop, then delete the master branch and just create a new one from the develop branch?

That seems a lot more clean than trying to rebase/merge in my situation.

Community
  • 1
  • 1
flhhc
  • 19
  • 4
  • 1
    If either `master` or `develop` is an ancestor of the other, you don't need to rebase; you can just ` git checkout $OLDBRANCH; git reset --hard $NEWBRANCH`. I don't know for sure though, because I can't figure out your branch structures from the question. – Yawar Sep 27 '16 at 22:37
  • Re "branch": this term is ambiguous in Git. It's not clear whether you mean the branch *structures* within the repository, or the branch *name*. See http://stackoverflow.com/questions/25068543/what-exactly-do-we-mean-by-branch for more about this. – torek Sep 28 '16 at 03:47
  • 1
    Re "change what branch the HEAD is on": in Git, `HEAD` is a special name (special because it is built in to many Git commands). In the top level directory of your repository, run `cat .git/HEAD` to see its contents. It usually contains `ref: refs/heads/`, which means that you are on the named branch. In other words, `HEAD` is just *a file that contains the name of your current branch*. When you do `git checkout otherbranch`, Git rearranges your work-tree as needed, then writes the name `otherbranch` into the HEAD file. So `HEAD` itself is quite boring, other than storing a branch *name*. – torek Sep 28 '16 at 03:49
  • 1
    Last, besides the two meanings of "branch" and the relative boring-ness of `HEAD`, there's the simple fact that each branch *name* is just a pointer to a specific commit. The commits themselves form the branch structure; you're free to rearrange the *names* at any time, but no commit can ever change. Each commit stores a complete snapshot of some work-tree, and that's the snapshot you get when you `git checkout` that commit. It's not clear to me if you just want to rearrange the names, or make a new commit with a new work-tree layout, whose parent commit is the old work-tree layout. – torek Sep 28 '16 at 03:52

1 Answers1

2

I put a bunch of stuff in comments, but to answer the question posed:

So, can I change the HEAD to develop, then delete the master branch and just create a new one from the develop branch?

Yes, you can do just that. In fact, you don't even have to do that, you can forcibly move the label master (i.e., no separate "delete" step is required—see the second section below).

The commits within the repository will be quite unchanged by this process. For instance, before you do this, you might start out with ths:

                         HEAD
                          |
                          v
o--o--...--o--o     <-- master
    \
     o--o--...--o   <-- develop

In the middle, when HEAD points to develop instead of master, you get this:

o--o--...--o--o     <-- master
    \
     o--o--...--o   <-- develop
                          ^
                          |
                         HEAD

If you then delete the name master you have this:

o--o--...--o--o     [abandoned]
    \
     o--o--...--o   <-- develop
                          ^
                          |
                         HEAD

and if you then add master as another name pointing to the same commit as develop, you get:

o--o--...--o--o     [abandoned]
    \
     o--o--...--o   <-- develop, master

(at this point you can make HEAD point back to master, if you like).


Without actually deleting master, you can simply make master point to the same commit as develop. There are two commands that can do this; which one to use, depends on what's in HEAD.

The more obvious command is:

git branch -f master develop

This tells Git to change master so that it now points to wherever develop points. That is, this tells Git to overwrite the hash ID stored under the name master with the hash ID stored under the name develop.

If HEAD currently points to master, the command is:

git reset --hard develop

The reason to use git reset is that if HEAD currently points to master, that indicates that the work-tree (and its corresponding index) are set up to match the commit to which master points. If you change the commit to which master points, without changing the index and work-tree, Git's internals will be out of sync (not a total disaster, but potentially very confusing). The drawback to git reset --hard is that it wipes out any uncommitted work you have in the work-tree. This is also the advantage to git reset --hard. (It's an advantage if that's what you want; it's a drawback if not.)

(Note that if you're not on master and you git reset --hard develop, you've just forcibly re-pointed whatever other branch you are on. That is, git reset always affects the current branch.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Wow, thanks for the detail. I now understand it MUCH better. Although when I tried to run git reset --hard master develop, I got an error fatal: Cannot do hard reset with paths. – flhhc Oct 12 '16 at 19:08
  • So I ended up changing the origin/HEAD to develop and deleted the master branch, then created a new one on the develop commit. – flhhc Oct 12 '16 at 19:09
  • Ah, oops, I didn't want `git reset --hard master develop` but rather just `git reset --hard develop` (`git reset` *always* resets `HEAD`, or the branch to which `HEAD` points, you can't give it a branch name to reset, only a commit to reset-to). – torek Oct 12 '16 at 20:46