[git] There isn't anything to compare. Nothing to compare, branches are entirely different commit histories

I have a CMS theme installed on my machine. I'm tracking changes to it via git and decided to back it up on GitHub so I could share those changes.

The theme as provided is also available on GitHub. On my machine I have added this as a remote upstream. Now I can easily see the changes between my master and the remote upstream by using the following command:

git diff --color master upstream/number

If I could add the remote upstream on GitHub I could easily share these changes. Is it possible to set this relationship on GitHub?

I have tried the following:

git push -u origin upstreambranch

which adds an upstreambranch to the master on GitHub. However trying to compare both branches doesn't work, the result I get on GitHub is that: "There isn't anything to compare"

Is there an alternative way to compare these?

This question is related to git github git-diff

The answer is


This looks like undesirable behavior on github's part, but it's fairly easy to fix. What you want to do is to rebase your branch on a reasonable (any reasonable) commit in the existing history. What you can do is to fetch the github repo and find which tree in its history is most similar to the one you started with. Start this way:

git remote add github u://r/l
git fetch github

myroot=`git rev-list master --max-parents=0`
root_tree=`git rev-parse $myroot^{tree}`

github_base=`git log --pretty=%H\ %T github/master | sed -n "s/$root_tree//p"`

With any luck, that will find you a commit in the github history that has the exact tree you started with. Assuming it does,

git rebase --onto $github_base $myroot master 

and you're done.


If that doesn't find a matching tree, you get to find a nearest approximation. Here's one way to get a rough estimate of the differences:

git log --pretty='echo %H $(git diff-tree -p -b -U0 '$myroot:' %T|wc -l)' github/master \
| sh

which will count the lines in a minimized diff between the tree of each commit in the github/master history and your root tree. It seems reasonable to hope for a nice small difference, you could eyeball the actual diffs on it before calling that the github_base commit and doing the rebase above.



I wanted to copy commit history of "master" branch & overwrite the commit history of "main" branch .
The steps are:-

  1. git checkout master
  2. git branch main master -f
  3. git checkout main
  4. git push

To delete master branch:-

a. Locally:-

  1. git checkout main
  2. git branch -d master

b. Globally:-

  1. git push origin --delete master

Do Upvote it!


I solved that problem. In my case when i did “git clone” in one directory of my choice without do “git init” inside of that repository. Then I moved in to the cloned repository, where already have a “.git” (is a git repository i.e. do not need a “git init”) and finally I started do my changes or anything.

It probably doesn’t solve the problem but shows you how to avoid it.

The command git clone should be a “cd” command imbued if no submodule exists.


I found that none of the answers provided actually worked for me; what actually worked for me is to do:

git push --set-upstream origin *BRANCHNAME*

After creating a new branch, then it gets tracked properly. (I have Git 2.7.4)


From the experiment branch

git rebase master
git push -f origin <experiment-branch>

This creates a common commit history to be able to compare both branches.


This happened with me yesterday cause I downloaded the code from original repo and try to pushed it on my forked repo, spend so much time on searching for solving "Unable to push error" and pushed it forcefully.

Solution:

Simply Refork the repo by deleting previous one and clone the repo from forked repo to the new folder.

Replace the file with old one in new folder and push it to repo and do a new pull request.


Top guy is probably right that you downloaded instead of cloning the repo at start. Here is a easy solution without getting too technical.

  • In a new editor window, clone your repo in another directory.
  • Make a new branch.
  • Then copy from your your edited editor window into your new repo by copy paste.

Make sure that all your edits are copied over by looking at your older github branch.


I had a similar situation, where my master branch and the develop branch I was trying to merge had different commit histories. None of the above solutions worked for me. What did the trick was:

Starting from master:

git branch new_branch
git checkout new_branch
git merge develop --allow-unrelated-histories

Now in the new_branch, there are all the things from develop and I can easily merge into master, or create a pull request, as they now share the same commit hisotry.


You can force update your master branch as follows:

git checkout upstreambranch  
git branch master upstreambranch -f    
git checkout master  
git push origin master -f

For the ones who have problem to merge into main branch (Which is the new default one in Github) you can use the following:

git checkout master  
git branch main master -f    
git checkout main  
git push origin main -f

The following command will force both branches to have the same history:

git branch [Branch1] [Branch2] -f 

A more simple approach where you can't mingle with the master.

Consider i have master and JIRA-1234 branch and when i am trying to merge JIRA-1234 to master i am getting the above issue so please follow below steps:-

  1. From JIRA-1234 cut a branch JIRA-1234-rebase (Its a temp branch and can have any name. I have taken JIRA-1234-rebase to be meaningful.)

    git checkout JIRA-1234

    git checkout -b JIRA-1234-rebase

  2. The above command will create a new branch JIRA-1234-rebase and will checkout it.

  3. Now we will rebase our master.

    git rebase master (This is executed in the same branch JIRA-1234-rebase)

  4. You will see a window showing the commit history from first commit till the last commit on JIRA-1234-rebase. So if we have 98 commits then it will rebase them 1 by 1 and you will see something like 1/98.

  5. Here we just need to pick the commit we want so if you want this commit then don't do anything and just HIT Esc then :q! and HIT ENTER.
  6. There would be some changes in case of conflict and you need to resolve this conflict and then add the files by

    git add <FILE_NAME>.

  7. Now do git rebase continue it will take you to rebase 2/98 and similarly you have to go through all the 98 commits and resolve all of them and remeber we need to add the files in each commit.

  8. Finally you can now push these commits and then raise Pull Request by

    git push or git push origin JIRA-1234-rebase


If you know from which commit issue started, you can reset your branch to that commit and then merge them.


I don't think we have same case here, but still someone else may find it helpful.

When similar error occurred to me, it was going to be the first merge and first commit. There was nothing in on-line repository. Therefore, there was no code on git-hub, to compare with.

I simply deleted the empty repository and created new one with same name. And then there was no error.


I had an issue where I was pushing to my remote repo from a local repo that didn't match up with history of remote. This is what worked for me.

I cloned my repo locally so I knew I was working with fresh copy of repo:

git clone Your_REPO_URL_HERE.git

Switch to the branch you are trying to get into the remote:

git checkout Your_BRANCH_NAME_HERE

Add the remote of the original:

git remote add upstream Your_REMOTE_REPO_URL_HERE.git

Do a git fetch and git pull:

git fetch --all

git pull upstream Your_BRANCH_NAME_HERE

If you have merge conflicts, resolve them with

git mergetool kdiff3 

or other merge tool of your choice.

Once conflicts are resolved and saved. Commit and push changes.

Now go to the gitub.com repo of the original and attempt to create a pull request. You should have option to create pull request and not see the "Nothing to compare, branches are entirely different commit histories" Note: You may need to choose compare across forks for your pull request.


I had mine solved by overriding the branch:

My case: I wanted to override whatever code is in the develop with version_2.

  1. delete the local copy of conflicting branch:
git checkout version_2
git branch -D develop
  1. checkout a fresh branch from the version_2 and force push to git:
git checkout -b `develop`
git push origin `develop`

I didn't need to rebase. But in my case, I didn't need to take code from my old code.


Terminology

First, let's get some terminology out of the way...

upstream <= The remote git repo (likely whose master or release branch is in production)

forked-repo <= The remote [experimental git repo] (https://docs.github.com/en/github/getting-started-with-github/fork-a-repo) also known as "origin".

local repo <= The files and directories that you work with on your local workstaion, which you likely got by running a git clone my-forked-repo.git command

local index <= Also known as your local git "stage", i.e., where you stage your files before pushing them to you remote repo.

Github workflow process

Next, let's talk about the process of getting your changes to the upstream repo:

The process is generally to work on a feature branch and then push said branch, and open a Pull Request, either to your forked-repo's master branch or to the upstream's master branch

Create a feature branch by running git checkout -b FEATURE_BRANCH_NAME

Add/delete/modify files project files.

Add files by running git add .

Commit your files to your index by running git commit -m'My commit message'

Push your staged files by running git push origin FEATURE_BRANCH_NAME

Solution for entirely different commit histories

The master and upstreambranch are entirely different commit histories message can occur when you've forked a git repository and have changed your git history.

For example, if you fork a repo and pull your forked repo to work on it locally...

If then you decide to rewrite the entire application and then decide it's a good idea to deleting all existing files, including the forked-repo's .git directory. You add new files and directories to recreate your app and also recreate your .git directory with git init command.

Now, your application works great with your new files and you want to get it merged into the upstream repo. However, when you push your changes you get that "...entirely different commit histories..." error message.

You'll see that your original git commit will be different in your new local directory and if in your remote fork (as well as your upstream). Check this out by running this command in your current directory: git log --reverse master. Then running the following: pushd $(mktemp -d); git clone https://github.com/my-forking-username/my-forked-repo.git; git log --reverse master; popd

You must fix your local .git repo to match your remote my-forked-repo if you want to push your commits and subsequently perform a pull request (in hopes of merging your new updates to the upstream/master branch).

git clone https://github.com/my-forking-username/my-forked-repo.git
cd my-forked-repo
git checkout -b my-new-files-branch-name
# Delete all files and directories except for the .git directory
git add .
git commit -m'Remove old files'
# Copy your new files to this my-forked-repo directory
git add .
git commit -m'Add new files'
git push origin my-new-files-branch-name

Create a PR on GitHub and request to merge your my-new-files-branch-name branch in your my-forked-repo into master.

Note: The "...entirely different commit histories..." error message can also occur in non-forked repos for the same reasons and can be fixed with the same solution above.


I solve my issue using these commands

git checkout [BRANCH]   
git branch master [BRANCH] -f   
git checkout master   
git push origin master -f

I got this error message, because I was migrating an application from SVN to GitHub and it's not enough to invoke a git init in the location of the source code checked out from SVN, but you need to invoke a git svn clone in order to have all the commit history. This way the two source codes on GitHub will have a mutual history and I was able to open pull requests.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to git-diff

How to show uncommitted changes in Git and some Git diffs in detail Git list of staged files Create patch or diff file from git repository and apply it to another different git repository There isn't anything to compare. Nothing to compare, branches are entirely different commit histories git: diff between file in local repo and origin Git diff between current branch and master but not including unmerged master commits How to Diff between local uncommitted changes and origin How to see the changes in a Git commit? How do you take a git diff file, and apply it to a local branch that is a copy of the same repository? git diff file against its last change