目录

Git-高级指南完整命令大全及进阶用法

Git 高级指南:完整命令大全及进阶用法

Git 是最流行的分布式版本控制系统。本指南详细整理了 Git 的 基础操作、高级技巧、远程仓库管理、代理配置、子模块管理 以及 进阶使用技巧 ,帮助你全面掌握 Git。


git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"

💡 作用 :设置全局用户名和邮箱,这些信息会出现在提交记录中。

git init

💡 作用 :初始化一个新的 Git 仓库。

git clone <仓库URL>

💡 作用 :从远程仓库克隆代码到本地。

git add <文件名>   # 添加单个文件
git add .          # 添加所有更改的文件
git commit -m "提交描述"
git status
git log
git diff            # 查看未暂存的更改
git diff --staged   # 查看已暂存但未提交的更改
git checkout -- <文件名>    # 放弃未提交的更改
git reset HEAD <文件名>     # 取消暂存
git reset --hard HEAD       # 丢弃所有未提交的更改

git remote -v
git remote add origin <仓库URL>
git remote set-url origin <新的仓库URL>
git push origin <分支名>
git push -u origin <分支名>   # 设为默认上游分支
git pull origin <分支名>
git fetch origin
git push origin --delete <分支名>
git remote remove origin

git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --get http.proxy
git config --global --get https.proxy
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
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
git config --global http.proxy socks5://127.0.0.1:7897
git config --global https.proxy socks5://127.0.0.1:7897
git config --global --unset http.proxy
git config --global --unset https.proxy

如果你在公司内网 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

如果你希望 只对某个仓库 使用代理,而不影响全局 Git 配置,可以在该仓库的 config 文件中进行本地设置。

git config --global --unset http.https://git.company.com.proxy
git config --global --unset https.https://git.company.com.proxy
cd /path/to/your/repo
git config http.proxy http://127.0.0.1:7897
git config https.proxy http://127.0.0.1:7897

💡 此配置只会影响当前仓库,不会影响其他 Git 仓库的网络访问。

git config --get http.proxy
git config --get https.proxy

如果返回 http://127.0.0.1:7897,说明该仓库代理已生效。


git submodule add <子模块Git仓库URL> <子模块存放路径>
git clone --recursive <仓库URL>
git submodule update --init --recursive
git submodule update --remote
git submodule deinit -f <子模块路径>
rm -rf .git/modules/<子模块路径>
git rm -f <子模块路径>
git config -f .gitmodules submodule.<子模块路径>.url <新的URL>
git submodule sync

git stash           # 暂存所有未提交的更改
git stash pop       # 恢复最近一次存储的更改
git stash list      # 查看存储的更改列表
git stash drop      # 删除最近一次存储的更改
git stash clear     # 清空所有存储的更改
git commit --amend -m "新的提交描述"
git reset --soft HEAD~1  # 撤回最近一次提交,保留更改
git reset --hard HEAD~1  # 撤回最近一次提交并删除所有更改
git rebase -i HEAD~3   # 交互式变基,修改最近 3 次提交

Git 的配置文件按 优先级 分为 3 层:

级别文件路径作用范围优先级(高 → 低)
仓库级(Local).git/config (当前仓库)仅影响当前 Git 仓库⭐⭐⭐⭐⭐
用户级(Global)~/.gitconfig~/.config/git/config影响当前用户的所有 Git 仓库⭐⭐⭐
系统级(System)/etc/gitconfig影响整个系统所有 Git 用户

💡 Git 读取配置时,优先使用更高优先级的配置

比如:如果 ~/.gitconfig 里定义了代理,而 .git/config 里没有,那 Git 会使用全局代理。

  • 修改 ~/.gitconfig 影响当前用户。
  • 修改 /etc/gitconfig 影响所有用户。
  • 修改 .git/config 影响单个 Git 仓库。
  • 修改 %USERPROFILE%gitconfig 影响当前用户。
  • 修改 C:\ProgramData\Git\config 影响所有用户。
  • 修改 C:\path\to\repogit\config 影响单个 Git 仓库。
[http]
    proxy = http://127.0.0.1:7897

[https]
    proxy = http://127.0.0.1:7897
[http "https://github.com"]
    proxy = http://127.0.0.1:7897

[https "https://github.com"]
    proxy = http://127.0.0.1:7897
[http]
    proxy = socks5://127.0.0.1:7897

[https]
    proxy = socks5://127.0.0.1:7897

Git 是开发过程中不可或缺的工具。本文详细整理了 Git 基础命令、远程操作、代理配置、子模块管理 以及 高级 Git 技巧 ,希望可以帮助你提升 Git 使用效率。🚀