Skip to content

Git ​

基础配置 ​

bash
# 全局配置
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# 查看配置
git config --list

# 代理设置(国内加速)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 全局配置
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# 查看配置
git config --list

# 代理设置(国内加速)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

常用命令 ​

仓库操作 ​

bash
git init                    # 初始化仓库
git clone <url>             # 克隆远程仓库
git remote add origin <url> # 关联远程仓库
git remote -v               # 查看远程仓库
git init                    # 初始化仓库
git clone <url>             # 克隆远程仓库
git remote add origin <url> # 关联远程仓库
git remote -v               # 查看远程仓库

分支管理 ​

bash
git branch                  # 查看本地分支
git branch -a               # 查看所有分支(含远程)
git branch <name>           # 创建分支
git checkout <name>         # 切换分支
git checkout -b <name>      # 创建并切换分支
git merge <name>            # 合并指定分支到当前分支
git branch -d <name>        # 删除分支
git branch -D <name>        # 强制删除分支(未合并)
git branch                  # 查看本地分支
git branch -a               # 查看所有分支(含远程)
git branch <name>           # 创建分支
git checkout <name>         # 切换分支
git checkout -b <name>      # 创建并切换分支
git merge <name>            # 合并指定分支到当前分支
git branch -d <name>        # 删除分支
git branch -D <name>        # 强制删除分支(未合并)

提交与暂存 ​

bash
git status                  # 查看状态
git add <file>              # 暂存文件
git add .                   # 暂存所有变更(慎用)
git commit -m "message"     # 提交
git commit --amend          # 修改上次提交

git reset HEAD <file>       # 取消暂存
git checkout -- <file>      # 撤销工作区修改
git restore <file>          # 新版撤销修改
git status                  # 查看状态
git add <file>              # 暂存文件
git add .                   # 暂存所有变更(慎用)
git commit -m "message"     # 提交
git commit --amend          # 修改上次提交

git reset HEAD <file>       # 取消暂存
git checkout -- <file>      # 撤销工作区修改
git restore <file>          # 新版撤销修改

查看历史 ​

bash
git log                     # 查看提交历史
git log --oneline           # 单行显示
git log --graph             # 图形化显示
git log -p                  # 显示差异详情
git blame <file>            # 查看每行谁改的
git log                     # 查看提交历史
git log --oneline           # 单行显示
git log --graph             # 图形化显示
git log -p                  # 显示差异详情
git blame <file>            # 查看每行谁改的

远程同步 ​

bash
git push origin <branch>            # 推送到远程
git push -u origin <branch>         # 首次推送并关联
git push origin --delete <branch>   # 删除远程分支
git pull                            # 拉取并合并
git fetch                           # 拉取但不合并
git push origin <branch>            # 推送到远程
git push -u origin <branch>         # 首次推送并关联
git push origin --delete <branch>   # 删除远程分支
git pull                            # 拉取并合并
git fetch                           # 拉取但不合并

暂存与恢复 ​

bash
git stash                   # 暂存当前修改
git stash list              # 查看暂存列表
git stash pop               # 恢复并删除暂存
git stash apply             # 恢复但不删除暂存
git stash drop              # 删除暂存
git stash                   # 暂存当前修改
git stash list              # 查看暂存列表
git stash pop               # 恢复并删除暂存
git stash apply             # 恢复但不删除暂存
git stash drop              # 删除暂存

常用技巧 ​

.gitignore 常见配置 ​

gitignore
node_modules/
dist/
.env
*.log
.DS_Store
.idea/
*.local
node_modules/
dist/
.env
*.log
.DS_Store
.idea/
*.local

合并 vs 变基 ​

bash
# merge:保留完整分支历史(推荐公共分支)
git merge feature

# rebase:线性历史(推荐个人分支)
git checkout feature
git rebase main
# merge:保留完整分支历史(推荐公共分支)
git merge feature

# rebase:线性历史(推荐个人分支)
git checkout feature
git rebase main

撤销操作 ​

bash
# 撤销上一次 commit(保留修改)
git reset --soft HEAD~1

# 撤销上一次 commit(丢弃修改)
git reset --hard HEAD~1

# 回退到指定版本
git reset --hard <commit-hash>

# 已 push 的撤销(谨慎使用)
git revert <commit-hash>
# 撤销上一次 commit(保留修改)
git reset --soft HEAD~1

# 撤销上一次 commit(丢弃修改)
git reset --hard HEAD~1

# 回退到指定版本
git reset --hard <commit-hash>

# 已 push 的撤销(谨慎使用)
git revert <commit-hash>

子模块 ​

bash
git submodule add <url> <path>
git submodule update --init --recursive
git submodule add <url> <path>
git submodule update --init --recursive