Make the current Git branch a master branch

前端 未结 14 1905
悲&欢浪女
悲&欢浪女 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 05:58

    The following steps are performed in the Git browser powered by Atlassian (Bitbucket server)

    Making {current-branch} as master

    1. Make a branch out of master and name it “master-duplicate”.
    2. Make a branch out of {current-branch} and name it “{current-branch}-copy”.
    3. In repository setting (Bitbucket) change “Default Branch” to point at “master-duplicate” (without this step, you will not be able to delete master - “In the Next step”).
    4. Delete “master” branch - I did this step from source tree (you can do it from the CLI or Git browser)
    5. Rename “{current-branch}” to “master” and push to repository (this will create a new “master” branch still “{current-branch}” will exist).
    6. In repository settings, change “Default Branch” to point at “master”.
    0 讨论(0)
  • 2020-11-22 06:02

    One can also checkout all files from the other branch into master:

    git checkout master
    git checkout better_branch -- .
    

    and then commit all changes.

    0 讨论(0)
  • 2020-11-22 06:04

    I found the answer I wanted in the blog post Replace the master branch with another branch in git:

    git checkout feature_branch
    git merge -s ours --no-commit master
    git commit      # Add a message regarding the replacement that you just did
    git checkout master
    git merge feature_branch
    

    It's essentially the same as Cascabel's answer. Except that the "option" he added below his solution is already embedded in my main code block.

    It's easier to find this way.

    I'm adding this as a new answer, because if I need this solution later, I want to have all the code I am going to use in one code block.

    Otherwise, I may copy-paste, then read details below to see the line that I should have changed - after I already executed it.

    0 讨论(0)
  • 2020-11-22 06:05

    From what I understand, you can branch the current branch into an existing branch. In essence, this will overwrite master with whatever you have in the current branch:

    git branch -f master HEAD
    

    Once you've done that, you can normally push your local master branch, possibly requiring the force parameter here as well:

    git push -f origin master
    

    No merges, no long commands. Simply branch and push— but, yes, this will rewrite history of the master branch, so if you work in a team you have got to know what you're doing.




    Alternatively, I found that you can push any branch to the any remote branch, so:

    # This will force push the current branch to the remote master
    git push -f origin HEAD:master
    
    # Switch current branch to master
    git checkout master
    
    # Reset the local master branch to what's on the remote
    git reset --hard origin/master
    
    0 讨论(0)
  • 2020-11-22 06:08

    My way of doing things is the following

    #Backup branch
    git checkout -b master_backup
    git push origin master_backup
    git checkout master
    #Hard Reset master branch to the last common commit
    git reset --hard e8c8597
    #Merge
    git merge develop
    
    0 讨论(0)
  • 2020-11-22 06:09

    For me, i wanted my devl to be back to the master after it was ahead.

    While on develop:

    git checkout master
    git pull
    
    git checkout develop
    git pull
    
    git reset --hard origin/master
    git push -f
    
    0 讨论(0)
提交回复
热议问题