最流行的开源版本管理软件,有高性能,分布式,简单易学的特点。
文档:https://git-scm.com/book/zh/v2
编译安装
ius.io也提供较新的rpm包
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.27.0.tar.xz
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.27.0.tar.sign
xz -d git-2.27.0.tar.xz
gpg --recv-keys 96AFE6CB
gpg --verify git-2.27.0.tar.sign git-2.27.0.tar
yum -y install make gcc gcc-c++
yum -y install autoconf curl-devel expat-devel gettext-devel openssl-devel perl-devel zlib-devel
tar xf git-2.27.0.tar
cd git-2.27.0
make configure
./configure --prefix=/usr/local/
make -j $(grep 'processor' /proc/cpuinfo |wc -l)
make install
git --version
基础配置
# 将家目录中.ssh/id_rsa.pub中的公钥设置到git平台。虽然也可以使用https协议,不过有时候总要输入密码,非常的麻烦。ssh可以测试连接
ssh-keygen -t rsa -b 2048
ssh -T git@github.com
# system/global/local分别对应系统全局和单个仓库,配置文件:/etc/gitconfig,~/.gitconfig, .git/config,后者覆盖前者
git config --global user.name yourname
git config --global user.email youremail@mail.com
git config --global core.autocrlf false
#git config --global https.proxy 'socks5://127.0.0.1:1080'
#git config --global --unset https.proxy
git config --global http.https://github.com.proxy 'socks5://127.0.0.1:1080'
# git://协议
#git config --global core.gitproxy "git-proxy"
#git config --global socks.proxy "socks5://127.0.0.1:1080"
# 查看所有配置
git config --list --show-origin
日常使用
日常位于工作区,添加修改到暂存区,提交到本地仓库,最后推送到远程仓库
# 添加到暂存区,-u仅添加modify的文件
git add xxx
git add -u
git add .
# feat新特性,fix修复,deps以来
git commit -m "feat: xxx"
# 推送到远程仓库
git push
# 拉取并合并
git pull
# 合并能保留原始提交,FF合并, 无需修改代码文件,仅移动HEAD指针
# 非FF,两个分支的末端提交和共同祖先做一个三方合并
git checkout -b feat
# 完成了修改
git add xxx && git commit -m "feat: xxx"
git checkout master && git merge feat && git branch -d feat
# 变基能避免分叉,便于发布的版本管理,在共同祖先上比对,然后以最新的提交为基底,依此应用分支的修改,最后FF
# 一般仅对未推送的提交变基,以免删除了别人依赖的提交
git rebase dev
git remote -v
# 修改远程仓库地址
git remote remove origin
git remote add origin xxx
# 推送并关联本地仓库
git push -u origin master
# 切换并新建分支
git checkout -b new
# 切换分支
git checkout dev
git branch -d dev
# 重置整个仓库
git reset --hard xxx
# 重置整个仓库,但不覆盖文件
git reset HEAD
# 重置单个文件
git checkout -- xxx
git log
# 对比提交的内容
git diff xxx
# 暂存未提交的内容
git stash
# 提取暂存的修改
git stash pop
# 附注标签
git tag -a v1.4 -m "version 1.4" hexdigest
# 轻量标签
git tag v1.4
git push origin v1.4
git tag -d v1.4
#git push origin :refs/tags/v1.4
git push origin --delete v1.4
# 分支历史
git log --oneline --decorate --graph
# 发布,避免http下载到.git目录的源码
git archive master --prefix='project/' --format=zip > project-$(date +%Y%m%d%H%M%S).zip
服务
四种协议,file,http,ssh,git。http有简单的和智能的,简单的仅能拉取不能推送,push后执行hooks/post-update钩子程序以便能拉取新的版本库
git协议没有授权机制,需要守护进程并监听9418端口
智能HTTP通过CGI脚本git-http-backend提供push等功能,通过htpasswd实现授权,通过git-daemon-export-ok判断是否提供匿名访问
常用组合:智能HTTPS、SSH推送加GIT或HTTP匿名拉取、本地FILE推送加HTTP匿名拉取
echo $(which git-shell) >> /etc/shells
useradd -s $(which git-shell) git
mkdir /srv/git && chown git /srv/git
su -l -s /bin/bash git
mkdir /srv/git/project.git && cd /srv/git/project.git
git init --bare
# 每个公钥前加上no-port-forwarding等避免运行shell命令,加在公钥ssh-rsa字符串前,并用空格分隔
(cd; umask 077; mkdir .ssh; echo -n 'no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ' >> .ssh/authorized_keys)
cat /tmp/bob_rsa.pub >> .ssh/authorized_keys
# 对于GitHub不同repo用不同的私钥,Host可以使用REPONAME.github.com,对应的远程origin地址中也是用该域名
cat >> .ssh/config <<EOL
Host 192.168.1.78
HostName 192.168.1.78
User git
IdentityFile ~/.ssh/projtct_rsa
IdentitiesOnly yes
EOL
git clone git@192.168.1.78:/srv/git/project.git
git协议匿名访问:
sudo -u git git daemon --reuseaddr --base-path=/srv/git/ /srv/git/
cd /srv/git/project.git
touch git-daemon-export-ok
GitWeb: 提供网页查看器,git instaweb --httpd=webrick
,需要ruby环境,HTTP监听于1234端口,也可以通过apache提供服务
Gitlab提供类似GitHub的开源git程序包