Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 894
Jeff Croft Avatar asked Oct 18 '11 17:10

Jeff Croft


People also ask

Can you ignore merge conflicts in git?

Well, you can not ignore conflicts, because that means that something is wrong, and you have to tell Git that you fixed the conflict. If you really want to keep the file as-is, you can remove the conflict diff lines, and then git add / git commit the files that were in conflict so that you keep all lines of the file.

Is it possible to avoid merge conflicts?

Merge conflicts are mostly inevitable. You will experience more than one merge conflict in your career, but with good communication and planning, you can reduce the number of merge conflicts you encounter. Let's discuss how we can do that!

How do you avoid a merge conflict in git?

Git commands that can help resolve merge conflicts Passing the --merge argument to the git log command will produce a log with a list of commits that conflict between the merging branches. diff helps find differences between states of a repository/files. This is useful in predicting and preventing merge conflicts.

How do I force a branch merge with master?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.


2 Answers

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.

like image 168
Jay Conrod Avatar answered Oct 03 '22 13:10

Jay Conrod


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.

like image 20
David M. Syzdek Avatar answered Oct 03 '22 14:10

David M. Syzdek