This branch has conflicts that must be resolved что делать
Resolving a merge conflict using the command line
In this article
You can resolve merge conflicts using the command line and a text editor.
Merge conflicts occur when competing changes are made to the same line of a file, or when one person edits a file and another person deletes the same file. For more information, see «About merge conflicts.»
Tip: You can use the conflict editor on GitHub to resolve competing line change merge conflicts between branches that are part of a pull request. For more information, see «Resolving a merge conflict on GitHub.»
Competing line change merge conflicts
To resolve a merge conflict caused by competing line changes, you must choose which changes to incorporate from the different branches in a new commit.
For example, if you and another person both edited the file styleguide.md on the same lines in different branches of the same Git repository, you’ll get a merge conflict error when you try to merge these branches. You must resolve this merge conflict with a new commit before you can merge these branches.
Navigate into the local Git repository that has the merge conflict.
Generate a list of the files affected by the merge conflict. In this example, the file styleguide.md has a merge conflict.
Open your favorite text editor, such as Atom, and navigate to the file that has merge conflicts.
Add or stage your changes.
Commit your changes with a comment.
You can now merge the branches on the command line or push your changes to your remote repository on GitHub and merge your changes in a pull request.
Removed file merge conflicts
To resolve a merge conflict caused by competing changes to a file, where a person deletes a file in one branch and another person edits the same file, you must choose whether to delete or keep the removed file in a new commit.
For example, if you edited a file, such as README.md, and another person removed the same file in another branch in the same Git repository, you’ll get a merge conflict error when you try to merge these branches. You must resolve this merge conflict with a new commit before you can merge these branches.
Navigate into the local Git repository that has the merge conflict.
Generate a list of the files affected by the merge conflict. In this example, the file README.md has a merge conflict.
Open your favorite text editor, such as Atom, and navigate to the file that has merge conflicts.
Decide if you want keep the removed file. You may want to view the latest changes made to the removed file in your text editor.
To add the removed file back to your repository:
To remove this file from your repository:
Commit your changes with a comment.
You can now merge the branches on the command line or push your changes to your remote repository on GitHub and merge your changes in a pull request.
How To Resolve Merge Conflicts in Git
Home » DevOps and Development » How To Resolve Merge Conflicts in Git
The git merge command helps a contributor add to a project from a branch. The concept is one of the core ideas of collaborative programming, allowing multiple people to work on their part of the code without any conflicts.
When multiple contributors work on the same part of a code or work with numerous branches, merge conflicts are bound to happen. The primary goal of git merge is to resolve or warn about these conflicts automatically.
This guide explains what a merge conflict is and offers resolutions for when they do not sort out automatically. The article also provides helpful tips for preventing Git merge conflicts.
What Is a Git Merge Conflict?
When working with version control systems such as Git, most merge conflicts resolve automatically. However, there are situations where git merge is unable to resolve an issue.
Some examples of merge conflicts include:
Since the problem happens locally and the rest of the project members are unaware of the issue, resolving the conflict is of high priority and requires an immediate fix.
Types Of Git Merge Conflicts
The general types of merge conflicts depend on when the issue appears. The conflicts happen either:
How To Resolve Merge Conflicts in Git
There are three ways to resolve a merge conflict in Git:
1. Accept the local version. To accept all changes on a file from the local version, run:
Alternatively, to accept the local version for all conflicting files, use:
2. Accept the remote version. To update the changes on a file from the remote branch, run:
Accept the remote version for all conflicting files with:
Ultimately, the choice of what parts of the code stay and which do not depends on the developer’s decision for the current project.
Getting a Merge Conflict in Git
The merge conflict in Git happens when the command git merge throws an error.
The error message prints the information about where the conflict is present. Check the file from the error message and look at the contents where the merge conflict happened:
Git automatically adds three indicators alongside the conflicting lines of code:
The added syntax helps search through the code to find the location of the merge conflict. However, a much more straightforward approach is to use a difference/merging tool to discover the issues and track the changes.
Setting Up a Default Diff Tool in Git
To set up the default diff tool for git mergetool :
1. Run the following line in your terminal:
The output prints out all the supported diff tools for your current setup:
Different tools are available based on the editor of choice. For example:
The further steps show an example of how to set up the vimdiff tool for Vim.
2. Change the git config to set the default merge tool:
For example, if using Vim, run:
3. Set the diff tool to show the common ancestor for both files, which is the version before any edits:
4. Set the option to not prompt before running:
The diff tool setup for Git is complete.
Using Mergetool to See the Differences
To use the mergetool and see the differences, run:
The output displays a window with four views:
1. LOCAL represents the file version from the current branch.
2. BASE is how the file looked before any changes.
3. REMOTE shows how the file looks in the remote branch where the conflicting information is.
4. MERGED has the final merge file. This result represents what gets saved to the repository.
The primary navigation commands between these windows are:
Updating and Resolving a Merge Conflict
Update the MERGED file to resolve a conflict. Some shortcuts for updating the MERGED version include:
Note: Ensure the cursor is on the line where the conflicts are before running these commands.
Commit and Cleanup
The final step is to commit and clean up the extra files. Commit the updated version by running:
Note: If you made a mistake while committing, you could undo the last commit.
The diff tool creates extra files on the project for comparing versions. Clean them up with:
Tips On How to Prevent Merge Conflicts
Merge conflicts only happen when the computer is not able to resolve the problem automatically.
Here are some tips on how to prevent merge conflicts:
Merge conflicts happen when working in Git or other version control programs from time to time. By following the instructions in this guide, you know how to handle merge conflicts and how to prevent them from happening.
For more Git resources, check out our article on What Is Git Upstream and How To Set Upstream Branch.
Merge conflicts
Merge conflicts happen when the two branches in a merge request (the source and target) each have different changes, and you must decide which change to accept. In a merge request, Git compares the two versions of the files line by line. In most cases, GitLab can merge changes together. However, if two branches both change the same lines, GitLab blocks the merge, and you must choose which change you want to keep.
Conflicts you can resolve in the user interface
If any file in your merge request contains conflicts, but can’t meet all of these criteria, you must resolve the conflict manually.
Conflicts GitLab can’t detect
When these branches merge, both example1.txt and example3.txt are present.
Methods of resolving conflicts
Resolve conflicts in interactive mode
Find the merge conflicts message, and select Resolve conflicts. GitLab shows a list of files with merge conflicts. The conflicts are highlighted:
Resolve conflicts in the inline editor
Resolve conflicts from the command line
While most conflicts can be resolved through the GitLab user interface, some are too complex. Complex conflicts are best fixed locally, from the command line, to give you the most control over each change:
Open the terminal and check out your feature branch. For example, my-feature-branch :
Rebase your branch against the target branch (here, main ) so Git prompts you with the conflicts:
The GitHub Blog
It’s going to happen one day. One of your students tries to merge one branch into another, and the dreaded error appears:
When changes in two branches overlap, Git shouts out for a clever human (like you or one of your students) to reconcile the differences. Even if you follow all the best practices of Git—communicate with your collaborators, pull in changes regularly, and make commits that are small, logical chunks—merge conflicts can still happen.
But merge conflicts don’t have to be scary for you or your students! In this post you’ll learn how to help students resolve them.
Why do merge conflicts happen?
There are many situations when a merge conflict can happen—when a student pulls in changes from a repository, after they open a pull request, or when they push changes to another repository (there are other, less common situations too, like unstashing, cherry picking, and rebasing, that won’t be covered here). Let’s look at a few examples:
Each of these situations leads to a merge conflict, but the process for resolving them is similar.
Yours, mine, and ours: resolving merge conflicts
When Git encounters a conflict, it pauses whatever it was doing (like pulling changes) and asks you to resolve the conflict. To help you sort out the situation, Git inserts conflict markers into your files, showing you what your changes are and what your collaborators’ changes are. Conflict markers look like this:
In this example, Students Frank and Helen have both modified README.md. Frank’s change has been merged to master while Helen’s changes are on a local topic branch. Helen discovered this when she tried to merge the changes from master into her topic branch.
Helen looks closely at the conflicting line. Both students have made roughly the same change to the file by adding a link, but Frank’s change includes a typo. Helen decides to keep her version (HEAD) and discard Frank’s (master). She does this by deleting Frank’s changes and the conflict markers, like this:
When she’s done resolving the conflict, she runs git add README.md to tell Git. Satisfied with her changes, she runs git commit to finish the merge.
Consider another example, however, where Helen and Frank have both changed the same line in different ways. Before either of their changes, the file they’re both working on contains this line:
In this example, Frank has expanded the sentence while Helen has added a filename. Helen pulls in the changes and sees these conflict markers:
It doesn’t make much sense to choose one side over the other, so Git offers another option: write a new line that wasn’t in either side’s commit history. Like this:
If there are more conflicts, then Git will insert conflict markers for each conflicting chunk it finds. Resolving many conflicts is like resolving one, except there are more conflict markers to tidy up. Walk through them one-by-one to resolve all the conflicts in all of the affected files. When you’re finished, add the files and commit.
Resolving conflicts in pull requests
Sometimes you’ll see a merge conflict in the context of a pull request.
A conflict in a pull request is like one on the command line. In a pull request, you can discuss with your collaborators how you plan to resolve merge conflicts and revise your branch.
The first option: the pull request author can update their branch. Since pull requests work with branches, you can often resolve a merge conflict by adding one or more commits to the incoming branch. For example, you can merge changes locally (e.g. git merge master ) and push the branch with the newly-added merge commit to GitHub.
If it’s a complicated conflict, you can discuss the best approach right from the pull request comments. Alternatively, you can amend your pull request (for example, by rebasing the incoming branch or amending a previous commit). This can be confusing at times, especially if there’s already been discussion in the comments.
Another option is to let the person accepting the pull request resolve the conflict while merging it. You can do this within the web interface using the GitHub editor or with the command line instructions GitHub provides—either way, it’s just like resolving a conflict with Git alone by choosing the resulting lines and removing conflict markers.
Bringing it back to the classroom
Resolving merge conflicts under a deadline might not be the best time for students to learn about them. Deliberately introducing merge conflicts in the classroom can help students when it comes time to handle them on their own. Check out Dr. Mine Çetinkaya-Rundel’s talk in which she shares how she teaches students Git skills, including resolving merge conflicts.
Want to learn even more about helping students understand and resolve merge conflicts? Check out the Campus Advisors learning module #3. This video course will help you learn more about group work, pull requests, and how to get your students comfortable with resolving conflicts.
Do you have an assignment you like to use to teach merge conflicts? Let us know in the GitHub Education Community.
What I can do to resolve «1 commit behind master»?
After pushing I’ve been seeing this message at remote repository:
This merge has conflicts that must be resolved before it can be committed.
To manually merge these changes into TA20footerLast run the following commands:
git checkout 7c891f50c557
Note: This will create a detached head!
git merge remotes/origin/master
8 Answers 8
Before you begin, if you are uncomfortable with a command line, you can do all the following steps using SourceTree, GitExtensions, GitHub Desktop, or your favorite tool.
To solve the issue, you might have two scenarios:
1) Fix only remote repository branch which is behind commit
Example: Both branches are on the remote side
ahead === Master branch
behind === Develop branch
Solution:
Clone the repository to the local workspace: this will give you the Master branch, which is ahead with commit
Create a branch with Develop name and checkout to that branch locally
Pull from the remote Develop branch. Conflict might occur. if so, fix the conflict and commit the changes.
Merge the local Develop branch with the remote Develop branch
Push the merged branch to the remote Develop branch
2) Local Master branch is behind the remote Master branch
This means every locally created branch is behind.
Before preceding, you have to commit or stash all the changes you made on the branch behind commits.
Solution:
Checkout your local Master branch
Pull from remote Master branch
Now your local Master is in sync with the remote branch. As a result of the above command, other local branches branched from the previous local Master branch are not in sync. To fix that:
Checkout the branch that is behind your local Master branch
Merge with the local Master branch
If this branch is on the remote repository, you have to push your changes.
Add remote from original repository in your forked repository:
Updating your fork from original repo to keep up with their changes:
If your branch is behind by master then do:
After merging it, check if there is a conflict or not.
If there is NO CONFLICT then:
If there is a conflict then fix your file(s), then:
If the message is «n commits behind master.»
You need to rebase your dev branch with master. You got the above message because after checking out dev branch from master, the master branch got new commit and has moved ahead. You need to get those new commits to your dev branch.
there can be some merge conflicts which you have to resolve.
Suppose currently you are in your branch myBranch
Do the following :-
If all changes are committed
If changes are not committed than
Check if there are any conflicts then resolve and commit changes
Having a look at your repository in GitHub.
Imagine the scenario in which main or master is your primary branch, and also you have another branch as dev, and your dev branch is N commits behind main or master, and there’s nothing about this living in your local machine.
Firstly you have to make a clone from the Github repository to your machine, however, at first, your local git doesn’t have to know about your secondary branch status, only from the main or master, so Git gets only your remote label branch updated, and there are no commits yet in your local.
So, what you need is at least 1 commit, 1 push to main or master, and 1 pull back to you local.
Do NOT push anything for your dev branch else you would have 1 commit ahead and N commits behind main or master (so, conflicts).
So, you’ll need an auxiliary branch to send a Pull Request to your remote main or master.
Without further ado. How to proceed in this scenario:
Ater cloning the repository.
Now, you have to make at least one change in order to record a first commit in you local Git, suposing you haven’t anything to implement at the moment. Take the README.md file to change a letter, a space, a blank line more.
I use accepting that with Rebase and Merge (the last option in green button when dropdown), after the accepting GitHub will ask you if you want to delete aux-branch. yes you want, so delete, ’cause your main or master now has this newest feature brought by aux-branch. Now back to your local (Git), proceed this following way:
Now go to your GitHub reload the page, drop down to your dev branch you’ll see your dev branch is the same as your main/master branch.