在日常开发中,Git 是必不可少的版本控制工具。以下是一些最常用的命令。

基本操作

# 克隆仓库
git clone git@github.com:user/repo.git

# 查看状态
git status

# 添加文件到暂存区
git add filename.py
git add .  # 添加所有变更

# 提交
git commit -m "feat: add login feature"

分支管理

# 创建并切换分支
git checkout -b feature/new-feature

# 查看分支
git branch -a

# 合并分支
git checkout main
git merge feature/new-feature

# 删除分支
git branch -d feature/new-feature

变基 (Rebase)

变基可以让提交历史更加线性整洁。

# 变基到 main
git checkout feature-branch
git rebase main

# 交互式变基,合并最近 3 个提交
git rebase -i HEAD~3

贮藏 (Stash)

当需要临时切换分支时,可以用 stash 保存工作进度:

# 保存当前修改
git stash push -m "wip: half-done refactoring"

# 查看贮藏列表
git stash list

# 恢复最近一次贮藏
git stash pop

常用配置

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status

以上就是日常使用频率最高的 Git 命令。记住这些,大多数场景都够用了。