0
      To https://github.com/prakashperam/myrepo
      ! [rejected]        master -> master (non-fast-forward)
      error: failed to push some refs to 
      'https://github.com/prakashperam/myrepo.git'
      hint: Updates were rejected because the tip of your current branch is 
      behind
      hint: its remote counterpart. Integrate the remote changes (e.g.
      hint: 'git pull ...') before pushing again.
      hint: See the 'Note about fast-forwards' in 'git push --help' for details

I have a remote repository. I have copied the code directly from the server(not cloned from git) in which the application is running and made few changes and added files. Now, I want to push the code to the remote repository but it is throwing me above error.

when I do git pull here is the
`` * branch HEAD -> FETCH_HEAD fatal: refusing to merge unrelated histories


error

  • 1
    Why do you use copy instead of clone? And do you want to keep the commit history from the remote repo or the commit history from your local repo? – Marina Liu Feb 23 '18 at 07:43

2 Answers2

1

While it is possible to merge unrelated histories, that's the wrong thing to do in this case.

Normally I would not recommend saving your work elsewhere and starting over, but in this particular case, that's the right path. Make a new clone of the original, copy your work into the new clone, and make a new commit from the copy. This part of your question:

I have copied the code directly from the server (not cloned from git)

is why.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Because, the code in the server and the code in the github are inter related. However, server code is recent and there are many changes which are not pushed to the github. – Prakash AWS Feb 27 '18 at 08:08
  • If you have a different repository with multiple commits, you can attempt to combine repositories. If you have a repository you made specifically with *one* commit with the *current* version of the code, though, it has zero value: clone the original, copy the current version in to the work-tree, and commit the current version as one new commit. You will be able to work with the new repository and have it mesh with the old one, and you have lost nothing by changing your single commit in the erroneous repository into a single commit in the correct one. – torek Feb 27 '18 at 15:53
0

this is what I would do.

Say you copied the code into ~/copied

run the following commands

git clone https://github.com/prakashperam/myrepo ~/cloned
cd ~/cloned
git remote add localcopy ~/copied/.git
git rebase localcopy master
git push
  1. we properly clone the repo from github
  2. we cd into it
  3. we add a remote pointing to the manually copied version of the repo
  4. we rebase therefore avoid the "unrelated histories"
  5. now we can push and from here on we'll be fully synched to whatever is there in github
sudavid4
  • 1,081
  • 6
  • 14