使用 git

Contents
  1. 1. 创建版本库
  2. 2. 提交
  3. 3. 使用 GPG 签名
  4. 4. 添加远程库
  5. 5. 克隆远程库
  6. 6. 分支
  7. 7. 远程分支
  8. 8. 变基
  9. 9. 储藏
  10. 10. 标签
  11. 11. .gitignore
  12. 12. 别名
  13. 13. .git/config
  14. 14. 搭建 Git 服务器
  15. 15. References

创建版本库

1
2
3
mkdir -p ~/Projects/Git/first
cd ~/Projects/Git/first
git init

提交

1
2
git add -A
git commit -m "message here"

使用 GPG 签名

1
2
git config user.signingkey <key ID>
git config commit.gpgsign true

添加远程库

以 Github 为例:

1
git remote add origin [email protected]:<username>/<repository>.git

首次推送:

1
git push -u origin master

日常推送:

1
git push origin master

克隆远程库

以 Github 为例:

1
git clone [email protected]:<username>/<repository>.git

分支

创建分支:

1
git branch <name>

切换分支:

1
git checkout <name>

创建并切换分支:

1
git checkout -b <name>

合并分支到当前分支:

1
git merge <name>

合并时禁用 Fast Forward :

1
git merge --no-ff -m "message here" <name>

删除分支:

1
git branch -d <name>

强行删除分支:

1
git branch -D <name>

远程分支

查看远程版本库:

1
git remote -v

在本地创建对应的远程分支:

1
git checkout -b <name> origin/<name>

或建立本地分支和远程分支的对应关系:

1
git branch --set-upstream <name> origin/<name>

抓取分支:

1
git pull

推送分支:

1
git push origin <name>

变基

1
git rebase

储藏

储藏当前状态:

1
git stash

恢复储藏状态:

1
git stash apply

删除储藏状态:

1
git stash drop

恢复并删除储藏状态:

1
git stash pop

标签

查看标签

1
git tag
1
git show <tag>

创建标签:

1
git tag <tag>

在某个 commit 上创建标签:

1
git tag <tag> <commit ID>

附带信息:

1
git tag -a <tag> -m "message here" <commit ID>

删除标签:

1
git tag -d <tag>

推送标签:

1
git push origin <tag>

推送所有标签:

1
git push origin --tags

删除远程标签:

1
git push origin :refs/tags/<tag>

.gitignore

存储忽略的文件规则。

强制添加:

1
git add -f <filename>

检查规则:

1
git check-ignore -v <filename>

如果刚有变动,想要不在包括某些文件,可能在更改 .gitignore 后还要清一下缓存:

1
2
3
git rm -r --cached .
git add .
git commit -m "fixed untracked files"

别名

1
git config alias.xx "xxxx yyyy"

.git/config

存储版本库的 Git 设置。

1
git config color.ui true

搭建 Git 服务器

以 Ubuntu 18.04 为例:

1
sudo apt update && sudo apt install git -y
1
sudo adduser git

使用 ssh-copy-id 将公钥上传到服务器上,见 Linux 管理多个 SSH Key

git 用户登录服务器:

1
2
3
mkdir ~/Projects/Git
cd ~/Projects/Git
git init --bare example.git

禁止 shell 登录:

1
sudo vim /etc/passwd
/etc/passwd
1
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

保存并退出服务器。

本地克隆:

1
git clone git@server:Projects/Git/example.git

如需搭建 Gitlab 服务器,参见 在 Debian (Ubuntu) 上安装搭建 Gitlab CE

References

Git 学习指南

Signing commits

gpg failed to sign the data fatal: failed to write commit object

Git error: gpg failed to sign the data on Linux

github/gitignore

.gitignore is ignored by Git

Mastodon