構建git項目本地鏡像

在服務器上建立一個git用戶和用戶組

sudo adduser gitgit

根據提示設置對應的信息github

設置git home目錄的權限爲git,不然未來沒法訪問

sudo chown -R git:git /home/git/shell

使用git clone --mirror 將github上的項目克隆到本地:

# 建立一個gitmirror目錄,用來作鏡像中轉目錄
git@localhost:~$ mkdir gitmirror
git@localhost:~$ cd gitmirror/
# 在githua上添加本地ssh公鑰
git@localhost:~/gitmirror$ cat ~/.ssh/
id_rsa       id_rsa.pub   known_hosts  
git@localhost:~/gitmirror$ cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDF5picVZ1nut052WI2RHGFxVVkTdcU3KYejxKBrclxwB/XG6KcLcES7lMkvJngWRPzCWElJVYrfHAAUkM/El6TwoJsO3/D8TK+FEfeVsTKaRC2+TYoYLiKdOCyH75LJ8zZYJYDJvC51WOBlEWWBD3C0mG33mFmCupXvUXlbrnGOJWRTnPWe98+oY/a2inxZkNrMfNN5leKzoOtx4tX+/26IccGAroALA+sDHCrDZZ89Yy5Jlmv4K4FuG5w9LXHQBCTUYPrQfBhnTwwLGQg8ImU4WixPjHeBK97XeE4SS8EYx6K7Z3UBBuSS3kEadminfi6R+m1Z0IeOoTe2ZrLadminUEAhL git@localhost
git@localhost:~/gitmirror$ 
#攜帶--mirror選項將項目克隆到本地
git@localhost:~/gitmirror$ git clone --mirror git@github.com:ouyangxb/study-rebase.git
Cloning into bare repository 'study-rebase.git'...
Warning: Permanently added the RSA host key for IP address '140.82.113.4' to the list of known hosts.
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 9 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (9/9), done.
Checking connectivity... done.
git@localhost:~/gitmirror$

建立咱們須要鏡像的git項目的目錄,並將其初始化爲倉庫:

git@localhost:~/repos$ mkdir study-rebase.git
git@localhost:~/repos$ cd study-rebase.git/
git@localhost:~/repos/study-rebase.git$ git init --bare       
Initialized empty Git repository in /home/git/repos/study-rebase.git/
git@localhost:~/repos/study-rebase.git$

設置項目的本地Repo

#設置項目本地Repo地址
git@localhost:~/gitmirror/study-rebase.git$ git remote set-url --push origin git@172.17.0.1:/home/git/repos/study-rebase.git
#將項目推送到本地Repo
git@localhost:~/gitmirror/study-rebase.git$ git push --mirror 
git@172.17.0.1's password: 
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (9/9), 1.08 KiB | 0 bytes/s, done.
Total 9 (delta 0), reused 9 (delta 0)
To git@172.17.0.1:/home/git/repos/study-rebase.git
 * [new branch]      dev -> dev
 * [new branch]      master -> master
git@localhost:~/gitmirror/study-rebase.git$

測試

admin@localhost:~$ git clone git@172.17.0.1:/home/git/repos/study-rebase.git
Cloning into 'study-rebase'...
git@172.17.0.1's password: 
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
Checking connectivity... done.
admin@localhost:~$

同步github上的遠程repo的變化到本地repo

修改github上的repo信息

git@localhost:~$ git clone git@github.com:ouyangxb/study-rebase.git
Cloning into 'study-rebase'...
Warning: Permanently added the RSA host key for IP address '140.82.114.3' to the list of known hosts.
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 9 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (9/9), done.
Checking connectivity... done.
git@localhost:~$ 
git@localhost:~$ 
git@localhost:~$ cd study-rebase
git@localhost:~/study-rebase$ 
git@localhost:~/study-rebase$ ls
README.md
git@localhost:~/study-rebase$ 
# 修改README.md文件,提交一個修改記錄到遠程github上
git@localhost:~/study-rebase$ vim README.md 
# study-rebase
study rebase
git server

~
git@localhost:~/study-rebase$ 
git@localhost:~/study-rebase$ git add .
git@localhost:~/study-rebase$ git commit -m "study git mirror"                
[master c5c3bac] study git mirror
 1 file changed, 2 insertions(+)
git@localhost:~/study-rebase$ 
git@localhost:~/study-rebase$ 
git@localhost:~/study-rebase$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:ouyangxb/study-rebase.git
   811986a..c5c3bac  master -> master
git@localhost:~/study-rebase$ 
git@localhost:~/study-rebase$ git log
commit c5c3bac6758440d64b9151d1e138d8131d023aaf
Author: Your Name <you@example.com>
Date:   Sun Dec 8 02:24:05 2019 -0500

    study git mirror

commit 811986a7a427e51e8519505525000904a626802c
Author: ouyangxb <ouyangxb@163.com>
Date:   Wed Nov 27 18:08:00 2019 +0800

    Initial commit
git@localhost:~/study-rebase$

同步到本地repo中轉目錄,而後同步到本地Repo

git@localhost:~/gitmirror/study-rebase.git$ 
git@localhost:~/gitmirror/study-rebase.git$ git fetch -p origin 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:ouyangxb/study-rebase
   811986a..c5c3bac  master     -> master
git@localhost:~/gitmirror/study-rebase.git$ git push --mirror 
git@172.17.0.1's password: 
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.17.0.1:/home/git/repos/study-rebase.git
   811986a..c5c3bac  master -> master
git@localhost:~/gitmirror/study-rebase.git$

查看本地Repo是否有更新

git@localhost:~/gitmirror/study-rebase.git$ git log
commit c5c3bac6758440d64b9151d1e138d8131d023aaf
Author: Your Name <you@example.com>
Date:   Sun Dec 8 02:24:05 2019 -0500

    study git mirror

commit 811986a7a427e51e8519505525000904a626802c
Author: ouyangxb <ouyangxb@163.com>
Date:   Wed Nov 27 18:08:00 2019 +0800

    Initial commit
git@localhost:~/gitmirror/study-rebase.git$

本地工做區更新

admin@localhost:~/study-rebase$ git remote -v
origin  git@172.17.0.1:/home/git/repos/study-rebase.git (fetch)
origin  git@172.17.0.1:/home/git/repos/study-rebase.git (push)
admin@localhost:~/study-rebase$ git pull
git@172.17.0.1's password: 
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 172.17.0.1:/home/git/repos/study-rebase
   811986a..c5c3bac  master     -> origin/master
Updating 811986a..c5c3bac
Fast-forward
 README.md | 2 ++
 1 file changed, 2 insertions(+)
admin@localhost:~/study-rebase$ git log
commit c5c3bac6758440d64b9151d1e138d8131d023aaf
Author: Your Name <you@example.com>
Date:   Sun Dec 8 02:24:05 2019 -0500

    study git mirror

commit 811986a7a427e51e8519505525000904a626802c
Author: ouyangxb <ouyangxb@163.com>
Date:   Wed Nov 27 18:08:00 2019 +0800

    Initial commit
admin@localhost:~/study-rebase$
相關文章
相關標籤/搜索