在服务器上创建一个git用户和用户组

sudo adduser git

根据提示设置对应的信息

设置git home目录的权限为git,否则将来无法访问

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

使用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$ 

ouyangxibao
189 声望163 粉丝

不生产代码,只是代码的搬运工