708

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?

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119

16 Answers16

1017
  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.

GitHub has recently renamed its master branch to main so (depending on whether your branch is called master or main) in step 5 you might have to use git push origin master and for pulling patches from upstream git pull upstream main && git push origin main, otherwise you will receive an error message.

benebrue
  • 25
  • 6
troelskn
  • 115,121
  • 27
  • 131
  • 155
  • 33
    There is no reason to rename the original origin, just call the new play-ground something else. – tacaswell Aug 02 '13 at 03:18
  • 47
    @tcaswell Technically no, but it is a strong convention that `origin` points to the canonical remote location. In this case, the github location would presumably be the canonical. – troelskn Aug 11 '13 at 10:27
  • 1
    I would consider 'upstream' to be the canonical location, but I think this is start to split hairs. – tacaswell Aug 11 '13 at 15:05
  • 3
    Maybe I should post a different question but I think you guys are discussing what I want. I'd like to add a new remote, not replace the existing one so I can continue to pull from the old repository but push any changes to the new one. – ThatAintWorking Sep 15 '14 at 20:56
  • 19
    @ThatAintWorking You probably should open a new question, but in short - you can add any number of remotes with the command `git remote add`. You can then push to one of them by explicitly stating the remote in `git push`. E.g. `git push foobar master` to push the current branch to `master` on remote `foobar`. – troelskn Sep 16 '14 at 10:49
  • GitHub gives you a service for importing automatically from Subversion server repo: [Importing from SVN](https://help.github.com/articles/importing-from-subversion/) – kelgwiin Feb 27 '15 at 14:06
  • does not push cloned files – Furkan Gözükara Jul 30 '16 at 09:47
  • Nice solution but with an important shortcoming: the `upstream` remote repo meta data is only available in the local "workarea" repo; not the new remote repo. In other words, if I clone the new repo somewhere else (`git clone URL_TO_GITHUB_REPO` to follow example) and try `git remote show upstream` I get: "fatal: 'upstream' does not appear to be a git repository". Hence I've lost crucial ability to do things like `git pull upstream master`. Is there a solution to this? – sxc731 Nov 15 '16 at 12:48
  • @sxc731Not really. Github has their own internal tracking of "forks" between repositories, but the way that git is constructed, means that all repository clones are fully autonomous. Once you fork, there is really no canonical master, apart from whatever convention you uphold. – troelskn Nov 15 '16 at 15:50
  • 2
    When I do it like this and don't use **"--mirror push"** from [mob's answer](https://stackoverflow.com/a/18751888/5195032), I always get `error: failed to push some refs to 'https://github.com/username/testrep.git'` – Denis Babarykin Dec 29 '17 at 00:45
  • 4
    Didn't push all my branches, just master. The mirror answer worked for all the branches – user1114 Aug 22 '19 at 21:57
  • 2
    Also ... remember to recover tags: "git push --tags" – Mario Orlandi Oct 10 '19 at 08:06
  • For "error: failed to push some refs to 'https://..." You can use "git push -f origin master" – Fractal Mind Sep 23 '21 at 04:15
  • 1
    This answer worked great for me. Instead of renaming origin, I just `git remote add new-remote-name URL_TO_NEW_GITHUB_REPO` and pushed to it `git push new-remote-name branch-name` – Anupam Sep 28 '21 at 06:27
  • where in these codes is there an equivalent command to 'add' and 'commit' like it would be for a push from a local repo? or does the mere setting of the two remotes (an upstream one and then the downstream one (the new githubrepo)) sets up the those 'add' and 'commit' instructions? – Abdul-Kareem Abdul-Rahman Apr 13 '23 at 13:32
154

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
Olie
  • 24,597
  • 18
  • 99
  • 131
mob
  • 117,087
  • 18
  • 149
  • 283
  • 11
    What is the benefit of the bare clone and the mirror push over simply adding and pushing to another remote? Is it that all branches will be pushed and not just the current branch? (If so, this seems like a limitation of the accepted answer that should be noted.) – yoyo Jul 21 '16 at 00:38
  • 3
    This was exactly the info I was looking for, the --bare / --mirror is usually what people want to perform! This is an important answer! Thanks – claf Sep 13 '16 at 09:06
  • 3
    Great! Definetly the most correct answer on topic question. Thanks! If I don't do it like this, I always get `error: failed to push some refs to 'https://github.com/username/testrep.git'` – Denis Babarykin Dec 29 '17 at 00:43
78

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.

  3. 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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 3
    the `git push ... old_branch_name:new_branch_name` allows you to push a feature branch from the old repository as the main branch in the new repository. Usefull! – gorjanz Jul 25 '18 at 08:29
  • This was the simplest way for me to do it. – Adam Nov 04 '19 at 23:38
64

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
Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51
32

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
Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
22

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.

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 git@example.com/upstream-repository.git
cd upstream-repository.git
git push --mirror git@example.com/new-location.git
HairOfTheDog
  • 2,489
  • 2
  • 29
  • 35
  • I think this is the link: http://blog.plataformatec.com.br/2013/05/how-to-properly-mirror-a-git-repository/ – David Feb 11 '16 at 03:03
16

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.

vm345
  • 813
  • 12
  • 28
  • I liked this since it helped me duplicate a repo with a specific subset of branches, some of them being superfluous. I note that doing this with a GitHub-based repo seems to make it automatically create a PR for each non-default branch, and you can just close them; not sure if possible to stop the PRs from coming. – learning2learn Mar 14 '22 at 12:38
16

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 git@bitbucket.org:Batman/batmanRepoName.git

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

Then push normally like so:

git push -u origin master
Abdel Ourimchi
  • 196
  • 1
  • 5
7

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
Denise Ignatova
  • 465
  • 4
  • 7
5

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.

upe
  • 1,862
  • 1
  • 19
  • 33
5

Link a local repository to a different remote repository

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!

Caro Pérez
  • 476
  • 4
  • 4
  • 2
    Careful of deleting the .git directory. All data means all git data, even the git history. I already had some scenarios where i really need to check why something was done. This is not possible anymore if the .git directory is deleted. – Manuel Jan 12 '22 at 09:51
5

First, create your repo on Github. Then change directory to the checked-out source repository - suppose you want to push the master branch. You need to execute 5 simple steps:

git remote add origin2 https://github.com/user/example.git
git checkout master
git pull
git push origin2 master
git remote remove origin2

This creates the link between your local repo and the new remote, check out and pulls the source branch (to ensure that it has the latest), then pushes the current branch, finally unlinks the local repo from the remote.

Your local repo will be intact after this, you can use it as before. If you need to push multiple branches, repeat the checkout-pull-push steps as many times as you need, just change the branch name accordingly.

gaborsch
  • 15,408
  • 6
  • 37
  • 48
3

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
Aguid
  • 943
  • 2
  • 10
  • 24
2

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).

itrascastro
  • 775
  • 6
  • 14
2

Visual studio 2022 and default git extension works flawlessly without even need for a single line of command.

Step 1: Go to git settings

enter image description here

Step 2: Add new origin pointing to different repository in git/azure

enter image description here enter image description here

Step 3: Now you have option to push to new origin in different repository in git/azure

enter image description here

Now there is a new branch in new repository enter image description here

Chandraprakash
  • 773
  • 1
  • 10
  • 18
2

The way I've accomplished this is:

  1. Create a new repo on github (new-repo.git)
  2. cd old-repo/ on local machine and fetch all new changes
  3. git push -u https://github.com/[username]/new-repo.git main -f
  4. Clone your new remote repo https://github.com/[username]/new-repo.git to your local environment

I've found this a simple way to essentially copy an old remote repo into a new remote repo.

elliot
  • 111
  • 2
  • 6