Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

Git merge with force overwrite -git force merge

Asked by Daniel Cameron Jul 12, 2021 9.3K views 2 answers
Share

About this question

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.

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Apr 23, 2024

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:

Was this helpful?

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.