2

I have some repository X. I would like to create a new empty repository that contains all of the files of X and the commit history as well. I have created a new repository remotely. I cloned the old repository X and I ran the following commands (I work with gitflow):

git flow init
git remote set-url origin $URL
git remote -u # Just to check
git push -u origin master

The last command failed with the following error:

To $URL
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '$URL'
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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The new remote repository is empty and does not have any files. It also has only master branch. What am I doing wrong?

Edit: I ran the following command:

git push --force -u origin master

The result:

Counting objects: 3751, done.
Delta compression using up to 36 threads.
Compressing objects: 100% (1131/1131), done.
Writing objects: 100% (3751/3751), 577.15 KiB | 322 KiB/s, done.
Total 3751 (delta 2615), reused 3727 (delta 2605)
error: unpack failed: error zeroPaddedFilemode: object a8025c7657985de77312ba1eea3dd926d50eae2f: mode starts with '0'
fatal: Unpack error, check server log
To $URL
 ! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to '$URL'
vesii
  • 2,760
  • 4
  • 25
  • 71

2 Answers2

1

The new remote repository is empty and does not have any files.

I suppose it have at least one commit, even an empty one, which would explain why the push fails.

Try git push --force -u origin master and check the remote repository history reflect your local one.

This assumes "origin" is set to the new empty remote repository you have created.
If not:

git remote set-url origin /url/new/empty/remote/repo

Regarding the error zeroPaddedFilemode message, check if the local repository you want to add has any error:

git fsck

If it does, see "How to fix Git zero-padded file modes warning": re-create the local repo, with an export/import should fix it, and then the push --force should work.
See also gitlab-org/gitlab-foss issue 22095 as an illustration for this error.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thank you for your help but when running the first command you suggested, I still get the same error. Any other suggestions? – vesii Nov 28 '19 at 22:11
  • @vesii Exactly the same error? Because a `--force` is made to override the remote history. – VonC Nov 28 '19 at 22:12
  • @vesii Double-check the `git remote -v` output: its URL must reference the new empty repo; – VonC Nov 28 '19 at 22:13
  • Yeah sorry, I have added the full message in my topic. I ran `git remote -v`, the URL is valid. – vesii Nov 28 '19 at 22:17
0

Git does not like to write a history that it cannot trace back to the time that the current repository was init'ed. If you think about it, that is kind of what you are doing here: taking a big history from somewhere foreign, and trying to slap it on top of this new repo, which doesn't know anything about the history you are trying to graft on top.

The easiest way to do this is to clone the repo, checkout all the branches and tags that you want to take with (if just master, you don't need to check out anything) then re-point remote origin to the new URL, and push.

git clone <OLD URL>
git checkout <branch>
...(repeat for all targeted branches)...
git fetch --tags
git remote rm origin
git remote add origin <new URL>
git push

This should also prevent the need to force-push, which is best avoided.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42