Git: possible to make a branch into the master, ignoring any merge conflicts?

I've got a git repo with one branch, in addition to the master. I want to make that one branch the master -- overwriting the master completely. Obviously I could do a merge, but this will result in many conflicts, and it seems like a lot of work to resolve them when I know I ALWAYS want the file from the additional branch, not the master.

Is it possible to convert a branch to master, or to do a merge and tell git to always favor the file from one branch over the other?

Thanks in advance!


You can do this in one shot with the reset command:

git checkout master
git reset --hard topicBranch

A hard reset will make the current branch point to the given commit. This will throw away any commits on the master that are not already on the topic branch; they will not be merged.

The same warning for rebases applies here as well: don't do this if you've already push the master branch to any repository shared with other developers.


If you are the only developer accessing this repository and it is not shared with other developers, this is pretty easy to do by creating new branches. Before you begin, make sure that your repository is clean (all modifications have been checked into the current branch).

Make a backup of your old master branch:

git checkout master
git branch oldMaster

Delete old master and create new master:

git checkout topicBranch
git branch -D master
git branch master

If hosting on the repository on a remote server, you will need to update the remote:

git push --force remoteName master:master

IMPORTANT: If other developers have been pulling from your repository, this method will generate errors for them the next time they pull from your repo or you push to their repo.


I'm still not an expert in git, but it looks like you want the rebase command:

git checkout branch_name
git rebase master
git checkout master

Looks like this will auto-merge all the changes into your master at the point where you're branch diverged from master, then re-apply the current changes to master ontop of those changes (so you won't have to merge).

Of course if you just wanted to completely overwrite the master, I think you should just be able to push that branch onto the master repository if you use the force option. Something like this:

git push --force origin branch_name

Then your remote will have all the changes from your branch in the master branch, and you can pull it back out...

git checkout master
git pull --force origin
链接地址: http://www.djcxy.com/p/43552.html

上一篇: Git:如何正确合并两个功能完全不同的分支?

下一篇: Git:可能会将分支插入主控中,忽略任何合并冲突?