Git 常用操作

搭建Git服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 以在/opt/doc.git目录下创建仓库为例
# 注意:提交后Git服务器上的源文件内容不会改变,提交后改变内容的在版本库中

# 在服务器上创建仓库
sudo apt install git
sudo mkdir /opt/doc.git
cd /opt/doc.git
sudo git init --bare ./ # 在当前目录下创建共享空仓库
sudo chown -R git:git /opt/doc.git # 将目录所有者改为你,否则会没有权限访问

# 在客户端上使用仓库
git clone ssh://name@IP:Port/opt/doc.git ./ # 克隆空仓库到当前目录下
git config core.autocrlf false # 提交和检出代码时均不进行换行符转换
git config core.filemode false # 避免NTFS文件权限变更引起的修改
git config core.quotepath false # 显示中文路径
git add . # 复制文件到仓库后执行,将文件提交到暂存区
git commit -m "Initial" # 提交到版本库
git push origin master # 推送到远端仓库

常用基础操作

1
2
3
4
5
6
7
8
9
10
11
git clone ssh://name@IP:Port/opt/doc.git		# 克隆远端库到本地
git add test.sh # 添加文件到缓存区
git commit test.sh -m "修改示例更加详细" # 提交改动到版本库
git push origin master # 推送改动到远端仓库

git init # 创建普通空仓库
git remote add origin ssh://name@IP:Port/opt/doc.git # 添加origin主机
git push -u origin master # 使用-u指定默认提交的主机

git rm file_name # 删除本地文件并提交删除到暂存区
git diff commit_id1 commit_id2 # 比较两个版本的区别

常用高级操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
git stash		# 将当前改动暂存
git stash list # 查看有多少暂存
git stash pop # 恢复到最后暂存

git reset --hard # 丢弃所有未提交的修改
git reset <commit_id> # 重置到某版本

git branch # 查看分支
git branch <name> # 创建分支
git checkout <name> # 切换分支
git checkout -b <name> # 创建+切换分支
git merge <name> # 合并某分支到当前分支
git branch -d <name> # 删除分支

git tag # 查看所有标签
git tag <tagname> <commit_id> # 新建一个标签
git tag -a <tagname> -m "note" # 指定标签信息
git push origin <tagname> # 推送一个本地标签
git push origin --tags # 推送全部未推标签
git tag -d <tagname> # 删除一个本地标签
git push origin :refs/tags/<tagname> # 删除远程标签

# 忽略一些文件、文件夹不提交,在仓库根目录下创建名称为“.gitignore”的文件
# 写入不需要的文件夹名或文件,每个元素占一行即可,如:
target
bin
*.db

关联多个主机

1
2
3
4
5
6
git remote -v				# 查看已存主机
git remote add <name> <url> # 添加一个主机
git remote rm <name> # 删除远程主机
git remote rename <原名> <新名> # 远程主机改名
git remote set-url origin <url> # 改库对应地址
git push Host master # 推到其他主机

常用GIT设置

1
2
3
4
5
6
7
8
9
git config --global user.name "name"		# 设置姓名
git config --global user.email "xxx@xx.com" # 设置邮箱
git config --global http.proxy http://ip:port # 设置代理
git config --global https.proxy http://ip:port

git config core.sharedRepository 0660 # 设置权限以让其他用户读写
git config core.autocrlf false # 提交和检出代码时均不进行换行符转换
git config core.filemode false # 避免NTFS文件权限变更引起的修改
git config core.quotepath false # 显示中文路径

指定密钥登陆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 需要密钥免密执行操作的,请将远端主机的私钥放到本地主机以下路径
mv publickey C:\Users\git\.ssh\id_rsa # Win32系统用户
mv publickey /home/git/.ssh/id_rsa # Linux系统用户

# 如果用户没有权限读取密钥,那么每次还是要输入密码的
sudo chown -R git:git /home/git/.ssh/
sudo chown -R git:git /opt/doc.git

# 默认SSH只会读取id_rsa,有多个私钥的话需要将其添加到SSH agent
ssh-add ~/.ssh/id_rsa_me

# 或者修改 ~/.ssh/ssh_config 添加如下内容:
Host gitee.com
User git
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/.ssh/gitee

推送之后执行

1
2
3
4
5
6
7
# 自动部署到网站:每次push成功后都会执行检出到doc目录下
vi /opt/doc.git/hooks/post-receive

#!/bin/bash
git --work-tree=/var/www/doc --git-dir=/opt/doc.git checkout -f

chmod +x post-receive