'GitHub "can't automatically merge"?

Sometimes when i try to merge the head fork into my base fork, or my base fork into the head fork, I get the following message on GitHub:

"Can’t automatically merge. Don’t worry, you can still create the pull request."

How do I view the conflicts, if there are any for this?

I have read about 10 different examples with various commands but I can't tell what the names in the examples apply to in my situation as different names for bases, forks, branches, etc, exist.

After all of this, I can't believe there isn't a command you can type to see the conflicts, edit the conflicts and go on with merging. If there is, I haven't found it yet.



Solution 1:[1]

Let's say there is your-branch and the master branch. You want to merge changes from your-branch into the master for others to see them, but someone else did conflicting changes to the master (e.g. merging their PR) in the meantime. It is often useful to merge master into your-branch (i.e. do the merge the other way round) before creating the PR.

In the command line, you can:

git checkout master
git pull
git checkout your-branch
git merge master

Now you can see the list of conflicts. Follow the messages you get from git to resolve the conflicts. You can use your favorite tools, so it is way easier. Finally, you commit&push. When you re-create the PR, there should be no conflicts.

Solution 2:[2]

That means that your pull request can't be merged into the upstream without the upstream owner(s) having to resolve merge conflicts.

The resolution here would be for you to do a fetch from the upstream and then resolve the merge conflicts from the upstream. At this point, if you theoretically resolve the conflicts from the upstream and then create your pull request, upstream would be able to automatically merge in your pull request without having any conflicts (provided there were no commits on the upstream between you locally resolving the upstream merge conflicts and merging into your local/fork, and then creating the pull request).

Let's use GitHub as an example here for remote repo store.

OriginalAccount\repo1 - say this is the original repository (we will refer to this as "upstream")

YourAccount\repo1 - this would be your fork of the repository (this is typically the "origin" remote)

repo1 local - this is your local copy of the repository.

When you create a pull request from YourAccount\repo1 to OriginalAccount\repo1 (virtually from origin to upstream), seeing the message that you can't merge automatically means that OriginalAccount\repo1 has commits that YourAccount\repo1 doesn't have (commits that were most likely pushed after you forked).

The solution here would be to fetch from upstream to your local repository (from OriginalAccount\repo1 to your local repo) and resolve any merge conflicts locally. Then push your commits to YourAccount\repo1. At this point, you should be able to create your pull request that should be able to be automatically merged into OriginalAccount\repo1.

Note: Even though most Git services won't prevent you from continuing on with a pull request that requires the upstream contributors to resolve merge conflicts, it is good practice and good etiquette to ensure that your pull request merges with no conflict. Think about it like this, you should be doing the merge conflict resolution work, instead of having the upstream contributors doing that work from your contribution.

Solution 3:[3]

The easy way to do this is to use Github this way:

  • In Github select branches tab, click merge view button, drag your head fork to the left box, drag your base fork to the right box.
  • Click Merge Branches button.
  • Select changes tab.
  • Select each file that has conflicts. see '<<<<<<<' Edit files in external viewer to keep headfork code and add yours to it.
  • Click Save on file(s).
  • Click Commit to button.
  • Click sync.
  • Go to Git website and try merging your base fork to head fork again.

It should no longer show the original message

Solution 4:[4]

You get conflicts when your code being merged into the repo will conflict with the code already there. If your pull request is accepted, it will be up to the repo manager to resolve them.

Solution 5:[5]

Nice answer from : Clean up a fork and restart it from the upstream

The simplest solution would be (using 'upstream' as the remote name referencing the original repo forked):

git remote add upstream /url/to/original/repo
git fetch upstream
git checkout master
git reset --hard upstream/master  
git push origin master --force 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aaa bbb
Solution 2 Evan R
Solution 3 sandip
Solution 4 Ben
Solution 5 diyism