术语

  • 工作区(Working Directory):从某个版本独立提取出来的内容,也包括未被 Git 管理的文件。简单来说就是 .git 文件夹以外的内容。
  • 暂存区(Staging Area):记录了待提交信息的一个文件。
  • 仓库/版本库(Repository):Git 用来保存项目的元数据和对象数据库的地方,也就是 .git 文件夹。

配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 名字和邮箱必须设置,其他按需要配置就好
git config --global user.name "your-name"
git config --global user.email "your-email"

# 设置远程分支后才能用 `git push`
git config --global push.default simple

# 显示中文路径
git config --global core.quotepath false

# 设置编辑器
git config --global core.editor "nano"

# 设置换行符为 LF
git config --global core.autocrlf input

# 设置好看的 git log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.lgs "log --pretty=oneline --graph --abbrev-commit"

.gitignore

.gitignore 用于忽略不需要的文件,例如:

1
2
3
4
node_modules
.DS_Store
.idea
.vscode

命令别名(alias)

配置命令别名后操作更简单,Oh My Zsh 的 git 插件就自动配置了别名。如果不用 zsh,手动添加别名到 ~/.bashrc 也行。

1
2
3
4
5
6
alias ga='git add'
alias gc='git commit -v'
alias gl='git pull'
alias gp='git push'
alias gco='git checkout'
alias gst='git status'

配置 SSH 密钥

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 生成 SSH 密钥(ed25519 比 RSA 安全)
ssh-keygen -t ed25519 -C "注释"

# 查看密钥
cat ~/.ssh/id_ed25519.pub

# 复制代码
xclip -selection clipboard < ~/.ssh/id_ed25519.pub

# 添加密钥到 GitHub
xdg-open https://github.com/settings/ssh/new

# 接收 GitHub 公钥
ssh -T [email protected]

GitHub 文档

创建仓库

1
2
3
4
5
# 初始化当前目录
git init

# 初始化文件夹
git init directory-name

分支

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 查看分支
git branch

# 创建分支
git branch branch-name

# 切换分支
git switch branch-name
git checkout branch-name

# 创建并切换分支
git switch -c branch-name # -c --create
git checkout -b branch-name # -b(branch)

# 删除分支
git branch -d branch-name

# 合并分支
git checkout main # 切换到接受代码的分支
git merge patch # 把 patch 分支的代码合并到 main

远程仓库

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 克隆仓库
git clone repo-link
git clone repo-link local-repo-name # 克隆并重命名
git clone repo-link . # 克隆到当前文件夹(不推荐)
gh repo clone username/repo-name

# 添加远程仓库
git remote add remote-name repo-link

# 查看远程仓库
git remote -v

# 删除远程仓库
git remote remove remote-name

# 推送代码
git push remote-name local-branch:remote-branch
git push -u remote-name branch-name # 设置上游
git push remote-name branch-name
git push

# 拉取代码
git pull remote-name branch-name
git pull

查看状态

1
2
3
4
5
# 详细内容
git status

# 精简内容
git status -sb 

把文件加到暂存区

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 添加文件
git add file-name
git add file-1 file-2

# 添加目录
git add directory-name

# 添加所有文件
git add .
git add *
git add --all

恢复暂存区的文件

1
2
3
4
5
# 恢复所有
git reset -- .

# 恢复某个文件或目录
git reset -- file-name

隐藏未提交的内容

1
2
3
4
5
6
7
8
# 隐藏暂存区
git stash

# 查看隐藏内容
git stash list

# 恢复内容
git stash pop

清空暂存区

1
git clean -di

删除文件

以下方法删除的内容仍然可以被恢复。如果要完全删除,请参考这篇文章

1
2
3
4
5
6
# 方法 1(推荐)
git rm file-name

# 方法 2
rm file-name
git add file-name

提交代码

1
2
git commit -v # 在提交说明底部显示 diff
git commit -m "提交说明" # 不推荐

查看历史

1
2
3
4
5
6
7
# 查看历史
git log
git log --pretty=oneline --graph --abbrev-commit
git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

# 查看所有历史(git reset 之前的都有)
git reflog # reflog 只在本地,不会上传到远程仓库

版本回退

1
2
3
# --hard 会删除暂存区的文件
git reset --hard commit-id
git reset --hard HEAD

解决冲突

  • git status 查看冲突文件

  • 打开冲突文件

  • 搜索 =======

  • 删除冲突文件里的 <<<<<<< HEAD=======>>>>>>> branch-name

  • 修改文件,留下需要的代码

  • git add conflicted-file

  • 解决完所有冲突再运行 git commit

合并提交

1
2
3
4
5
6
7
8
9
# 合并提交
git rebase -i HEAD~3
git rebase -i commit-id

# 取消 rebase
git rebase --abort

# 继续 rebase
git rebase --continue