Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

119.8. 远程仓库

119.8.1. 查看远程地址

查看远程仓库

		
git remote show
origin
		
		

		
neo@MacBook-Pro-Neo ~> git remote -v
origin	git@192.168.30.5:netkiller.cn/www.netkiller.cn.git (fetch)
origin	git@192.168.30.5:netkiller.cn/www.netkiller.cn.git (push)			
			
		
		
neo@MacBook-Pro-M2 ~/netkiller (dev)> git remote get-url origin
ssh://git@gitlab.netkiller.cn/galaxy/ensd-fscs.git		
		
		

119.8.2. 显示远程地址

	
neo@MacBook-Pro-M2 ~/w/test (master)> git ls-remote --get-url origin
ssh://git@gitlab.netkiller.cn:/chenjingfeng/test.git	
	
		

119.8.3. 添加远程仓库

添加远程仓库

		
git remote add origin git@localhost:example.git
		
		

添加多个远程仓库

git remote add origin git@localhost:example.git
git remote add another https://gitcafe.com/netkiller/netkiller.gitcafe.com.git
git push origin master
git push another master
		

119.8.4. 修改 origin

		
git remote rename origin old-origin
		
		

修改远程仓库

			
git remote set-url origin git@gitlab.netkiller.cn:netkiller.cn/www.netkiller.cn.git
git remote set-url origin https://gitlab.netkiller.cn/netkiller.cn/www.netkiller.cn.git
			
		

119.8.5. 删除 origin

		
git remote remove origin		
		
		

删除远程仓库

		
git remote rm origin
		
		

119.8.6. 仓库共享

准备一台服务器

首先创建一个名为 git 用户

		
[git@netkiller ~]$ sudo adduser git
		
		

然后给 git 用户设置一个密码,当然你也可以不用密码,使用SSH证书登陆,将需要登陆 git 服务器的公钥证书添加到 .ssh/authorized_keys 文件

		
[git@netkiller ~]$ cat .ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDJi+vHwfOhm4hx2UWn5knfBe/k6d9K2aXOHhUVP43uSBlg8wmW/YVwoJo8B/XY3KqGFxxWyd1AVP6J273UBik= neo@MacBook-Pro-M2.local
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABlcFRWoBigBw7HwrszParVuJjP3wyJeOdNSevcgjG2ql985Vg3TXi2i2tNXQQeJGXwgWaDPsnowJtuGrnIXUw0bQa/iuBiMF40= neo@netkiller		
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABlcFRWoBigBw7HwPsnowJtuGrnMF40rszParVuJjP3wyJIXUw0bQa/iuBieOdNSevcgjG2ql985Vg3TXi2i2tNXQQeJGXwgWaD= neo@Macmini-M4.local	

[git@netkiller ~]$ ll .ssh/
total 16
-rw------- 1 git git 1145 Oct 10 11:19 authorized_keys
-rw------- 1 git git 2603 Oct 10 11:19 id_rsa
-rw------- 1 git git 1774 Oct 10 11:19 known_hosts
		
		

注意 authorized_keys 文件的权限

登陆 git 服务器

		
ssh git@git.netkiller.cn
		
		

创建一个共享仓库

		
[git@netkiller ~]$ sudo mkdir example.git
[git@netkiller ~]$ cd example.git
[git@netkiller example.git]$ sudo git --bare init
[git@netkiller example.git]$ Initialized empty Git repository in /home/git/example.git/
		
		

注意,与本地仓库不同,这里需要使用 --bare 选项。

创建分支,设置默认分支,这里使用 main 作为默认分支,当然你也可以使用 master

		
[git@netkiller example.git]$ git branch -m main
[git@netkiller example.git]$ git config --global init.defaultBranch main		
		
		

致辞 git server 部分已经完成

下面你可以测试一下 git 服务器的工作情况

		
neo@Neo-Mac-mini-M4 workspace % git clone git@git.netkiller.cn:example.git
Cloning into 'example'...
Warning: Permanently added 'git.netkiller.cn' (ED25519) to the list of known hosts.
warning: You appear to have cloned an empty repository.		
		
		

如果是已存在的git 仓库,可以使用 git remote add origin git@REMOTE_SERVER:example.git 修改

		
touch README
git add README
git commit -m 'first commit'
git remote add origin git@REMOTE_SERVER:example.git
git push origin master
		
		

119.8.7. git-daemon 服务器

119.8.7.1. git-daemon - A really simple server for git repositories

在/home/gitroot/ 上运行 git 守护进程

$ cd /home/gitroot
$ mkdir test.git
$ cd test.git
$ git --bare init --shared
Initialized empty shared Git repository in /home/gitroot/test.git/
	
	
git daemon --verbose --export-all --base-path=/home/gitroot --enable=receive-pack --reuseaddr
	
		

允许push,否则该仓库只能clone/pull

sudo git daemon --verbose --export-all --base-path=/home/gitroot --enable=upload-pack --enable=upload-archive --enable=receive-pack
	

或者增加配置项

$ git config daemon.receivepack true
$ git config --file config receive.denyCurrentBranch ignore
	

119.8.7.2. git-daemon-sysvinit

for a read-only repo:

	
$ sudo apt-get install git-daemon-sysvinit


$ dpkg -l git-daemon-sysvinit
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                   Version                  Architecture             Description
+++-======================================-========================-========================-==================================================================================
ii  git-daemon-sysvinit                    1:1.7.10.4-1ubuntu1      all                      fast, scalable, distributed revision control system (git-daemon service)


$ dpkg -L git-daemon-sysvinit
/.
/usr
/usr/share
/usr/share/git-core
/usr/share/git-core/sysvinit
/usr/share/git-core/sysvinit/sentinel
/usr/share/doc
/usr/share/doc/git-daemon-sysvinit
/usr/share/doc/git-daemon-sysvinit/copyright
/usr/share/doc/git-daemon-sysvinit/README.Debian
/etc
/etc/default
/etc/default/git-daemon
/etc/init.d
/etc/init.d/git-daemon
/usr/share/doc/git-daemon-sysvinit/changelog.Debian.gz
	
		

配置 /etc/default/git-daemon 文件

119.8.7.3. inet.conf / xinetd 方式启动

过程 119.2. git-daemon

  1. /etc/shells

    /etc/shells 最后一行添加 '/usr/bin/git-shell'

    $ grep git /etc/shells
    /usr/bin/git-shell
    			
  2. add new user 'git' and 'gitroot' for git

    you need to assign shell with /usr/bin/git-shell

    $ sudo adduser git --shell /usr/bin/git-shell
    $ sudo adduser gitroot --ingroup git --shell /bin/bash
    			

    /etc/passwd

    $ grep git /etc/passwd
    git:x:1001:1002:,,,:/home/git:/usr/bin/git-shell
    gitroot:x:1002:1002:,,,:/home/gitroot:/bin/bash
    			
  3. /etc/services

    $ grep 9418 /etc/services
    git             9418/tcp                        # Git Version Control System
    			
  4. /etc/inet.conf

    $ grep git /etc/inet.conf
    git     stream  tcp     nowait  nobody \
    /usr/bin/git-daemon git-daemon --inetd --syslog --export-all /home/gitroot
    			

    reload inetd

    $ sudo pkill -HUP inetd
    			
  5. xinetd

    目前的Linux逐渐使用xinetd.d替代inet.conf,如Redhat系列已经不再使用inet.conf, Ubuntu系列发行版已经不预装inet与xinetd

    $ apt-cache search xinetd
    globus-gfork-progs - Globus Toolkit - GFork Programs
    rlinetd - gruesomely over-featured inetd replacement
    update-inetd - inetd configuration file updater
    xinetd - replacement for inetd with many enhancements
    
    $ sudo apt-get install xinetd
    			

    /etc/xinetd.d/

    $ cat /etc/xinetd.d/git
    # default: off
    # description: The git server offers access to git repositories
    service git
    {
    	disable 		= no
    	type            = UNLISTED
    	port            = 9418
    	socket_type     = stream
    	protocol 		= tcp
    	wait            = no
    	user            = gitroot
    	server          = /usr/bin/git
    	server_args     = daemon --inetd --export-all --enable=receive-pack --reuseaddr --base-path=/home/gitroot
    	log_on_failure  += USERID
    }
    			

    reload xinitd

    $ sudo /etc/init.d/xinetd reload
    * Reloading internet superserver configuration xinetd                                       [ OK ]
    			

119.8.7.4. git-daemon-run

$ sudo apt-get install git-daemon-run
	

安装后会创建下面两个用户

$ cat /etc/passwd | grep git
gitlog:x:117:65534::/nonexistent:/bin/false
gitdaemon:x:118:65534::/nonexistent:/bin/false
	

git-daemon-run 包携带的文件

$ dpkg -L git-daemon-run
/.
/etc
/etc/sv
/etc/sv/git-daemon
/etc/sv/git-daemon/run
/etc/sv/git-daemon/log
/etc/sv/git-daemon/log/run
/usr
/usr/share
/usr/share/doc
/usr/share/doc/git-daemon-run
/usr/share/doc/git-daemon-run/changelog.gz
/usr/share/doc/git-daemon-run/changelog.Debian.gz
/usr/share/doc/git-daemon-run/README.Debian
/usr/share/doc/git-daemon-run/copyright
	

同时创建下面配置文件

$ find /etc/sv/git-daemon/
/etc/sv/git-daemon/
/etc/sv/git-daemon/run
/etc/sv/git-daemon/supervise
/etc/sv/git-daemon/log
/etc/sv/git-daemon/log/run
/etc/sv/git-daemon/log/supervise
	

编辑/etc/sv/git-daemon/run配置

	
$ sudo vim /etc/sv/git-daemon/run

#!/bin/sh
exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugitdaemon \
"$(git --exec-path)"/git-daemon --verbose --reuseaddr \
--base-path=/var/cache /var/cache/git
	
		

git-daemon --verbose --reuseaddr \
--base-path=/var/cache /var/cache/git

改为

git-daemon --verbose --reuseaddr \
--enable=receive-pack --export-all --base-path=/opt/git
	
[提示]提示

* 我加上了一个--export-all 使用该选项后,在git仓库中就不必创建git-daemon-export-ok文件。

其他选项--enable=upload-pack --enable=upload-archive --enable=receive-pack

/etc/services 文件中加入

# Local services
git             9418/tcp                        # Git Version Control System
	

确认已经加入

$ grep 9418 /etc/services
	

启动git-daemon

$ sudo sv stop git-daemon

or

$ sudo runsv git-daemon
runsv git-daemon: fatal: unable to change to directory: file does not exist
	

扫描git端口,确认git-daemon已经启动

$ nmap localhost

Starting Nmap 5.00 ( http://nmap.org ) at 2012-01-31 10:45 CST
Warning: Hostname localhost resolves to 2 IPs. Using 127.0.0.1.
Interesting ports on localhost (127.0.0.1):
Not shown: 989 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
22/tcp   open  ssh
53/tcp   open  domain
80/tcp   open  http
111/tcp  open  rpcbind
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
1723/tcp open  pptp
3128/tcp open  squid-http
3306/tcp open  mysql
9418/tcp open  git
	

119.8.7.5. Testing

	
$ sudo mkdir -p /opt/git/example.git
$ cd /opt/git/example.git
$ git init
$ sudo vim example.git/.git/config
[receive]
denyCurrentBranch = ignore

$ sudo chown gitdaemon -R /opt/git/*
$ touch git-daemon-export-ok
	
		

.git/config 文件应该是下面这样

$ cat example.git/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true

[receive]
denyCurrentBranch = ignore
	

git-clone git://localhost/example.git

	
neo@deployment:/tmp$ git clone git://localhost/example.git example.git
Cloning into example.git...
warning: You appear to have cloned an empty repository.
neo@deployment:/tmp$ cd example.git/
neo@deployment:/tmp/example.git$ echo helloworld > hello.txt
neo@deployment:/tmp/example.git$ git add hello.txt
neo@deployment:/tmp/example.git$ git commit -m 'Initial commit'
[master (root-commit) 65a0f83] Initial commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
	
		

我们添加了一些文件 push 到服务器

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://localhost/example.git
* [new branch]      master -> master
	

然后再git clone,可以看到文件数目

	
$ git-clone git://localhost/example.git
Cloning into example...
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.