Skip to content

Git

Git 基本配置

全局用户信息

shell
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  • 查看:
shell
git config --global --list

常用基础配置

shell
# 默认分支名改为 main
git config --global init.defaultBranch main

# 启用颜色输出
git config --global color.ui auto

# 提交信息编辑器(选一个)
git config --global core.editor "code --wait"     # VS Code
git config --global core.editor "vim"              # Vim
git config --global core.editor "webstorm --wait"  # Webstorm


# Windows / WSL 推荐(避免 CRLF 问题)
git config --global core.autocrlf input

# 提交前忽略文件权限变化
git config --global core.fileMode false

Git 别名(提高效率)

shell
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --all"
  • 使用示例:
shell
git st
git co main
git lg

仓库初始化 & 基本使用

初始化仓库

shell
git init
  • 或克隆远程仓库:
shell
git clone https://github.com/user/repo.git

Git 工作区状态

shell
git status
  • 状态说明:
    • Untracked:新文件,Git 还没管
    • Modified:已修改
    • Staged:已暂存(add 过)

添加文件到暂存区

shell
git add file.txt      # 添加单个文件
git add .             # 添加所有变更
git add -p            # 交互式添加(推荐)

提交代码

shell
git commit -m "feat: add login page"
  • 跳过暂存直接提交:
shell
git commit -am "fix: bug"

查看历史 & 变更

查看提交记录

shell
git log
git log --oneline
git log --oneline --graph --all

查看修改内容

shell
git diff              # 工作区 vs 暂存区
git diff --staged     # 暂存区 vs 上一次提交

查看文件修改历史

shell
git log file.txt
git blame file.txt

分支操作

查看 / 创建 / 切换分支

shell
git branch             # 查看本地分支
git branch dev         # 创建分支
git checkout dev       # 切换分支(旧)
git switch dev         # 切换分支(新)
git switch -c feature/login   # 创建并切换

合并分支

shell
git switch main
git merge dev

删除分支

shell
git branch -d dev      # 已合并
git branch -D dev      # 强制删除

远程仓库(GitHub / GitLab)

远程仓库配置

shell
git remote -v
git remote add origin git@github.com:user/repo.git

推送代码

shell
git push origin main
git push -u origin main   # 第一次推送(记住上游)

拉取代码

shell
git pull        # fetch + merge
git fetch       # 只拉不合

推送分支

shell
git push origin feature/login

撤销 / 回退

撤销工作区修改(未 add)

shell
git restore file.txt

撤销暂存区(已 add)

shell
git restore --staged file.txt

修改最后一次提交(未 push)

shell
git commit --amend

回退到某个提交(慎用)

shell
git reset --soft HEAD~1   # 保留代码
git reset --hard HEAD~1   # 丢弃代码(危险)

安全回退(推荐)

shell
git revert commit_id

暂存现场(stash)

shell
git stash
git stash list
git stash pop
git stash drop

适合:临时切分支 / 拉代码

.gitignore

git
node_modules/
dist/
.env
.DS_Store
.idea/
  • 生效前已提交的文件:
shell
git rm -r --cached node_modules

常见开发工作流

日常开发流程

shell
git pull
git switch -c feature/xxx
# coding...
git add .
git commit -m "feat: xxx"
git push origin feature/xxx

多人协作推荐(Git Flow 简化)

  • main:稳定版本
  • dev:开发分支
  • feature/*:功能分支
  • hotfix/*:紧急修复

提交规范

shell
feat: 新功能
fix: 修复 bug
docs: 文档
style: 格式
refactor: 重构
chore: 构建/工具

常见问题

提交错分支了?

shell
git reset --soft HEAD~1
git switch correct-branch
git commit

本地分支落后远程?

shell
git pull --rebase

解决冲突?

shell
# 手动改文件
git add .
git commit