Git : how to accept all current/incoming changes

Git is the most popular version control systems in the world. By taking snapshots, it keeps track of every change made on the project. A project can be rolled back to any previous version/status easily.

Initially, Git was intended for use by software development teams. However, it's flexible enough for virtually any file type. Git can be used by anyone, whether you're a teacher, a businessperson, a graphic designer, or a content writer.

As a team member using Git for collaboration, you may sometimes find yourself in a tricky situation where everyone is working on the same project at the same time. Depending on the number of changes we are trying to commit, these situations might make the process more challenging or time-consuming.

The scenario

Let's say you have to work on a new feature that you are really excited about. It therefore makes sense for you to create a new branch.

When you have completed all the changes necessary to achieve your goal, let's suppose you now need to merge the old branch, from which you have created the new feature branch, with the current branch. Before you go ahead with the merge, you notice there are several new commits on the branch made by other people.

urbanch

In spite of the fact that you noticed it, you think the changes in the old branch won't affect yours, therefore the merge command is executed within the your branch. You now continue to merge it.

$ (awesome-new-feature) git merge original-branch

You'll encounter the following error message.

Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

There are literally hundreds of changes, it would not make sense to manually handle all of them.

Git : accept all current changes

According to the git checkout man page, the command has options called --theirs and --ours. One will keep the merged version, and the other will keep the original one.

These options mentioned above are only available in Git versions 1.6.1 and later.

In order to quickly accept all current changes (which means our changes in Git's terms) in, for example, main.py, you have to run the following command.

(awesome-new-feature) git checkout --ours main.py

Following this, you come back to Git and explicitly specify that you fixed the conflict by adding the file to the Index by running.

(awesome-new-feature) git add main.py

If you want to ignore all changes made by others in the branch, keeping only what you created, you can cd to the repository and run git against the current directory.

(awesome-new-feature) git checkout --ours .

Git : accept all incoming changes

Similar to how we used --ours to ignore incoming changes, you can use --theirs to ignore all changes made by yourself and merge other commits.

(awesome-new-feature) git checkout --theirs main.py
(awesome-new-feature) git add main.py

In the case that you wish accept all incoming changes in the branch, you can cd to the repository and run git against the current directory.

(awesome-new-feature) git checkout --theirs .

Manage Git conflicts in VSCode

Visual Studio Code has excellent Git integration, incorporate most popular commands into its GUI.

The Git merge strategies mentioned above can be performed in VSCode quickly by opening Command Palette and search for "merge", you can see the Accept all current changes along with all other possible options.

Manage git conflicts in VSCode

Alternatively, you can select files in the sidebar, right-click and select Accept All Incoming or Accept All Current based on your specific scenario.

Accept All Incoming Git changes in VSCode

Click to rate this post!
[Total: 17 Average: 5]

Leave a Comment