git原生私服之git&gitweb
环境:
CentOS7.2
git1.8
git server: 192.168.8.101
git client: 192.168.8.254
ssh协议(默认) ssh://
client端
公钥上传至server端
提示:通常git用户没有设置密码,也不允许login,因此需要ssh key验证
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q -b 2048
scp ~/.ssh/id_rsa.pub 192.168.8.101:/tmp
server端
一.安装git
yum -y install git
二.初始化git
1.创建用户
useradd -r -m git -s /usr/bin/git-shell
2.创建仓库目录
mkdir /opt/gitrepo
3.初始化git仓库
git init --bare --shared /opt/gitrepo
chown -R git: /opt/gitrepo
4.导入client ssh公钥
mkdir /home/git/.ssh
cat /tmp/id_rsa.pub >>/home/git/.ssh/authorized_keys
chown -R git: /home/git/
测试
clone远程仓库
git clone git@192.168.8.101:/opt/gitrepo
或者
git clone ssh://git@192.168.43.144/opt/gitrepo
root@router:a#git clone ssh://gitweb@192.168.43.144/opt/gitrepo
Cloning into 'gitrepo'...
remote: Counting objects: 181, done.
remote: Compressing objects: 100% (155/155), done.
remote: Total 181 (delta 15), reused 178 (delta 15)
Receiving objects: 100% (181/181), 634.48 KiB | 0 bytes/s, done.
Resolving deltas: 100% (15/15), done.
添加并提交代码
cd gitrepo
touch README
git add README
git commit -m 'comment'
添加远程主机仓库映射
git remote add gitrepo git@192.168.8.101:/opt/gitrepo
添加本地主机仓库映射
git remote add local /root/gitrepo
root@router:gitrepo#git remote add gitrepo git@192.168.8.101:/opt/gitrepo
root@router:gitrepo#git remote add python /root/git/python/
root@router:gitrepo#git remote -v show
gitrepo git@192.168.8.101:/opt/gitrepo (fetch)
gitrepo git@192.168.8.101:/opt/gitrepo (push)
origin git@192.168.8.101:/opt/gitrepo (fetch)
origin git@192.168.8.101:/opt/gitrepo (push)
python /root/git/python/ (fetch)
python /root/git/python/ (push)
配置提交人和邮箱等标识
git config --global color.ui true
git config --global user.name jlive
git config --global user.email liujun_live@msn.com
push代码
git push gitrepo master:master
root@router:gitrepo#git push gitrepo master:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 204
bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta0)
To git@192.168.8.101:/opt/gitrepo
* [new branch] master ->master
git协议 git://
git-daemon
https://git-scm.com/docs/git-daemon
一.安装
yum -y install git-daemon
二.配置并启动
a.命令行
/usr/libexec/git-core/git-daemon --base-path=/opt/git --export-all --syslog --enable=receive-pack --detach
默认监听在9418端口,参数详情man git-daemon
b.systemd
安装git-daemon后会多出两个systemd管理文件
/usr/lib/systemd/system/git.socket
/usr/lib/systemd/system/git@.service
默认交由xinetd服务管理,只需对应修改git@.service中的--base-path即可
systemctl start git.socket
root@router:gitrepo#netstat -tunlp|grep :94
tcp6
root@router:gitrepo#systemctl
status git.socket
● git.socket - Git Activation Socket
Loaded: loaded
(/usr/lib/systemd/system/git.socket; enabled; vendor preset:
disabled)
Active: active (listening) since Thu 2017-02-09 14:35:32
CST; 8min ago
Listen: [::]:9418
(Stream)
Accepted: 4; Connected: 0
Feb 09 14:35:32 router.example.com systemd[1]: Listening on Git Activation Socket.
Feb 09 14:35:32 router.example.com systemd[1]: Starting Git Activation Socket.
或者作为常规服务
cat >/lib/systemd/system/git.service <<HERE
[Unit]
Description=Git Repositories Server Daemon
Documentation=man:git-daemon(1)
After=syslog.target
After=network.target
[Service]
Type=simple
User=nobody
Restart=always
KillSignal=SIGQUIT
StandardError=syslog
NotifyAccess=all
ExecStart=/usr/libexec/git-core/git-daemon --base-path=/opt/git --export-all --syslog --enable=receive-pack
[Install]
WantedBy=multi-user.target
HERE
systemctl daemon-reload
systemctl start git
systemctl enable git
root@router:a#git clone git://192.168.43.144/gitrepo.git
Cloning into 'gitrepo'...
remote: Counting objects: 181, done.
remote: Compressing objects: 100% (155/155), done.
remote: Total 181 (delta 15), reused 178 (delta 15)
Receiving objects: 100% (181/181), 634.48 KiB | 0 bytes/s, done.
Resolving deltas: 100% (15/15), done.
root@router:gitrepo#git remote -v
origin
git://192.168.8.254/gitrepo.git (fetch)
origin
git://192.168.8.254/gitrepo.git (push)
root@router:gitrepo#touch README
root@router:gitrepo#git add README
root@router:gitrepo#git commit -m 'test git-daemon push'
[master 64c73f9] test git-daemon push
1 file changed, 0 insertions(+), 0
deletions(-)
create mode 100644 README
root@router:gitrepo#git push origin master:master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://192.168.8.254/gitrepo.git
a91beea..64c73f9 master -> master
http协议 http://
gitweb
示例站点
● git.socket - Git Activation Socket
Feb 09 14:35:32 router.example.com systemd[1]: Listening on Git Activation Socket.
Feb 09 14:35:32 router.example.com systemd[1]: Starting Git Activation Socket.
或者作为常规服务
cat >/lib/systemd/system/git.service <<HERE
[Unit]
Description=Git Repositories Server Daemon
Documentation=man:git-daemon(1)
After=syslog.target
After=network.target
[Service]
Type=simple
User=nobody
Restart=always
KillSignal=SIGQUIT
StandardError=syslog
NotifyAccess=all
ExecStart=/usr/libexec/git-core/git-daemon --base-path=/opt/git --export-all --syslog --enable=receive-pack
[Install]
WantedBy=multi-user.target
HERE
systemctl daemon-reload
systemctl start git
systemctl enable git
root@router:a#git clone git://192.168.43.144/gitrepo.git
Cloning into 'gitrepo'...
remote: Counting objects: 181, done.
remote: Compressing objects: 100% (155/155), done.
remote: Total 181 (delta 15), reused 178 (delta 15)
Receiving objects: 100% (181/181), 634.48 KiB | 0 bytes/s, done.
Resolving deltas: 100% (15/15), done.
root@router:gitrepo#git remote -v
origin
origin
root@router:gitrepo#touch README
root@router:gitrepo#git add README
root@router:gitrepo#git commit -m 'test git-daemon push'
[master 64c73f9] test git-daemon push
root@router:gitrepo#git push origin master:master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://192.168.8.254/gitrepo.git
gitweb
示例站点
https://git.strongswan.org/
一.安装gitweb
yum -y install gitweb lighttpd
二.启动gitweb
cd /opt/gitrepo
git instaweb --start
提示:gitweb实际上是perl cgi脚本,默认会提示安装lighttpd,配置文件无需做任何修改,直接启动即可。即使手动修改了lighttpd.conf,只要在git仓库目录下执行git instaweb,则始终会将lighttpd.conf还原到初始状态。默认监听在1234端口
[root@node1 gitrepo]# pwd
/opt/gitrepo
[root@node1 gitrepo]# ls
branches config description FETCH_HEAD gitweb HEAD hooks info objects refs
[root@node1 gitrepo]# ls gitweb/
gitweb_config.perl lighttpd lighttpd.conf tmp
三.整合apache httpd
1.安装gitweb,httpd
yum -y install gitweb httpd
2.配置httpd
/etc/httpd/conf.d/git.conf #gitweb安装后会自动创建,保持默认即可
3.配置gitweb
/etc/gitweb.conf #主配置文件
最简配置,只需修改projectroot这行
our $projectroot = "/opt";
提示: 可以装下13,做一个.git后缀的同名软链,这样客户端在clone的时候url是带.git的,而实际clone下来后是不带.git的,是不是有点github的感觉
mkdir /opt/git
ln -s /opt/gitrepo /opt/git/gitrepo.git
修改gitweb配置our $projectroot = "/opt/git";
更多帮助请参考
/usr/share/doc/gitweb-1.8.3.1/README
https://wiki.archlinux.org/index.php/Gitweb
https://git-scm.com/book/en/v2/Git-on-the-Server-GitWeb
https://review.openstack.org/Documentation/config-gitweb.html
https://git.wiki.kernel.org/index.php/Gitweb
4.重启httpd
systemctl restart httpd
4.重启httpd
systemctl restart httpd
错误解决:No projects found
注:gitweb默认情况下会列出projectroot下所有的git仓库,这里的projectroot是上文gitrepo的父目录。
如果设置为our $projectroot = "/opt/gitrepo";
浏览器访问时会提示
可参看/opt/gitrepo/gitweb/gitweb_config.perl
定制访问url
启用http协议
cd /opt/git/gitrepo.git
一.安装gitweb
yum -y install gitweb lighttpd
二.启动gitweb
cd /opt/gitrepo
git instaweb --start
提示:gitweb实际上是perl cgi脚本,默认会提示安装lighttpd,配置文件无需做任何修改,直接启动即可。即使手动修改了lighttpd.conf,只要在git仓库目录下执行git instaweb,则始终会将lighttpd.conf还原到初始状态。默认监听在1234端口
[root@node1 gitrepo]# pwd
/opt/gitrepo
[root@node1 gitrepo]# ls
branches config description FETCH_HEAD gitweb HEAD hooks info objects refs
[root@node1 gitrepo]# ls gitweb/
gitweb_config.perl lighttpd lighttpd.conf tmp
三.整合apache httpd
1.安装gitweb,httpd
yum -y install gitweb httpd
2.配置httpd
/etc/httpd/conf.d/git.conf #gitweb安装后会自动创建,保持默认即可
3.配置gitweb
/etc/gitweb.conf #主配置文件
最简配置,只需修改projectroot这行
our $projectroot = "/opt";
提示: 可以装下13,做一个.git后缀的同名软链,这样客户端在clone的时候url是带.git的,而实际clone下来后是不带.git的,是不是有点github的感觉
mkdir /opt/git
ln -s /opt/gitrepo /opt/git/gitrepo.git
修改gitweb配置our $projectroot = "/opt/git";
更多帮助请参考
/usr/share/doc/gitweb-1.8.3.1/README
https://wiki.archlinux.org/index.php/Gitweb
https://git-scm.com/book/en/v2/Git-on-the-Server-GitWeb
https://review.openstack.org/Documentation/config-gitweb.html
https://git.wiki.kernel.org/index.php/Gitweb
4.重启httpd
systemctl restart httpd
4.重启httpd
systemctl restart httpd
错误解决:No projects found
注:gitweb默认情况下会列出projectroot下所有的git仓库,这里的projectroot是上文gitrepo的父目录。
如果设置为our $projectroot = "/opt/gitrepo";
浏览器访问时会提示
可参看/opt/gitrepo/gitweb/gitweb_config.perl
定制访问url
启用http协议
cd /opt/git/gitrepo.git
mv hooks/post-update.sample hooks/post-update
实测git clone http://192.168.8.101/git/gitrepo.git时404,排错中
gitweb主题
cd /usr/local/src
git clone https://github.com/kogakure/gitweb-theme.git
cd gitweb-theme
./setup -vi -t /var/www/git --install
补充:
分支管理
创建分支
git branch mybranch-1.0
git branch mybranch-1.1
git branch mybranch-1.2
git branch deleteme
查看分支
git branch -v
root@router:python#git branch -v
* master
删除分支
git branch -d deleteme
或
git branch -D deleteme
root@router:python#git branch -d deleteme
Deleted branch deleteme (was 11c5c8d).
分支合并
如,将开发分支(develop),合并到稳定分支(master)
1.切换master分支
git checkout master
2.合并分支
git merge develop
有冲突会提示,git status查看冲突
解决冲突,git add或git rm将解决后的文件暂存
所有冲突解决后,git commit 提交更改
例如:将acc2f69提交合并到当前分支
git merge acc2f69
clone远程github分支
git clone -b release_branch https://github.com/foo
push远程分支到远程git仓库
git push origin mybranch-1.0
root@router:python#git push origin mybranch-1.0
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To git@github.com:liujun1990/python.git
查看远程仓库分支
git branch -a
root@router:python#git branch -a
* mybranch-1.0
删除远程仓库分支
git push origin :mybranch-1.0
root@router:python#git push origin :mybranch-1.0
To git@github.com:liujun1990/python.git
git客户端入门
1.git clone
将远程版本库克隆到本地
本地库名称可以省略,省略后在本地会生成一个和远程版本库名字相同的目录
2.git remote
管理远程主机名(man git-remote)
3.git fetch
将远程版本库中的更新,更新到本地库(man git-fetch)
语法: git fetch 远程主机名
默认情况下,git fetch origin将会更新远程主机origin上的所有分支,如果只想更新某个分支,则在主机名origin后面加分支名。如:git fetch origin master
4.git push
将本地分支的更新推送到远程主机(man git-push)
语法: git push 远程主机名 本地分支名:远程分支名
如果省略远程分支名,则表示将本地分支推送与存在最终关系的远程分支,如果远程分支不存在,则会被新建
如: git push origin master,表示将本地master分支推送到origin主机的master分支上
如果省略本地分子名,则表示要删除远程主机中分支,如git push origin :
master,则表示删除origin主机中master分支
5.git pull
获取远程分支中更新(man git-pull)
语法: git pull 远程主机名 远程分支:本地分支
如: git pull origin master:master,表示将远程主机origin中的master分支跟新到本地分支master