git学习笔记
配置信息
配置文件
/etc/gitconfig # 所有用户
~/.gitconfig # 当前用户
.git/config # 当前项目
设置个人信息
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
文本编辑器
git config --global core.editor emacs
差异分析工具
git config --global merge.tool vimdiff
查看配置信息
git config --list
查看指定变量
git config user.name
帮助信息
git help <verb>
git <verb> --help
man git-<verb>
例如
git help config
忽略文件模式
git config core.filemode 查看
git config core.filemode false 设置为false以后 git不在控制文件权限
需要需改文件权限的时候使用如下命令
git add --chmod=+x index.php
git update-index --chmod=+x index.php
本地忽略文件改变
git update-index --assume-unchanged FILE # 添加忽略文件改变
git update-index --no-assume-unchanged FILE # 取消忽略文件改变
基本命令
克隆仓库
git clone https://github.com/dotcoo/sharpphp.git
git clone https://github.com/dotcoo/sharpphp.git sharp
初始化新仓库
git init
文件进行跟踪
git add *.c
git add README
检查文件状态
git status
Untracked 未监听
Unmodified 未修改
Modified 已修改
Staged 已存储
忽略文件或目录
vim .gitignore
# 此为注释 – 将被 Git 忽略
*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
提交更新
git commit -m 'initial project version' # 说明
git commit -a -m '提交说明'
查看提交历史
# 所有历史记录
git log
# 查看指定文件的修改记录
git log -- file
# -p 显示详细内容 -2 最近两次
git log -p -2
# 单行显示
git log --pretty=oneline
修改最后一次的提交 增量修改
# 提交
git commit -m 'initial commit'
# 发现漏掉了某个文件 或者 又修改了几处错误
git add forgotten_file
# 重新填写提交说明
git commit --amend
# 如果已经push到服务器上 强制更新
git push origin HEAD --force
删除已经放置到暂存区的文件
git restore --staged <file>... 只从暂存区恢复文件
git reset head index.php
git reset head .
取消对文件的修改
git restore <file>... 从暂存区恢复文件 没有就从HEAD恢复
git checkout -- index.php
git checkout -- .
取消commit提交
git reset --soft <commit_id>
git push origin HEAD --force
reset参数
git reset主要有--soft --mixed --hard 三种方式
--soft 这个就是除了git commit 还原外其他的都保留,比如说git status 里面的内容和代码的修改
--mixed 这个就是 除了代码修改保留外其他的都还原, 包括git commit 和 git status 里面的内容
--hard 这个就将所有的都还原,包括代码,git add 后的内容以及 git commit 里面的内容
取消跟踪
git rm -r --cached PATH
忽略跟踪 恢复跟踪
git update-index –assume-unchanged PATH
git update-index –no-assume-unchanged PATH
查看远程仓库
git remote -v
查看远程仓库
git remote show origin
添加远程仓库
git remote add origin https://github.com/dotcoo/sharpphp.git
删除远程仓库
git remote remove beta
从远程仓库抓取数据
git fetch origin master
# 只是抓取服务器数据 不会合并到当前分支
将远端分支自动合并到本地仓库中当前分支
git pull origin master
比对更新
git diff # 比较工作目录和暂存区快照之间的差异
git diff --staged # 比较工作目录和上次提交的快照的差异
移除文件
rm dir.txt
git rm dir.txt
git rm dir.txt -f
git rm --cached readme.txt # 从已存储中删除
git rm log/\*.log
移动文件
git mv file_from file_to
查看提交历史
git log
git log -p -2 # -p查看每次提交的差异 -2显示最近两次
git log --pretty=oneline # 有关文件
显示简要的增改行数统计
git log --stat # 显示增改行数统计
历史记录格式自定义
git log --pretty=format:"%h - %an, %ar : %s"
选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明
显示所有的标签
git tag
git tag -n
git tag -l 'v1.1.*'
创建标签
git tag -a v0.1 -m 'v0.1 2014-01-01 上线版本'
git tag v0.1 # 轻量级标签
后期加注标签
git log --pretty=oneline
git tag -a v1.2 9fceb02
git tag -a v1.2 -m "new message" -f # 修改tag
GPG私钥来签署标签
git tag -s v1.5 -m 'my signed 1.5 tag'
验证标签
git tag -v v1.4.2.1
分享标签
# 推送单个标签
git push origin v1.5
# 推送所有标签
git push origin --tags
分支
查看分支
git branch
git branch -v
git branch -a # 所有分支 包括远程分支
创建分支
# 根据当前分支创建新分支
git checkout -b openapi
# 同下
git branch openapi
git checkout openapi
切换分支
git checkout openapi
git checkout origin/openapi
合并分支 合并其他分支到当前分支
git checkout master
git merge openapi
删除分支
git branch -d openapi
遇到冲突时的分支合并
git merge openapi
# 提示有文件未合并 有冲突
git status # 查看同冲突如下
# unmerged: openapi.php
# 解决冲突
git mergetool
vim openapi.php
# 查看冲突是否解决
git status
# 将解决冲突的文件 添加到暂存区
git add openapi.php
# 提交
git commit -am 'asdf'
查看与当前分支合并的分支
git branch --merged
查看没有与当前分支合并的分支
git branch --no-merged
推送数据到远程仓库
# 推送当前分支到origin主机的master分支
git push
# 推送openapi分支到origin主机的openapi分支 没有就创建分支
git push origin openapi
# 推送openapi分支到origin主机的openapi分支
git push origin openapi:api
获取远程数据更新 并合检出为本地分支
git fetch origin
git checkout -b serverfix origin/serverfix # 跟踪分支
git checkout --track origin/serverfix # 上一句的简写
# 跟踪分支中使用git push和git pull会默认远程对应的分支
删除远程分支
git push origin :serverfix # 非常搞笑的语法 但是能用
分支的衍合
相当于将一个分支serverfix做成另一个分支master的补丁
服务器上的git
创建git总账用户
useradd -s /usr/bin/git-shell git
修改git目录权限
chmod -R o-rwx ~git
chmod o+x ~git
使用已有的git仓库创建服务器的git仓库
# 坑啊,低版本会在当前目录初始仓库,另外建立一个blog.git的空目录
git --bare init blog.git
# 坑啊,高版本提示blog仓库不存在
git clone --bare blog blog.git
或将本地已有的git仓库复制到服务器中
scp -r blog.git git@193.168.1.101:
项目仓库创建组
groupadd gitblog
修改仓库的所属组
chgrp -R gitblog blog.git
修改仓库的权限
chmod -R g+wXs,o-rwx blog.git/
创建git用户
useradd -G gitblog -s /usr/bin/git-shell dotcoo
useradd -G gitblog -s /usr/bin/git-shell ken
克隆库到本地
git clone dotcoo@192.168.1.101:/home/git/blog.git
添加SSH公钥 无密码提交
~/.ssh/authorized_keys
提交回滚 提交撤销
一不留神把敏感数据提交了,如下步骤解决
使用git log查看需要混滚到的commit id
commit 022903d8c6e7c797118bd8c38502b76d5a0f007a
Author: dotcoo <dotcoo@dotcoo.com>
Date: Tue Jun 14 21:12:08 2016 +0800
MPClient
commit f4e0a1c9941205a4564943b73749898c67412edc
Author: dotcoo <dotcoo@dotcoo.com>
Date: Tue Jun 14 21:07:44 2016 +0800
修改交易页面连接
回滚代码
回滚 commit提交
git reset --soft f4e0a1c9941205a4564943b73749898c67412edc
回滚 commit提交 index暂存区
git reset --mixed f4e0a1c9941205a4564943b73749898c67412edc
回滚 commit提交 index暂存区 work工作区代码
git reset --hard f4e0a1c9941205a4564943b73749898c67412edc
然后将结果push到origin,并删除远程的代码
git push origin HEAD --force
使用代理
git -c http.proxy=socks5://127.0.0.1:1080 clone https://github.com/dotcoo/sharpphp.git
git -c core.gitproxy=socks5://127.0.0.1:1080 clone git@github.com:dotcoo/sharpphp.git
git config --global http.proxy socks5://127.0.0.1:1080
git config --global core.gitproxy socks5://127.0.0.1:1080
windows cmd 中文乱码
在换将变量中设置, LESSCHARSET 和 LANG 连个环境变量, 然后重新打开 cmd.
LESSCHARSET=utf-8
LANG=zh_CN.UTF-8
自动部署
添加git挂钩,创建/home/git/blog.git/hooks/update文件,并添加可执行权限
#!/bin/sh
echo dotcoo1 $1 $2 $3 | nc 192.168.1.99 7070
监听git push动作,创建/home/git/blog.git/hooks/update-checkout文件
示例1
#!/bin/sh
cd /www/web/test.com/public_html/blog
nc -k -l 7070 | while read refname oldrev newrev ; do
echo `date "+%Y-%m-%d %H:%M:%S"` $refname $oldrev $newrev >> /tmp/git.log
git fetch >> /tmp/git.log
git checkout origin/develop >> /tmp/git.log
done
示例2
#!/bin/sh
cd /home/wwwroot/www.test.com
nc -k -l 7070 | while read refname oldrev newrev ; do
echo `date "+%Y-%m-%d %H:%M:%S"` $refname $oldrev $newrev &>> /tmp/git.log
git pull &>> /tmp/git.log
chown -R www:www . &>> /dev/null
done
示例3
#!/bin/sh
LOG_FILE=/tmp/gitupdate.log
nc -k -l 7070 | while read repo refname oldrev newrev ; do
case $repo in
dotcoo1 ) cd /home/wwwroot/dotcoo1.com ;;
dotcoo2 ) cd /home/wwwroot/dotcoo2.com ;;
dotcoo3 ) cd /home/wwwroot/dotcoo3.com ;;
* ) continue ;;
esac
echo `date "+%Y-%m-%d %H:%M:%S"` $repo $refname $oldrev $newrev &>> $LOG_FILE
git pull &>> $LOG_FILE
chown -R www:www . &>> $LOG_FILE
echo -e "\n\n" &>> $LOG_FILE
done
启动监听进程
Centos 6 /etc/rc.d/rc.local
/bin/su www -s /bin/bash /home/wwwroot/gitupdate.sh
Centos 7 /usr/lib/systemd/system/gitupdate.service
[Unit]
Description=Git Auto Update
After=syslog.target
After=network.target
[Service]
Type=simple
ExecStart=/bin/su www -s /bin/bash /home/wwwroot/gitupdate.sh
Restart=always
[Install]
WantedBy=multi-user.target
Centos 7 serivce 启用
systemctl enable gitupdate
Centos 7 serivce 修改后 重新载入
systemctl daemon-reloa
GitLab 自动部署
找到git资源目录, 在目录下创建 custom_hooks
目录, 然后创建 post-receive
文件. 内容如下:
#!/bin/bash
while read oldver newver branch; do
echo `date "+%Y-%m-%d %H:%M:%S"` $oldver $newver $branch >> /tmp/git.log
if [ "$branch" == "refs/heads/master" ]; then
git --git-dir=/www/web/dotcoo.com/.git --work-tree=/www/web/dotcoo.com pull >> /tmp/git.log
fi
done
exit 0
注意 post-receive
执行权限 chmod +x post-receive
角色 chown -R git:git custom_hooks
注意网站目录文件写入权限
git config 多域名 多账号
创建多个密钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_user1 -C "user1@dotcoo.com"
ssh-keygen -t rsa -f ~/.ssh/id_rsa_user2 -C "user2@dotcoo.com"
创建配置文件
~/.ssh/config
配置域名和密钥
Host user1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user1
Host user2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user2
测试账号配置是否正确
ssh -T git@user1
ssh -T git@user2
克隆项目
git clone git@user1:user1/hello.git
git clone git@user2:user2/world.git
npm安装模块
npm install git+ssh://user1/user1/hello.git