To resolve a merge conflict in Git
, you can follow these steps:
1. Checkout the branch:
Start by checking out the branch where the merge conflict occurred. Typically, this is the branch you were merging into, such as your feature branch or the main branch.
2. Pull the latest changes:
git checkout <branch-name>
It's essential to have the latest changes from the remote repository before resolving the conflict. Use the following command to pull the changes:
3. Identify the conflicted files:
git pull origin <branch-name>
Git will indicate the conflicted files in your project. You can use the `git status` command to see which files have conflicts. The conflicted files will have markers indicating the conflict within the file. For example:
4. Open the conflicted file:
$ git status
(use "git add <file>..." to mark resolution)
both modified: file.txt
Open the conflicted file(s) in a text editor and locate the conflict markers. The conflict markers look something like this:
This is the content on your branch.
This is the content from the branch you are merging.
The content between `<<<<<<< HEAD` and `=======` is from your branch, and the content between `=======` and `>>>>>>> branch-name` is from the branch you are merging.
5. Resolve the conflict:
Edit the conflicted file to resolve the conflict. Decide which changes to keep, modify the content as needed, and remove the conflict markers. For example, you might choose to keep one version of the code or combine the changes manually. After making the necessary changes, save the file.
6. Stage the resolved file:
Once you have resolved the conflict in a file, you need to stage it to mark it as resolved. Use the following command for each resolved file:
7. Commit the changes:
After staging all the resolved files, commit the changes with a commit message describing the conflict resolution.
8. Push the changes:
git commit -m "Resolve merge conflict"
Finally, push the changes to the remote repository to complete the merge.
git push origin <branch-name>
After following these steps, the merge conflict should be resolved, and your branch should be up to date with the changes from the other branch.