Git merge with force overwrite -git force merge

6.9K    Asked by DanielCameron in Data Science , Asked on Jul 12, 2021

I have a branch called demo which I need to merge with the master branch. I can get the desired result with the following commands:

git pull origin demo
git checkout master
git pull origin master
git merge demo
git push origin master

My only concern is, if there are any merge issues, I want to tell git to overwrite changes in a master branch without giving me a merge prompt. So basically changes in the demo branch should automatically overwrite changes in the master branch.

I looked around there are multiple options but I don't want to take chances with merging.

Answered by Asistha pandey

For force merge git you can try these commands:

git fetch origin   # update all our origin/* remote-tracking branches
git checkout demo
git merge -s ours branch
git checkout master
git merge -s ours origin/master
git merge -s ours branch
git push origin master

These commands will help to overwrite the demo branch.



Your Answer

Answer (1)

To perform a git merge with force overwrite, you can use the --strategy option along with the ours or theirs strategy. Here's how you can do it:


Merge with 'ours' strategy:

git merge --strategy=ours 

This will perform the merge, but if there are conflicts, it will resolve them by favoring the current branch's changes, effectively overwriting changes from the branch being merged in.

    git merge --strategy=theirs 

This will perform the merge, but if there are conflicts, it will resolve them by favoring the changes from the branch being merged in, effectively overwriting changes from the current branch.

Remember, using forceful merge strategies like ours or theirs can lead to loss of changes if not used carefully, so make sure to review the changes and conflicts before proceeding. Additionally, it's often a good idea to communicate with your team members before forcefully overwriting changes.

Merge with 'theirs' strategy:

6 Months

Interviews

Parent Categories