Make the current Git branch a master branch

前端 未结 14 1906
悲&欢浪女
悲&欢浪女 2020-11-22 05:29

I have a repository in Git. I made a branch, then did some changes both to the master and to the branch.

Then, tens of commits later, I realized the branch is in muc

相关标签:
14条回答
  • 2020-11-22 06:12

    The problem with the other two answers is that the new master doesn't have the old master as an ancestor, so when you push it, everyone else will get messed up. This is what you want to do:

    git checkout better_branch
    git merge --strategy=ours master    # keep the content of this branch, but record a merge
    git checkout master
    git merge better_branch             # fast-forward master up to the merge
    

    If you want your history to be a little clearer, I'd recommend adding some information to the merge commit message to make it clear what you've done. Change the second line to:

    git merge --strategy=ours --no-commit master
    git commit          # add information to the template merge message
    
    0 讨论(0)
  • 2020-11-22 06:13

    To add to Jefromi's answer, if you don't want to place a meaningless merge in the history of the source branch, you can create a temporary branch for the ours merge, then throw it away:

    git checkout <source>
    git checkout -b temp            # temporary branch for merge
    git merge -s ours <target>      # create merge commit with contents of <source>
    git checkout <target>           # fast forward <target> to merge commit
    git merge temp                  # ...
    git branch -d temp              # throw temporary branch away
    

    That way the merge commit will only exist in the history of the target branch.

    Alternatively, if you don't want to create a merge at all, you can simply grab the contents of source and use them for a new commit on target:

    git checkout <source>                          # fill index with contents of <source>
    git symbolic-ref HEAD <target>                 # tell git we're committing on <target>
    git commit -m "Setting contents to <source>"   # make an ordinary commit with the contents of <source>
    
    0 讨论(0)
  • 2020-11-22 06:14

    If you are using eGit in Eclipse:

    • Right click on the project node.
    • Choose Team → then Advanced → then Rename branch
    • Then expand the remote tracking folder.
    • Choose the branch with the wrong name, then click the rename button, rename it to whatever the new name.
    • Choose the new master, then rename it to master.
    0 讨论(0)
  • 2020-11-22 06:16

    Edit: You didn't say you had pushed to a public repo! That makes a world of difference.

    There are two ways, the "dirty" way and the "clean" way. Suppose your branch is named new-master. This is the clean way:

    git checkout new-master
    git branch -m master old-master
    git branch -m new-master master
    # And don't do this part.  Just don't.  But if you want to...
    # git branch -d --force old-master
    

    This will make the config files change to match the renamed branches.

    You can also do it the dirty way, which won't update the config files. This is kind of what goes on under the hood of the above...

    mv -i .git/refs/new-master .git/refs/master
    git checkout master
    
    0 讨论(0)
  • 2020-11-22 06:18

    Make sure everything is pushed up to your remote repository (GitHub):

    git checkout main
    

    Overwrite "main" with "better_branch":

    git reset --hard better_branch
    

    Force the push to your remote repository:

    git push -f origin main
    
    0 讨论(0)
  • 2020-11-22 06:18

    I found this simple method to work the best. It does not rewrite history and all previous check-ins of branch will be appended to the master. Nothing is lost, and you can clearly see what transpired in the commit log.

    Objective: Make current state of "branch" the "master"

    Working on a branch, commit and push your changes to make sure your local and remote repositories are up to date:

    git checkout master      # Set local repository to master
    git reset --hard branch  # Force working tree and index to branch
    git push origin master    # Update remote repository
    

    After this, your master will be the exact state of your last commit of branch and your master commit log will show all check-ins of the branch.

    0 讨论(0)
提交回复
热议问题