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!
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.
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!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With