🔧 基础配置与连接

SSH密钥管理

1
2
# 生成新的RSA密钥对
ssh-keygen -t rsa -C "[email protected]"

远程仓库操作

1
2
3
4
5
6
7
8
9
10
11
# 查看所有已配置的远程仓库
git remote -v

# 修改指定远程仓库地址(修正拼写错误)
git remote set-url origin 新url

# 添加新的远程仓库
git remote add [仓库别名] [仓库URL]

# 删除指定远程仓库
git remote rm [仓库别名]

📦 Git LFS 管理

1
2
3
4
5
6
7
8
# 安装Git LFS扩展
git lfs install

# 追踪指定类型的大文件(如PSD/视频)
git lfs track "*.psd" "*.mp4"

# 查找仓库中的大文件(显示前10个)
git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}' | git rev-list --objects --stdin | awk '{print $2}' | sort -u

远程与本地分支操作

远程某分支合并到本地

1
2
3
4
5
6
7
8
# 拉取远程分支并合并(添加安全参数)
git pull origin [分支名] --allow-unrelated-histories

# 获取远程仓库更新(修正拼写错误)
git fetch [远程仓库别名]

# 合并指定分支到当前分支
git merge [远程仓库别名]/[分支名]

配置用户信息

1
2
3
4
5
6
7
8
9
10
11
12
# 配置用户名
git config --global user.name "yourname"

# 配置用户邮箱
git config --global user.email "[email protected]"

# 查看当前的配置信息
git config --global --list

# 通过 alias 配置简写
## 例如使用 git co 代替 git checkout
git config --global alias.co checkout

📥 仓库克隆与初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 基础克隆(自动创建与远程仓库同名的目录)
git clone https://github.com/user/repo.git

# 克隆指定分支(适合只需要特定版本的情况)
git clone -b develop https://github.com/user/repo.git

# 克隆到指定目录(目录不存在会自动创建)
git clone https://github.com/user/repo.git my-project

# 深度克隆(只获取最近一次提交,节省时间空间)
git clone --depth 1 https://github.com/user/repo.git

# 克隆子模块(递归克隆包含的子仓库)
git clone --recurse-submodules https://github.com/user/repo.git

git add: 提交到暂存区

1
2
3
4
5
6
7
8
# 将所有修改的文件都提交到暂存区
git add .

# 将修改的文件中的指定的文件 a.js 和 b.js 提交到暂存区
git add ./a.js ./b.js

# 将 js 文件夹下修改的内容提交到暂存区
git add ./js

✏️ 提交管理

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
28
29
30
31
32
33
# 常规提交(推荐使用有意义的提交信息)
git commit -m "feat: add user authentication module"

# 修改上次提交(适合补漏小修改)
git commit --amend -m "fix: correct typo in auth module"

# 空提交(用于触发CI/CD等场景)
git commit --allow-empty -m "chore: trigger deployment"

# 签名提交(需要配置GPG密钥)
git commit -S -m "docs: update API documentation"

# 指定提交时间(格式:YYYY-MM-DD HH:MM:SS)
git commit --date="2025-02-27 15:00:00" -m "fix: timezone issue"

# 使用提交模板(需提前配置)
git commit -t .gitmessage

# 分阶段提交(交互式选择修改内容)
git commit -p

# 快速提交(跳过git hooks,慎用)
git commit --no-verify -m "紧急修复:服务器宕机问题"

# 提交规范建议:
# feat : 新功能
# fix : bug修复
# docs : 文档变更
# style : 代码格式调整
# refactor: 代码重构
# perf : 性能优化
# test : 测试相关
# chore : 构建过程或辅助工具变更

📤 远程推送管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 常规推送(需已建立跟踪分支)
git push

# 强制推送(谨慎使用,会覆盖远程历史)
git push -f

# 推送并建立上游关联
git push -u origin main

# 推送所有分支
git push --all

# 推送标签
git push --tags

# 删除远程分支
git push origin --delete feature-old

# 部分文件推送(需配合稀疏检出)
git push origin HEAD:main --force -- sparse-checkout

🔄 分支同步与合并

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
# 基础拉取(fast-forward合并)
git pull origin main

# 变基式拉取(保持历史线性)
git pull --rebase origin main

# 强制覆盖本地修改(慎用)
git reset --hard origin/main && git pull

# 解决冲突后继续变基
git rebase --continue

# 查看合并差异
git diff main..feature/new-feature

# 创建合并请求(GitLab)
git push origin new-feature && glab mr create

# 合并策略选择:
# --no-ff : 保留特性分支历史
# --squash: 压缩为单个提交
git merge --no-ff feature/new-feature

# 合并工具配置
git config --global merge.tool vscode
git mergetool

git checkout: 切换分支

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
# 切换到已有的本地分支 branch1
git checkout branch1

# 切换到远程分支 branch1
git checkout origin/branch1

# 基于当前本地分支创建一个新分支 branch2,并切换至 branch2
git checkout -b branch2

# 基于远程分支 branch1 创建一个新分支 branch2,并切换至 branch2
git checkout origin/branch1 -b branch2
## 当前创建的 branch2 关联的上游分支是 origin/branch1,所以 push 时需要如下命令关联到远程 branch2
git push --set-upstream origin branch2

# 撤销工作区 file 内容的修改。危险操作,谨慎使用
git checkout -- <file>

# 撤销工作区所有内容的修改。危险操作,谨慎使用
git checkout .

#当前分支改动了,但是未提交需要切换到其他分支
#如 直接 "git stash"则将上次commit注释作为说明
git stash save "存储说明"
git checkout branch1
#事后再切换回刚才分支继续进行开发
git checkout branch
git stash pop

git restore: 取消缓存

1
2
3
4
5
# 将 a.js 文件取消缓存(取消 add 操作,不改变文件内容)
git reset --staged a.js

# 将所有文件取消缓存
git reset --staged .

git reset: 回滚代码

1
2
3
4
5
6
7
8
9
10
11
12
# 将某个版本的 commit 从本地仓库退回到工作区(取消 commit 和 add 操作,不改变文件内容)
## 默认不加 -- 参数时时 mixed
git reset --mixed <commit_sha>

# 将某个版本的 commit 从本地仓库退回到缓存区(取消 commit 操作,不取消 add,不改变文件内容)
git reset --soft <commit_sha>

# 取消某次 commit 的记录(取消 commit 和 add,且改变文件内容)
git reset --hard <commit_sha>

## 以上三种操作退回了 commit,都是退回本地仓库的 commit,没有改变远程仓库的 commit。通常再次修改后配合如下命令覆盖远程仓库的 commit:
git push -f

🗂️ 完整命令速查

⚙️ 配置相关

1
2
3
4
5
6
7
8
9
# 初始化仓库 & 用户配置
git init
git config --global user.name "[用户名]"
git config --global user.email "[邮箱]"
git config --global color.ui true

# 代理设置
git config --global http.proxy http://proxy.example.com:8080
git config --global --unset http.proxy

📦 日常操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 仓库克隆
git clone [仓库URL]

# 变更管理
git add [文件/目录]
git commit -m "[描述]"
git push
git pull

# 分支操作
git branch [分支名]
git checkout [分支名]
git merge [分支名]
git rebase [分支名]

🔍 查询与比较

1
2
3
4
5
6
7
8
9
10
11
# 查看历史
git log --oneline --graph
git show [commitID]

# 差异比较
git diff [分支1]..[分支2]
git diff --staged

# 文件追溯
git blame [文件名]
git log -p [文件名]

🛠️ 高级功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 问题定位
git bisect start
git bisect good [commitID]
git bisect bad [commitID]

# 子模块管理
git submodule add [仓库URL]
git submodule update --init

# 工作暂存
git stash save "[描述]"
git stash pop

# 标签管理
git tag -a v1.0 -m "版本说明"
git push --tags

🧹 维护优化

1
2
3
4
5
6
7
8
9
10
# 仓库清理
git gc --prune=now
git remote prune origin

# 大文件管理
git lfs install
git lfs track "*.psd"

# 重写历史
git filter-branch --tree-filter 'rm -f [文件名]' HEAD