How to Use theirs With Git Merge
-
Use the
git merge
Command Withtheirs
in Git -
Use
--strategy-option
to Resolve Conflicts in Git - Use Temporary Branch to Merge in Git
While developing software with the Git tool, you can create different branches for different features. But there may be conflicts between different branches.
This article will explain using the git merge
command with the theirs
option to resolve conflicts.
Use the git merge
Command With theirs
in Git
The git merge
command can combine two or more development histories. However, this merge can sometimes not be done due to conflicts between branches.
Files in ours
or theirs
should be discarded to resolve this conflict.
The ours
refers to the original working branch with the authority of Git history, and the theirs
refers to the branch that contains the new applied commits.
You can use the git merge -s ours
to discard changes in ours
. This command discards all changes from the other branch and leaves the files on your branch unchanged.
When you next merge from the other branch, Git will only consider changes made from this point forward. However, the -s
option cannot be used with theirs
.
Use --strategy-option
to Resolve Conflicts in Git
Alternatively, we can use theirs
with the -X
or --strategy-option
option.
The main difference between -X
and -s
options is that -X
performs a regular recursive merge, resolving any conflicts using the chosen side, whereas -s
changes the merge to just completely ignore the other side.
The following command resolves any conflicts using the theirs
.
git merge -X theirs branchname
Use Temporary Branch to Merge in Git
Use the commands below to merge test2
into checked out test1
.
Switch to the test1
branch.
git checkout test1
Merge the commit without conflicts. The contents of ours
will be discarded later.
git merge -s ours test2
Create a temporary branch.
git branch temp
Set HEAD
with the git reset
command. It gets contents from the test2
branch.
git reset --hard test2
Reset to merged commit but keep contents with the git reset --soft
command.
git reset --soft temp
Change the merged commit’s contents with test2
’s contents.
git commit --amend
Delete the temporary branch we created earlier.
git branch -D temp
You can see that the merged commit contains only the contents of test2
.
git diff HEAD test2
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn