Reputation: 30994
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?
Upvotes: 759
Views: 875007
Reputation: 2144
Here is a manual way to do git remote set-url origin [new repo URL]
:
Clone the repository: git clone <old remote>
Create a GitHub repository
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/*
Push the repository: git push
You can also use both/multiple remotes.
Upvotes: 5
Reputation: 118695
There is a deleted answer on this question that had a useful link: https://help.github.com/articles/duplicating-a-repository
The gist is
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
Upvotes: 171
Reputation: 11
This was my answer to moving from Microsoft Team Foundation Server to Azure DevOps. It is PowerShell script.
#RUN SCRIPT FROM FOLDER YOU WANT A NEW GIT PROJECT CREATED
#
#1. Ask for URL that needs to be cloned
#2. Ask for branch to clone
#3. Ask what to call the new GIT Project folder
#
#4. RUNS THE CLONE
#
#5. Ask if you want to get all branches from remote
# If YES:
# 6. Gets all branches
# 7. Ask if you want to remove the remote
# If YES:
# 8. Removes remote
# 9. Ask if you want to add a new remote
# IF YES:
# 10. Ask for new remote URL
# 11. PUSHES A MIRROR OF LOCAL REPO TO NEW REMOTE
$RemoteRepo = Read-Host "URL TO REPO THAT NEEDS TO BE CLONE"
$CloneBranch = Read-Host "BRANCH TO CLONE"
$CloneFolderName = Read-Host "NEW FOLDER NAME"
git clone -b $CloneBranch $RemoteRepo
$NeedAllBranches = Read-Host "DO YOU NEED ALL BRANCHES? (YES OR NO)"
if ($NeedAllBranches -eq 'YES')
{
cd ((Get-Location).Path + "\" + $CloneFolderName)
foreach ($branch in (git branch -r))
{git checkout -b $branch.Trim().Replace("origin/","")}
$RemoveRemote = Read-Host "DO YOU NEED TO REMOVE REMOTE?"
if ($RemoveRemote -eq 'YES')
{
git remote remove origin
$AddNewRemote = Read-Host "DO YOU WANT TO ADD A NEW REMOTE AND PUSH?"
if ($AddNewRemote -eq 'YES')
{
$NewRemoteURL = Read-Host "URL TO NEW REMOTE"
git remote add origin $NewRemoteURL
git push --mirror
}
}
else {
Write-Host "NOT REMOVING REMOTE"
}
}
else {
Write-Host "NOT GETTING ALL VERSIONS"}
Upvotes: 0
Reputation: 1355
Well, a lot of answers. Mine will be one of the shortest. You want to push to a new server, but also to keep all the history and branches:
git clone --mirror https://git1.com/project/repo_name.git
cd repo_name
git branch
git remote set-url origin https://git2.com/project/repo_name.git
git push --mirror
Upvotes: 1
Reputation: 3234
If you want to duplicate your GitHub repository so that you have two identical repositories on GitHub:
git clone [existing-github-repo-url]
git remote add duplicate [new-github-repo-url]
git push duplicate master
Upvotes: 0
Reputation: 117615
git remote rename origin upstream
git remote add origin URL_TO_GITHUB_REPO
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 main
and for pulling patches from upstream git pull upstream main && git push origin main
, otherwise you will receive an error message.
Upvotes: 1082
Reputation: 149
The way I've accomplished this is:
cd old-repo/
on local machine and fetch all new changesgit push -u https://github.com/[username]/new-repo.git main -f
https://github.com/[username]/new-repo.git
to your local environmentI've found this a simple way to essentially copy an old remote repo into a new remote repo.
Upvotes: 4
Reputation: 15758
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.
Upvotes: 5
Reputation: 946
Visual studio 2022 and default git extension works flawlessly without even need for a single line of command.
Step 1: Go to git settings
Step 2: Add new origin pointing to different repository in git/azure
Step 3: Now you have option to push to new origin in different repository in git/azure
Now there is a new branch in new repository
Upvotes: 2
Reputation: 533
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
Upvotes: 9
Reputation: 1043
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
Upvotes: 3
Reputation: 538
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 repositorygit remote add origin urlrepository.git
To link with remote repositorygit remote -v
To confirm that it is linked to the remote repository3- 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!
Upvotes: 5
Reputation: 196
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
Upvotes: 16
Reputation: 6572
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
Upvotes: 77
Reputation: 23311
I found a solution using set-url which is concise and fairly easy to understand:
cd
into the existing repository on your local machine (if you haven't cloned it yet, then do this first)git remote set-url origin https://github.com/user/example.git
git push -u origin master
Upvotes: 37
Reputation: 813
Try this How to move a full Git repository
Create a local repository in the temp-dir directory using:
git clone temp-dir
Go into the temp-dir directory.
To see a list of the different branches in ORI do:
git branch -a
Checkout all the branches that you want to copy from ORI to NEW using:
git checkout branch-name
Now fetch all the tags from ORI using:
git fetch --tags
Before doing the next step make sure to check your local tags and branches using the following commands:
git tag
git branch -a
Now clear the link to the ORI repository with the following command:
git remote rm origin
Now link your local repository to your newly created NEW repository using the following command:
git remote add origin <url to NEW repo>
Now push all your branches and tags with these commands:
git push origin --all
git push --tags
You now have a full copy from your ORI repo.
Upvotes: 16
Reputation: 166919
To push your existing repo into different, you need to:
Clone the original repo first.
git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
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:
On the original repo, create and checkout a new branch:
git checkout -b new_branch
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.
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.
Upvotes: 81
Reputation: 2747
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 [email protected]/upstream-repository.git
cd upstream-repository.git
git push --mirror [email protected]/new-location.git
Upvotes: 22
Reputation: 785
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).
Upvotes: 2