GitHub - Source Repository Migations
I have been looking for a way to migrate a private Git repository to GitHub without losing commit history.
Using GIT Command Line Interface
Create an empty GitHub repository.
Clone the remote repository with the
--bare
option to obtain the clone with no working directory.1
git clone --bare https://<GIT_SERVER_FQDN>/<USER_NAME>/<REPO_NAME>.git
The
-- bare
option represents that a repository will be cloned with the project’s history, which can be pushed and pulled from but not directly modified.Push the locally cloned repository to GitHub using the
--mirror
option.1 2
cd <REPO_NAME>.git git push --mirror https://github.com/<USER_NAME>/<REPO_NAME>.git
This ensures that all the references to the cloned repository, such as
branches
andtags
, are pushed.
Using Shell Script
In order to facilitate any future needs of going through similar migrations I wrote a trivial shell script which bundles all the commands in the GIT CLI together into a single executable script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
read -p "Enter your orginial Git repository URL : " repo
read -p "Enter target GitHub repository URL : " newrepo
IFS='/'
read -a array <<< "$repo"
reponame = ${array[1]}
git clone --bare $repo
cd $reponame
echo $reponame
git push --mirror $newrepo
cd ..
rm -rf $repo
echo "Done!"