I have a server that I'm taking down. The only thing I have left to migrate is my repository. This server is listed as the origin (master) for one of my projects. What is the proper way to move the repository to keep the history.
To add the new repo location,
git remote add new_repo_name new_repo_url
Then push the content to the new location
git push new_repo_name master
Finally remove the old one
git remote rm origin
After that you can do what bdonlan said and edit the.git/config file to change the new_repo_name to origin. If you don't remove the origin (original remote repository), you can simply just push changes to the new repo with
git push new_repo_name master
git push -u new_repo_name --all
. –
Ghats git remote rename new_repo_name origin
–
Tablecloth git remote rm
does not delete the remote repository from the server. It simply removes the remote and its references from your local repository. –
Forethoughtful git init --bare new_repo_name.git
. Then I followed your instructions and it worked perfectly. Without that, when I tried to push
I got fatal: 'new_repo_name.git' does not appear to be a git repository
fatal: Could not read from remote repository.
You might want to suggest this in your answer. –
Derekderelict git remote rm origin
git remote add origin new_repo_url
git push -u origin --all
Note, as @the-new-mr said, this solution works only for local branches. –
Winebaum Updated to use git push --mirror origin
instead of git push -f origin
as suggested in the comments.
This worked for me flawlessly.
git clone --mirror <URL to my OLD repo location>
cd <New directory where your OLD repo was cloned>
git remote set-url origin <URL to my NEW repo location>
git push --mirror origin
I have to mention though that this creates a mirror of your current repo and then pushes that to the new location. Therefore, this can take some time for large repos or slow connections.
See also the receipt in section "Extra" of How to move a git repository with history | Atlassian Git Tutorial.
git push --mirror origin
over -f
. –
Quill 2.17.1
using git push -f origin
pushes only the working directory's current branch. @Quill 's recommendation for git push --mirror origin
worked for me, pushed all branches, tags, history, etc. –
Cowcatcher If you want to migrate all branches and tags you should use the following commands:
git clone --mirror [oldUrl]
to clone the old repo with all branches
cd the_repo
git remote add remoteName newRepoUrl
to setup a new remote
git push -f --tags remoteName refs/heads/*:refs/heads/*
to push all refs under refs/heads (which is probably what you want)
--tags
and refs/heads/*:refs/heads/*
are not compatible with --mirror
. https://mcmap.net/q/89509/-how-to-migrate-git-repository-from-one-server-to-a-new-one worked well. –
Hernandes refs/heads/refs/heads
using git push -f --tags remoteName refs/heads/*:refs/heads/*
so I have swapped to git push remoteName --mirror
–
Bohs push
also supports --mirror
. git clone --mirror; cd repo; git push --mirror new_remote
should do the trick –
Gluten Copy it over. It's really that simple. :)
On the client side, just edit .git/config in the client's local repo to point your remotes to the new URL as necessary.
This is sort of done in parts in some of the other answers.
git clone --mirror git@oldserver:oldproject.git
cd oldproject.git
git remote add new git@newserver:newproject.git
git push --mirror new
--mirror
in the push is much important: this should be the correct answer –
Salaidh git init --bare
–
Jameljamerson I'm just reposting what others have said, in a simple to follow list of instructions.
Move the repository: Simply login to the new server,
cd
to the parent directory where you now want to hold the repository, and usersync
to copy from the old server:new.server> rsync -a -v -e ssh [email protected]:path/to/repository.git .
Make clients point to the new repository: Now on each client using the repository, just remove the pointer to the old origin, and add one to the new one.
client> git remote rm origin client> git remote add origin [email protected]:path/to/repository.git
git remote set-url origin [email protected]:path/to/repository.git
–
Sportsmanship Take a look at this recipe on GitHub: https://help.github.com/articles/importing-an-external-git-repository
I tried a number of methods before discovering git push --mirror
.
Worked like a charm!
git clone --mirror ...
, git remote add ...
, git push --mirror ...
–
Marquet I followed the instructions on BitBucket to move a repo with all its branches there. Here come the steps with explanations following the #
character:
cd path/to/local/repo
git remote remove origin # to get rid of the old setting, this was not in the BitBucket instructions
git remote add origin ssh://[email protected]/<username>/<newrepo> # modify URL as needed
git push -u origin --all # pushes _ALL_ branches in one go
git push -u origin --tags # pushes _ALL_ tags in one go
Worked nicely for me.
Please follow the steps:
git remote add new-origin <GIT URL>
git push --all new-origin
git push --tags new-origin
git remote rm origin
git remote rename new-origin origin
This is a variation on this answer, currently suggested by gitlab to "migrate" a git repository from one server to another.
Let us assume that your old project is called
existing_repo
, stored in aexisting_repo
folder.Create a repo on your new server. We will assume that the url of that new project is
git@newserver:newproject.git
Open a command-line interface, and enter the following:
cd existing_repo git remote rename origin old-origin git remote add origin git@newserver:newproject.git git push -u origin --all git push -u origin --tags
The benefits of this approach is that you do not delete the branch that corresponds to your old server.
Should be as simple as:
git remote set-url origin git://new.url.here
This way you keep the name origin
for your new repo - then push to the new repo the old one as detailed in the other answers. Supposing you work alone and you have a local repo you want to mirror with all your cruft in it, you might as well (from inside your local repo)
git push origin --mirror # origin points to your new repo
but see Is "git push --mirror" sufficient for backing up my repository? (in all don't use --mirror
but once).
follow these instructions If you want to keep all the commits and branches from old to new repo
git clone --bare <old-repo-url>
cd <old-repo-directory>
git push --mirror <new-repo-url>
You can use the following command :
git remote set-url --push origin new_repo_url
Example from http://gitref.org/remotes/
$ git remote -v
github [email protected]:schacon/hw.git (fetch)
github [email protected]:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/github/git-reference.git (push)
$ git remote set-url --push origin git://github.com/pjhyett/hw.git
$ git remote -v
github [email protected]:schacon/hw.git (fetch)
github [email protected]:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/pjhyett/hw.git (push)
You can use git-copy to duplicate the repo with all histories.
git copy http://a.com/old.git http://a.com/new.git
If you want to move from one origin to another and also keep a backup of your current origin on your local machine you could use these steps:
- First locally go to the (git)folder you want to move over
- Create the new repository online This step creates a repository where we can push code to
Now in the folder do
git remote get-url origin
The above command gives the current remote origin url, useful to set the origin back to in the last step
git remote set-url origin [email protected]:folder/newrepo.git
The above command sets the remote origin to the new location
git push --set-upstream origin develop
The above command pushes the current active local branch to remote with branchname develop. Of course it preserves all history as with git all history is also pushed.
git remote set-url origin <original old origin>
The above command sets back the remote origin to your current origin: you want this because you are in your existing folder and you probably do not want to mix up your current local folder name with the new folder you are going to create for cloning the repo you just pushed to.
Hope this helps,
If you want to migrate a #git repository from one server to a new one you can do it like this:
git clone OLD_REPOSITORY_PATH
cd OLD_REPOSITORY_DIR
git remote add NEW_REPOSITORY_ALIAS NEW_REPOSITORY_PATH
#check out all remote branches
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
git push --mirror NEW_REPOSITORY_PATH
git push NEW_REPOSITORY_ALIAS --tags
All remote branches and tags from the old repository will be copied to the new repository.
Running this command alone:
git push NEW_REPOSITORY_ALIAS
would only copy a master branch (only tracking branches) to the new repository.
I have this bash script written that works as a breeze to migrate my git repo from one server ( one vender , repo hosted in bitbucket ) to another server ( repo hosted in gitlab or local git hosting ) with this command like tool , I only have to authenticate to the new git server where I want to migrate the code from the old repo server.
#!/bin/bash
# Default values
source_server=""
destination_server=""
source_repository=""
destination_repository=""
# Parse command-line options
OPTS=$(getopt -o "s:d:S:D:" --long source:,destination:,source_repository:,destination_repository: -n 'git-migration' -- "$@")
if [ $? != 0 ]; then
echo "Failed to parse command-line options. Exiting..."
exit 1
fi
eval set -- "$OPTS"
# Handle command-line options
while true; do
case "$1" in
-s | --source)
shift
source_server="$1"
shift
;;
-d | --destination)
shift
destination_server="$1"
shift
;;
-S | --source_repository)
shift
source_repository_link="$1"
shift
;;
-D | --destination_repository)
shift
destination_repository_link="$1"
shift
;;
--)
shift
break
;;
esac
done
# Check if required options are provided
if [[ -z "$source_server" || -z "$destination_server" || -z "$source_repository_link" || -z "$destination_repository_link" ]]; then
echo "Missing required options. Please provide source server, destination server, and source_repository and destination_repository. Exiting..."
echo "syntax : migrationtool --source <source_foldername> --destination <destination_foldername> --source_repository <source_Repo_link> --destination_repository <destination_repo_link>"
echo "Any one option missing will fail the program in the initial phase itself with returncode 121"
exit 121
fi
#clone the repo first
git clone $source_repository_link
#list all the branches that are present in the current old repository
cd $source_server
git branch -a
# save the list of the branch names into a txt file for further iteration
git branch -a | sed s/'remotes\/origin\/'// | grep -v HEAD | grep -v "*" > ../git_branches.txt
# Now we have to checkout each branch to load the data on the folder, in order to see the branch name
# when we give the command "git branch" this loads only the branches that are checkout mostly
# for this reason this next iteration step is useful
for i in `cat ../git_branches.txt `;do git checkout $i ;done
# fetch all the tags in the old repository
git fetch --tags
# this shows all the branch and tags that we have fetched for us.
echo "**********All the tags**********"
git tag
echo "**********All the branches ***********"
git branch -a
# now if your New repository on the other vendor like github is going to be different then you have to
# rename the folder name to sync up with repository name that you have created in the github
# mostly the name of the repository is going to be same but here I can use the passing of variable
if [[ -z $destination_server ]];
then
cd ..
mv $source_server $destination_server
cd $destination_server
fi
# remove the origin from this folder, here only the directory name is matched wrt to the new repository
# but the link to the old repo still exists that we are going to break
git remote rm origin
git remote add origin $destination_repository_link
#check the output of the new vender git link
echo "******* new github vendor link for the project*******"
git remote -v
echo ""
echo ""
# once the origin of the new Git repo is added successfully, then we can push the changes to the new
# repository vendor
git push origin --all
git push --tags
## cleanup should be there
cd ..
rm -rf $source_server
rm -rf $destination_server
rm -rf git_branches.txt
Usage : cp script.sh /usr/local/migrationtool
migrationtool --source old_Git_Repo --source_repository https://gitlab.com/developer123/old_Git_Repo.git
--destination_repository https://github.com/developer123/new_git_Repo.git --destination new_git_Repo
Order of the parameters passed is not required to be correct , just have to make sure that the correct arguments are passed with the parameters.
Remark:
git copy http://a.com/old.git http://a.com/new.git
works only for e.g. a github to github copy, i.e. remaining on the same git system. When copying from e.g. github to gerrit, this does not work.
Besides that, it's quite comfortable, as it copies branches, tags, and submodules automatically.
git: 'copy' is not a git command. See 'git --help'.
–
Brana © 2022 - 2024 — McMap. All rights reserved.