Failed 'git add'

Fellow Git users,

While trying to combine an existing repository, with a remote repository (ie. I started with having a local repository, and executed the “git add” command) it does not seem to work. I am stuck and don’t know how to fix it.

The commands I executed and their responses can be seen below.

Anybody that has any ideas?

Please feel free to link to the repository if you wish.

Thanks,

Manie

C:\Users\GIT\courcera>git remote add origin https://github.com/M7-Robotics/courcera.git

C:\Users\GIT\courcera>git remote -v
origin https://github.com/M7-Robotics/courcera.git (fetch)
origin https://github.com/M7-Robotics/courcera.git (push)

C:\Users\GIT\courcera>git status
On branch master
nothing to commit, working tree clean

C:\Users\GIT\courcera>git push -u origin master
To https://github.com/M7-Robotics/courcera.git
 ! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/M7-Robotics/courcera.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

C:\Users\GIT\courcera>git pull origin master
From https://github.com/M7-Robotics/courcera
 * branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories

If I read the error message correctly:

fatal: refusing to merge unrelated histories

 it means your work tree and the remote you just added do not have any commit in common. That means Git cannot know what to do with them.

I suggest you start examining the log of both repositories. I would do it like that:

vimdiff =(git log master) =(git log remotes/origin/master)

If it is confirmed that the trees do not have commit in common, then the first step would be figuring out how you arrived to that situation: how did you start your work repository? how was the remote repository started?

1 Like

Thanks for the reply.

I created the remote repository (master branch only, and a single file), and then created the local repository (and did some experiments). So, yes they do not have a common commit. Now I arrived here where I wish to combine the two.

I hoped that the two masters would just merge. Would it work to rebase it somehow?

In the meantime I tried the following:

C:\Users\GIT\courcera>git pull --no-ff
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

C:\Users\GIT\courcera>git branch --set-upstream-to=origin/master master
Branch 'master' set up to track remote branch 'master' from 'origin'.

C:\Users\GIT\courcera>git pull --no-ff
fatal: refusing to merge unrelated histories

C:\Users\GIT\courcera>git status
On branch master
Your branch and 'origin/master' have diverged,
and have 8 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

C:\Users\GIT\courcera>git pull --no-ff
fatal: refusing to merge unrelated histories

C:\Users\GIT\courcera>git log --oneline
01f51a2 (HEAD -> master, origin) commit
f163dc1 Probleem
a01833d Tak 3 invoeg by basis
d0b9a58 (tag: tak3) Tak 3 se werk
c886982 (tag: tak2) Verandering nuwe tak
c8f6753 Nuwe tak
f83db5b (tag: v0.1) added file2.txt
323ad38 (tag: v0.0) second commit

Something else I tried:

C:\Users\GIT\courcera>git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 232ee7b add Readme.txt

It seems I need to point my tracking branch to the right place.  But don’t know how.

C:\Users\GIT\courcera>git log master -1
commit 01f51a291dc376ffd53896c3b1fa1dca4083e6a6 (HEAD -> master, origin)

    commit

C:\Users\GIT\courcera>git log origin/master
commit 232ee7bcfd14dc105e9747137dcd95f41f7cc6f6 (origin/master, remote_merge)

    add Readme.txt

I found a solution, although it does not feel like a very elegant solution. Rebasing did work:

C:\Users\GIT\courcera>git rebase origin/master master
First, rewinding head to replay your work on top of it...
Applying: second commit
Applying: added file2.txt
Applying: Nuwe tak
Applying: Verandering nuwe tak
Applying: Tak 3 se werk
Applying: Probleem
Applying: commit

C:\Users\GIT\courcera>git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

C:\Users\GIT\courcera>git push
Counting objects: 21, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (21/21), 1.67 KiB | 155.00 KiB/s, done.
Total 21 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), done.
To https://github.com/M7-Robotics/courcera.git
   232ee7b..c465055 master -> master

I think it only worked (in this case) because what I did locally is completely independant from what was on the remote repository (ie. no possibility for conflicts)

I think it would have worked in any case, you just would have had to resolve conflicts.

AFAIK, creating two repositories independently is a bad idea. Just create one and clone it to start the second. And if you already have the local repository and need to create the GitHub one, do not populate it, leave it completely empty to allow the initial push.