Activities

February 2014
M T W T F S S
« Jan   Jun »
 12
3456789
10111213141516
17181920212223
2425262728  

Quick steps to backup Git repository and restore the files

Git is a distributed version control system which help people to work on the same documents (often code) at the same time, and without stepping on each other’s toes.We can easily switch in between the branches and commit the update to central master repository.

GitHub is a most popular/known Git service providers like bitbucket. Git is mostly known by GitHub service over the web. As part of moving all GitHub repositories to my owned GitLab hosted Server, I need to take backup of all repositories prior to delete it from the Git account.

Backing up Git repositories

a. Method 1 [ Using –bare parameter]

Eg: git clone –bare git@github.com:company/test-repo.git

[root@rc090 git_test]# git clone --bare  git@github.com:company/Camper.git
Cloning into bare repository 'Camper.git'...
remote: Reusing existing pack: 20, done.
remote: Total 20 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (20/20), 2.78 MiB | 187.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.
[root@rc090 git_test]#

b. Method 2 [Using –mirror parameter]

Eg :git clone –mirror git@github.com:company/yutess.git

 [root@rc090 git_test]# git clone --mirror git@github.com:company/Camper.git
Cloning into bare repository 'Camper.git'...
remote: Reusing existing pack: 20, done.
remote: Total 20 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (20/20), 2.78 MiB | 398.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.
[root@rc090 git_test]#

How do I restore files from Git backup

1. Restoring Git backup locally

Suppose I have a backup “Camper.git” on /root/liju/ location and I want to restore the files on “/root/git-restore“. Default all the files under “Master” branch will be restored because Git uses “Master” branch by default for all actions.

Eg: #git clone file:///root/liju/Camper.git/ /root/git-restore/

[root@rc090 liju]# git clone file:///root/liju/Camper.git/ /root/git-restore/
Cloning into '/root/git-restore'...
remote: Counting objects: 19325, done.
remote: Compressing objects: 100% (3359/3359), done.
remote: Total 19325 (delta 14259), reused 19325 (delta 14259)
Receiving objects: 100% (19325/19325), 28.76 MiB | 43.63 MiB/s, done.
Resolving deltas: 100% (14259/14259), done.
[root@rc090 liju]#

2. Restoring specific branch name from Git backup

First we need to list the braches of existing Git repo. So we need to swtich to Git checkout folder.

[root@rc090 GitHub_bkp]# cd /root/git-restore/
[root@rc090 git-restore]# git branch
* master
[root@rc090 git-restore]# git branch -a
* master
  remotes/origin/API-Docs
  remotes/origin/HEAD -> origin/master
  remotes/origin/android_shop_owner
  remotes/origin/master
  remotes/origin/mobile-android
  remotes/origin/mobile-shop-owner
  remotes/origin/web-development
[root@rc090 git-restore]#

Now we go ahead with checkout the exact branch we needed,

Eg: git clone file:/// -b

[root@rc090 ~]# git clone file:///root/liju/Camper.git/ -b mobile-android  /root/git-restore/
Cloning into '/root/git-restore'...
remote: Counting objects: 2705, done.
remote: Compressing objects: 100% (801/801), done.
remote: Total 2705 (delta 1211), reused 2705 (delta 1211)
Receiving objects: 100% (2705/2705), 5.15 MiB | 0 bytes/s, done.
Resolving deltas: 100% (1211/1211), done.
[root@rc090 ~]# ls -la /root/git-restore/
.git/       Camper_ICE/

3. How do I restore GitHub backup to our own self hosted GitLab repository.

In this scenario, I’m going to restore my Git backup which took from GitHub and restore it on my GitLab server. This is quite simple process. Steps are,

1. Create a new project (MyGitApp) in Gitlab and get the git checkout url, user name and password.
2. Switch to the project folder where GitHub backup is copied (/root/liju/Camper.git). ie inside the git clone backup repo.
3. Push the entire files along with all changes send to newly created GitLab repository url (gitlab.serveridol.com).

[root@rc090 Camper.git]# pwd
/root/liju/Camper.git
[root@rc090 Camper.git]# git push --mirror http://gitlab.serveridol.com/company/mygitapp.git
Username for 'http://gitlab.serveridol.com': liju
Password for 'http://liju@github.rainconcert.intra':
Counting objects: 2705, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (801/801), done.
Writing objects: 100% (2705/2705), 5.15 MiB | 4.31 MiB/s, done.
Total 2705 (delta 1211), reused 2705 (delta 1211)
To http://gitlab.serveridol.com/company/mygitapp.git
 * [new branch]      API-Docs -> API-Docs
 * [new branch]      android_shop_owner -> android_shop_owner
 * [new branch]      master -> master
 * [new branch]      mobile-android -> mobile-android
 * [new branch]      mobile-shop-owner -> mobile-shop-owner
 * [new branch]      web-development -> web-development
[root@rc090 Camper.git]#

Git_restore

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>