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 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
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>
If you are using eGit in Eclipse:
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
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
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.