Git push existing repo to a new and different remote repo server?

GitGithub

Git Problem Overview


Say I have a repository on git.fedorahosted.org and I want to clone this into my account at github to have my own playground aside from the more "official" repo on fedorahosted. What would be the steps to initially copy that over? Within github there is this nice "fork" button, but I can't use this for obvious reasons.

And how would I track changes in the fedorahosted repo into the github one?

Git Solutions


Solution 1 - Git

  1. Create a new repo at github.
  2. Clone the repo from fedorahosted to your local machine.
  3. git remote rename origin upstream
  4. git remote add origin URL_TO_GITHUB_REPO
  5. git push origin master

Now you can work with it just like any other github repo. To pull in patches from upstream, simply run git pull upstream master && git push origin master.

Solution 2 - Git

There is a deleted answer on this question that had a useful link: https://help.github.com/articles/duplicating-a-repository

The gist is

0. create the new empty repository (say, on github)
1. make a bare clone of the repository in some temporary location
2. change to the temporary location
3. perform a mirror-push to the new repository
4. change to another location and delete the temporary location

OP's example:

On your local machine

$ cd $HOME
$ git clone --bare https://git.fedorahosted.org/the/path/to/my_repo.git
$ cd my_repo.git
$ git push --mirror https://github.com/my_username/my_repo.git
$ cd ..
$ rm -rf my_repo.git

Solution 3 - Git

To push your existing repo into different, you need to:

  1. Clone the original repo first.

     git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
    
  2. Push the cloned sources to your new repository:

     cd rhq
     git push https://github.com/user/example master:master
    

You may change master:master into source:destination branch.


If you want to push specific commit (branch), then do:

  1. On the original repo, create and checkout a new branch:

     git checkout -b new_branch
    
  2. Choose and reset to the point which you want to start with:

     git log # Find the interesting hash
     git reset 4b62bdc9087bf33cc01d0462bf16bbf396369c81 --hard
    

Alternatively select the commit by git cherry-pick to append into existing HEAD.

  1. Then push to your new repo:

     git push https://github.com/user/example new_branch:master
    

If you're rebasing, use -f for force push (not recommended). Run git reflog to see history of changes.

Solution 4 - Git

If you have Existing Git repository:

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/newproject
git push -u origin --all
git push -u origin --tags

Solution 5 - Git

I found a solution using set-url which is concise and fairly easy to understand:

  1. create a new repo at Github
  2. cd into the existing repository on your local machine (if you haven't cloned it yet, then do this first)
  3. git remote set-url origin https://github.com/user/example.git
  4. git push -u origin master

Solution 6 - Git

Do you really want to simply push your local repository (with its local branches, etc.) to the new remote or do you really want to mirror the old remote (with all its branches, tags, etc) on the new remote? If the latter here's a great blog on [How to properly mirror a git repository][1].

I strongly encourage you to read the blog for some very important details, but the short version is this:

In a new directory run these commands:

git clone --mirror [email protected]/upstream-repository.git
cd upstream-repository.git
git push --mirror [email protected]/new-location.git

[1]: http://blog.plataformatec.com.br/2013/05/how-to-properly-mirror-a-git-repository/ "How to properly mirror a git repository"

Solution 7 - Git

Simply point the new repo by changing the GIT repo URL with this command:

git remote set-url origin [new repo URL]

Example: git remote set-url origin [email protected]:Batman/batmanRepoName.git

Now, pushing and pulling are linked to the new REPO.

Then push normally like so:

git push -u origin master

Solution 8 - Git

Try this How to move a full Git repository

  1. Create a local repository in the temp-dir directory using:

    git clone temp-dir

  2. Go into the temp-dir directory.

  3. To see a list of the different branches in ORI do:

    git branch -a
    
  4. Checkout all the branches that you want to copy from ORI to NEW using:

    git checkout branch-name
    
  5. Now fetch all the tags from ORI using:

    git fetch --tags
    
  6. Before doing the next step make sure to check your local tags and branches using the following commands:

    git tag
    
    
    git branch -a
    
  7. Now clear the link to the ORI repository with the following command:

    git remote rm origin
    
  8. Now link your local repository to your newly created NEW repository using the following command:

    git remote add origin <url to NEW repo>
    
  9. Now push all your branches and tags with these commands:

    git push origin --all
    
    
    git push --tags
    
  10. You now have a full copy from your ORI repo.

Solution 9 - Git

Here is a manual way to do git remote set-url origin [new repo URL]:

  1. Clone the repository: git clone <old remote>

  2. Create a GitHub repository

  3. Open <repository>/.git/config

    $ git config -e
    
    [core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
    [remote "origin"]
        url = <old remote>
        fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
        remote = origin
        merge = refs/heads/master
    

    and change the remote (the url option)

    [remote "origin"]
        url = <new remote>
        fetch = +refs/heads/*:refs/remotes/origin/*
    
  4. Push the repository to GitHub: git push

You can also use both/multiple remotes.

Solution 10 - Git

1- Delete all connection with the remote repository: Inside the project folder:

  • git rm .git (Remove all data from local repository)
  • git status (I must say that it is not linked to any, something like an error)

2- Link to a new remote repository

  • git init To start a local repository
  • git remote add origin urlrepository.git To link with remote repository
  • git remote -v To confirm that it is linked to the remote repository

3- Add changes to the local repository and push to the remote repository

  • git pull or git pull origin master --allow-unrelated-histories if git history is different in both local and remote repo.
  • git add.
  • git commit -m" Message "
  • git push -u origin master

that's it!

Solution 11 - Git

This is has helped me to push my local project into a different repo on git

 git push https://github.com/yourusername/yourgithubproject.git master:master

Solution 12 - Git

I have had the same problem.

In my case, since I have the original repository in my local machine, I have made a copy in a new folder without any hidden file (.git, .gitignore).

Finally I have added the .gitignore file to the new created folder.

Then I have created and added the new repository from the local path (in my case using GitHub Desktop).

Solution 13 - Git

To push an existing repository from the command line

git remote add origin https://github.com/AyadiAkrem/teachandgo.git
git branch -M main
git push -u origin main

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHeiko RuppView Question on Stackoverflow
Solution 1 - GittroelsknView Answer on Stackoverflow
Solution 2 - GitmobView Answer on Stackoverflow
Solution 3 - GitkenorbView Answer on Stackoverflow
Solution 4 - GitKamil NękanowiczView Answer on Stackoverflow
Solution 5 - GitMobiletainmentView Answer on Stackoverflow
Solution 6 - GitHairOfTheDogView Answer on Stackoverflow
Solution 7 - GitAbdel OurimchiView Answer on Stackoverflow
Solution 8 - Gitvm345View Answer on Stackoverflow
Solution 9 - GitupeView Answer on Stackoverflow
Solution 10 - GitCaro PérezView Answer on Stackoverflow
Solution 11 - GitDenise IgnatovaView Answer on Stackoverflow
Solution 12 - GititrascastroView Answer on Stackoverflow
Solution 13 - GitAguidView Answer on Stackoverflow