How to Use Git’s Ours Merge Strategy

Thomas Milgrew
2 min readMar 31, 2023

--

Git is a powerful version control system that allows you to manage changes to your codebase and collaborate with others on software projects. One of the most useful features of Git is its merge strategy, which allows you to combine changes from different branches of your codebase.

Git has several merge strategies that you can use, including the “ours” merge strategy. The “ours” merge strategy is useful when you want to merge two branches, but you want to keep all the changes from one of the branches and ignore changes from the other branch. In this blog post, we’ll show you how to use Git’s “ours” merge strategy.

Step 1: Switch to the branch you want to keep changes from

Before you start the merge process, ensure that you are on the branch that you want to keep the changes from. For example, if you want to keep the changes from the main branch, switch to the main branch using the following command:

git checkout main

Step 2: Start the merge process

Once you are on the branch you want to keep the changes from, you can start the merge process by running the following command:

git merge -s ours <other-branch>

Replace <other-branch> with the name of the branch you want to merge. For example, if you want to merge changes from the feature-branch, run the following command:

git merge -s ours feature-branch

Step 3: Perform the merge

Git will now perform the merge, but it will ignore any changes from the other branch and keep the changes from the current branch. You can review the changes by using the git diff command.

Step 4: Push changes to the remote repository

Finally, you can push the changes to the remote repository using the following command:

git push origin main

This will push the changes to the main branch in the remote repository.

Conclusion

Using Git’s “ours” merge strategy can be a powerful tool when you want to merge changes from different branches and keep all the changes from one of the branches. However, it’s important to use this strategy with caution and ensure that you understand the implications of discarding changes from the other branch. By following the steps outlined in this blog post, you can use Git’s “ours” merge strategy with confidence and effectively manage changes to your codebase.

--

--