摘要
Git 是最流行的分布式版本控制系统。本指南详细整理了 Git 的 基础操作、高级技巧、远程仓库管理、代理配置、子模块管理 以及 进阶使用技巧,帮助你全面掌握 Git。
1. Git 基础命令
1.1 配置 Git 用户信息
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"
💡 作用:设置全局用户名和邮箱,这些信息会出现在提交记录中。
1.2 初始化 Git 仓库
git init
💡 作用:初始化一个新的 Git 仓库。
1.3 克隆远程仓库
git clone <仓库URL>
💡 作用:从远程仓库克隆代码到本地。
1.4 添加文件到暂存区
git add <文件名> # 添加单个文件
git add . # 添加所有更改的文件
1.5 提交更改
git commit -m "提交描述"
1.6 查看 Git 状态
git status
1.7 查看提交历史
git log
1.8 查看文件修改
git diff # 查看未暂存的更改
git diff --staged # 查看已暂存但未提交的更改
1.9 撤销更改
git checkout -- <文件名> # 放弃未提交的更改
git reset HEAD <文件名> # 取消暂存
git reset --hard HEAD # 丢弃所有未提交的更改
2. Git 远程操作
2.1 查看远程仓库
git remote -v
2.2 添加远程仓库
git remote add origin <仓库URL>
2.3 修改远程仓库地址
git remote set-url origin <新的仓库URL>
2.4 推送代码到远程
git push origin <分支名>
git push -u origin <分支名> # 设为默认上游分支
2.5 拉取代码
git pull origin <分支名>
2.6 获取远程更新
git fetch origin
2.7 删除远程分支
git push origin --delete <分支名>
2.8 删除远程仓库关联
git remote remove origin
3. Git 代理配置
3.1 设置全局 HTTP 代理
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897
3.2 取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy
3.3 查看当前代理
git config --global --get http.proxy
git config --global --get https.proxy
3.4 仅对 GitHub 设置代理
git config --global http.https://github.com.proxy http://127.0.0.1:7897
git config --global https.https://github.com.proxy http://127.0.0.1:7897
3.5 取消仅对 GitHub 的代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
3.6 使用全局 SOCKS5 代理
git config --global http.proxy socks5://127.0.0.1:7897
git config --global https.proxy socks5://127.0.0.1:7897
3.7 取消全局 SOCKS5 代理
git config --global --unset http.proxy
git config --global --unset https.proxy
3.8 让公司内网 Git 不走代理
如果你在公司内网 Git (git.company.com) 访问不需要代理,确保 ~/.gitconfig 里没有它的代理配置,或者执行:
git config --global --unset http.https://git.company.com.proxy
git config --global --unset https.https://git.company.com.proxy
3.9 针对单个远程仓库设置代理
如果你希望 只对某个仓库
使用代理,而不影响全局 Git
配置,可以在该仓库的 config
文件中进行本地设置。
git config --global --unset http.https://git.company.com.proxy
git config --global --unset https.https://git.company.com.proxy
3.9.1 进入目标 Git 仓库
cd /path/to/your/repo
3.9.2 设置 仅针对该仓库
走代理:
git config http.proxy http://127.0.0.1:7897
git config https.proxy http://127.0.0.1:7897
💡 此配置只会影响当前仓库,不会影响其他 Git 仓库的网络访问。
3.9.3 验证是否生效
git config --get http.proxy
git config --get https.proxy
如果返回 http://127.0.0.1:7897,说明该仓库代理已生效。
4. Git 子模块管理
4.1 添加子模块
git submodule add <子模块Git仓库URL> <子模块存放路径>
4.2 克隆包含子模块的仓库
git clone --recursive <仓库URL>
4.3 更新子模块
git submodule update --init --recursive
4.4 拉取子模块更新
git submodule update --remote
4.5 删除子模块
git submodule deinit -f <子模块路径>
rm -rf .git/modules/<子模块路径>
git rm -f <子模块路径>
4.6 修改子模块远程地址
git config -f .gitmodules submodule.<子模块路径>.url <新的URL>
git submodule sync
5. 进阶 Git 技巧
5.1 Git stash(存储未提交的修改)
git stash # 暂存所有未提交的更改
git stash pop # 恢复最近一次存储的更改
git stash list # 查看存储的更改列表
git stash drop # 删除最近一次存储的更改
git stash clear # 清空所有存储的更改
5.2 修改最近一次提交
git commit --amend -m "新的提交描述"
5.3 软重置、硬重置
git reset --soft HEAD~1 # 撤回最近一次提交,保留更改
git reset --hard HEAD~1 # 撤回最近一次提交并删除所有更改
5.4 交互式变基
git rebase -i HEAD~3 # 交互式变基,修改最近 3 次提交
6. Git 配置文件
Git 的配置文件按 优先级 分为 3 层:
级别 | 文件路径 | 作用范围 | 优先级(高 → 低) |
---|---|---|---|
仓库级(Local) | .git/config (当前仓库) |
仅影响当前 Git 仓库 | ⭐⭐⭐⭐⭐ |
用户级(Global) | ~/.gitconfig 或 ~/.config/git/config |
影响当前用户的所有 Git 仓库 | ⭐⭐⭐ |
系统级(System) | /etc/gitconfig |
影响整个系统所有 Git 用户 | ⭐ |
💡 Git 读取配置时,优先使用更高优先级的配置。
比如:如果 ~/.gitconfig
里定义了代理,而 .git/config
里没有,那 Git 会使用全局代理。
6.1 Linux / macOS 下配置文件存在的路径
- 修改 ~/.gitconfig 影响当前用户。
- 修改 /etc/gitconfig 影响所有用户。
- 修改 .git/config 影响单个 Git 仓库。
6.2 Windows 下配置文件存在的路径
- 修改 %USERPROFILE%\.gitconfig 影响当前用户。
- 修改 C:\ProgramData\Git\config 影响所有用户。
- 修改 C:\path\to\repo\.git\config 影响单个 Git 仓库。
6.3 全局代理(适用于所有 Git 服务器)
[http]
proxy = http://127.0.0.1:7897
[https]
proxy = http://127.0.0.1:7897
6.4 仅对 GitHub 设置代理
[http "https://github.com"]
proxy = http://127.0.0.1:7897
[https "https://github.com"]
proxy = http://127.0.0.1:7897
6.5 SOCKS5 代理
[http]
proxy = socks5://127.0.0.1:7897
[https]
proxy = socks5://127.0.0.1:7897
总结
Git 是开发过程中不可或缺的工具。本文详细整理了 Git 基础命令、远程操作、代理配置、子模块管理 以及 高级 Git 技巧,希望可以帮助你提升 Git 使用效率。🚀