Search code examples
gitversion-control

Make current git branch the master branch (when there are no changes to master)


my question is very similar to this question, with one difference I wanted some extra clarity on (that one is also 12+ years old and the answer may be very different now). In that question there's the issue of discarding updates to the master branch and only keeping updates to the new "better" branch, but in my case the most current version of the master branch is exactly identical to the first version of my other branch. I did something similar, where I branched then realized I actually wanted all the stuff I did to be reflected in master.

So the question is, can I turn my "better" branch into the master branch? Apologies if the answer is the same as in the linked question, I'm < 1 year into development and there aren't any senior devs to ask these questions to at my job. Normally I'm down with trial and error but I'm way more skiddish trying stuff out that I'm unsure of when it comes to VCS.

Thanks!


Solution

  • If I understand correctly, your commit history is linear, so it looks something like this:

    A --- B --- C --- D
          ^           ^
          |           |
        master     better
    

    In this case, you can simply fast-forward master:

    $ git checkout master
    $ git merge --ff-only better
    

    The --ff-only argument just ensures that you won't get a merge commit in case you made a mistake.

    After this, both master and better will be pointing at commit D, so you can delete better:

    $ git branch -d better
    

    Using lowercase -d ensures that you won't accidentally be discarding any commits that haven't been merged.