How to Merge With Force Overwrite in Git
Most of the time, when we apply git push
or git merge
, eventually, some conflict occurs. In some cases, the solution to merge conflict is as simple as discarding local changes or remote or other branch changes.
When Git can’t figure out how to merge two conflicting changes, it creates a conflict request. A conflict request is a special kind of patch that describes the problem, and it contains both sides of the conflicting change (ours and theirs) and the result of the merge attempt.
While working on the same files by two members of teams and got a conflict in that file, Git applies the conflict to our working file. We can then edit the resulting file, and Git will record our changes. If a conflicting change does occur, Git will mark the file as being in a conflict state. There are several commands for resolving conflicts in that particular branch.
Conflicts are most common when two or more people work on the same file in the same repository. However, when the conflict is found in a file, Git is very smart and intelligent about how to solve that in a pretty awesome way. Git uses conflict markers to show which parts of the file conflict. The conflict markers are little hashes placed on either side of the conflicting section of the file.
Pull is not used singly. It is used with the assistance of fetching data from the remote server and then applying merging with the changes in the local repository. These two below-mentioned operations can be executed if we want.
git fetch
git merge origin/$CURRENT_BRANCH
The origin/$CURRENT_BRANCH
mentioned above means below.
- Git will apply merge options and apply the changes from the remote repository, namely
origin
. - And that is added to the
$CURRENT_BRANCH
- That are not currently present in our local checked out branch
git pull
is not only recommended, which just runs git fetch
followed by git merge
. We’ll do three merges, through which Git will execute three fetch operations, where one fetch is all we will need.
git fetch origin # it will update all our origin/* remote-tracking branches
git checkout new branch
git merge origin/new branch
git checkout master
git merge origin/master
git merge -X theirs new branch
git push origin master
The commands mentioned above would effectively ignore any changes that were different on the branch we were merging from and develop a new commit on the branch we are merging to, where the commits are all merged.
We can also use --ours
on a normal merge to merge all changes in the branch we are merging from, and then skip any files that exist in the branch we are merging to, effectively doing a three-way merge between the two branches and then just using the files from the branch you are merging to.
We found it much easier to use git merge --ours
to merge the files and then use git rebase -i
to manually re-apply the changes from the branch I was merging from.
The above commands would not work on files that had conflicts, but we found the following would work to resolve the conflict.
git checkout file_with_conflict
git merge --ours --no-commit file_from_branch_with_conflict
git reset --hard git add file_with_conflict git commit -m
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn