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
The following steps are performed in the Git browser powered by Atlassian (Bitbucket server)
Making {current-branch} as master
master
and name it “master-duplicate”.One can also checkout all files from the other branch into master:
git checkout master
git checkout better_branch -- .
and then commit all changes.
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.
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
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
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