Contents

Resolving Git Conflicts with CLI

Git is a powerful version control system that enables developers to collaborate on projects and manage changes to the codebase. When multiple developers work on the same file and make conflicting changes, Git creates a conflict that needs to be resolved. Resolving conflicts is an important part of the Git workflow and ensures that the codebase remains consistent and functional.

Reproduce the Git Conflict

First things first, let’s reproduce the conflict.

  1. Create a new Git repository:
git init myproject
cd myproject
  1. Create a new file and add some content:
echo "Hello, world!" > myfile.txt
git add myfile.txt
git commit -m "Added myfile.txt"
  1. Create a new branch:
git branch feature
  1. Switch to the new branch:
git checkout feature
  1. Make changes to the file in the new branch:
echo "Goodbye, world!" >> myfile.txt
git add myfile.txt
git commit -m "Changed myfile.txt in feature branch"
  1. Switch back to the master branch:
git checkout master
  1. Make changes to the same file in the master branch:
echo "How are you today?" >> myfile.txt
git add myfile.txt
git commit -m "Changed myfile.txt in master branch"
  1. Merge the feature branch into the master branch:
git merge feature

At this point, Git will detect a conflict because both branches have made changes to the same file. Git will provide instructions on how to resolve the conflict. You can then use a text editor or CLI tools like git mergetool to resolve the conflict.

Steps to Resolve Git Conflicts with Vim

To manually resolve Git conflicts, follow these steps:

  1. Open the affected file in a text editor.
vim myfile.txt
  1. Navigate to the first conflict Use the Vim commands to navigate to the first conflict in the file. You will see conflict markers that indicate the start and end of the conflicting sections:
<<<<<<< HEAD
This is the text from the HEAD
=======
This is the text from the incoming changes
>>>>>>> incoming

The text between the <<<<<<< HEAD and ======= markers is the content from the local repository (HEAD), while the text between the ======= and >>>>>>> incoming markers is the content from the incoming changes.

  1. Resolve the conflicts
    Edit the conflicting sections to keep the changes you want to keep and remove the changes you don’t want to keep. Once you’ve resolved the conflicts, remove the conflict markers so that the file looks correct.

  2. Save the changes and exit Vim
    Save the changes by typing :w and exit Vim by typing :q.

  3. Mark the file as resolved
    Mark the file as resolved using the git add command:

git add myfile.txt
  1. Commit the changes
    Commit the changes using the git commit command:
git commit -m "Resolved conflicts in myfile.txt"

That’s it! You’ve successfully resolved Git conflicts using Vim.

Git Mergetool

You can set Vimdiff as your default merge tool in Git by running the following command:

git config merge.tool vimdiff

This will set Vimdiff as your default merge tool for resolving conflicts in Git. You can then use the command git mergetool filename.txt to launch Vimdiff when Git encounters a conflict. This will bring up the different versions of the file in different Vim split panels, as you described.

Here is an example of what the Vimdiff interface may look like when resolving a Git conflict:

+--------------------------------+
| LOCAL  |     BASE     | REMOTE |
+--------------------------------+
|             MERGED             |
+--------------------------------+

After running git mergetool, you will see the different versions of the file displayed in Vim split panels. The top left split panel shows the LOCAL version of the file, the top middle split panel shows the BASE version of the file, and the top right split panel shows the REMOTE version of the file. The bottom split panel shows the MERGED version of the file, which is where you will make your changes.

When Vimdiff detects differences between the different versions of the file, it will highlight them in red, as you mentioned. You can then use Vim commands and shortcuts to navigate between the different split panels, make changes to the file, and save your changes once you are done resolving the conflict.

To resolve Git conflicts using a merge tool, follow these steps:

  1. Launch the merge tool.
  2. Select the files with conflicts.
  3. Review the highlighted changes and select the changes you want to keep.
  4. Save the changes and exit the merge tool.
  5. Add the resolved files to the staging area using the “git add” command.
  6. Commit the changes using the “git commit” command.

Best Practices for Avoiding Git Conflicts

While Git conflicts can be easily resolved, it’s always best to avoid them altogether. Here are some best practices to follow:

  • Pull the latest changes before making your own changes.
  • Break up large changes into smaller, manageable chunks.
  • Communicate with your team to ensure you’re not working on the same file at the same time.
  • Use feature branches to isolate changes and minimize conflicts.

Conclusion

Git conflicts are a common occurrence in the development process, but they can be easily resolved with the right tools and techniques. By following best practices and communicating with your team, you can minimize the risk of conflicts and ensure a smoother development process.