'How can I push all branch to a newly created repository?

Description

I have a GitLab repository with branch dev, master, and github-snapshot with many tags.

We want to go open source and we have written a small script that creates the GitHub repository and push the whole project on it.

The script is run during a Gitlab-CI and during this time, the head is on detached state using ${CI_COMMIT_REF_NAME} (the commit hash).

Also, we want at the end the branch github-snapshot not to be puhsed on the GitHub repository.

After the GitHub repository creation, the script does in order:

  1. create new remote github.
  2. push all branches to github.
  3. push all tags to .github
  4. Delete branch github-snapshot from remote github.

Reproduction

The step (2) is failing, this is how I do it

git -C ${path} push ${origin} --all

It produces the following error:

[ERROR]  Command failed: /bin/sh -c git -C /mnt/mesos/sandbox/builds/bootstrap-styled/navigation-bar push github --all
warning: refname '93a9252c263da35b2fdc6a7ca78ca18083ac5951' is ambiguous.
Git normally never creates a ref that ends with 40 hex characters
because it will be ignored when you just specify 40-hex. These refs
may be created by mistake. For example,

  git checkout -b $br $(git rev-parse ...)

where "$br" is somehow empty and a 40-hex ref is created. Please
examine these refs and maybe delete them. Turn this message off by
running "git config advice.objectNameWarning false"
remote: error: GH002: Sorry, branch or tag names consisting of 40 hex characters are not allowed.        
remote: error: Invalid branch or tag name "93a9252c263da35b2fdc6a7ca78ca18083ac5951"        
To https://[crypted]@github.com/bootstrap-styled/navigation-bar.git
 ! [remote rejected] 93a9252c263da35b2fdc6a7ca78ca18083ac5951 -> 93a9252c263da35b2fdc6a7ca78ca18083ac5951 (pre-receive hook declined)
 ! [remote rejected] github-snapshot -> github-snapshot (pre-receive hook declined)
 ! [remote rejected] gitlab-to-github -> gitlab-to-github (pre-receive hook declined)
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://[crypted]@github.com/bootstrap-styled/navigation-bar.git'


[ERROR]  Command failed: /bin/sh -c git -C /mnt/mesos/sandbox/builds/bootstrap-styled/navigation-bar push github --all
warning: refname '93a9252c263da35b2fdc6a7ca78ca18083ac5951' is ambiguous.
Git normally never creates a ref that ends with 40 hex characters
because it will be ignored when you just specify 40-hex. These refs
may be created by mistake. For example,

  git checkout -b $br $(git rev-parse ...)

where "$br" is somehow empty and a 40-hex ref is created. Please
examine these refs and maybe delete them. Turn this message off by
running "git config advice.objectNameWarning false"
remote: error: GH002: Sorry, branch or tag names consisting of 40 hex characters are not allowed.        
remote: error: Invalid branch or tag name "93a9252c263da35b2fdc6a7ca78ca18083ac5951"        
To https://[crypted]@github.com/bootstrap-styled/navigation-bar.git
 ! [remote rejected] 93a9252c263da35b2fdc6a7ca78ca18083ac5951 -> 93a9252c263da35b2fdc6a7ca78ca18083ac5951 (pre-receive hook declined)
 ! [remote rejected] github-snapshot -> github-snapshot (pre-receive hook declined)
 ! [remote rejected] gitlab-to-github -> gitlab-to-github (pre-receive hook declined)
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://[crypted]@github.com/bootstrap-styled/navigation-bar.git'

Remark

For thoose who don't understand how git push <remote> --all can fail, I have added some log with git branch -l:

Remote github configured for GitHub.
Git branch list (git branch -l)
  93a9252c263da35b2fdc6a7ca78ca18083ac5951
  github-snapshot
  gitlab-to-github
* master
[ERROR]  Command failed: /bin/sh -c git -C /mnt/mesos/sandbox/builds/bootstrap-styled/navigation-bar push github --all
warning: refname '93a9252c263da35b2fdc6a7ca78ca18083ac5951' is ambiguous.
Git normally never creates a ref that ends with 40 hex characters
because it will be ignored when you just specify 40-hex. These refs
may be created by mistake. For example,

Question

  1. Considering I am in a detached state, how can I push all branch to a newly created repository?

My first idea was to checkout a real branch like master, but that would require another check to see if master is existing. I would like to be informed if there is a more appropriate way to do so.



Solution 1:[1]

To push all your branches, use either (replace REMOTE with the name of the remote, for example "origin"):

git push REMOTE '*:*'
git push REMOTE --all
To push all your tags:

git push REMOTE --tags

Finally, I think you can do this all in one command with:

git push REMOTE --mirror

However, in addition --mirror, will also push your remotes, so this might not be exactly what you want.

Solution 2:[2]

As the error 'remote: error: GH002: Sorry, branch or tag names consisting of 40 hex characters are not allowed.' suggests Git creates issue in branches having names equal to 40 characters. Rename your branch name having characters less than or greater than 40 (e.g. <=39 or >=41). You will never face this issue again. We faced this issue when importing repo from one server to another using Azure import feature.

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 The Dead Man
Solution 2 Sachin Bhardwaj