Git

Setup & Config

git config --global user.name "Your Name"     # set username
git config --global user.email "you@example.com"  # set email
git config --global core.editor vim            # set default editor
git config --global init.defaultBranch main    # set default branch name
git config --list                              # view all config

Create & Clone

git init                                      # initialize new repo
git clone https://github.com/user/repo.git    # clone via HTTPS
git clone git@github.com:user/repo.git        # clone via SSH
git clone --depth 1 https://github.com/user/repo.git  # shallow clone
git clone -b develop https://github.com/user/repo.git # clone specific branch

Stage & Commit

git add file.txt              # stage a file
git add .                     # stage all changes
git add -p                    # interactive staging (hunk by hunk)
git commit -m "feat: add login page"   # commit with message
git commit -am "fix: correct typo"     # stage tracked files and commit
git commit --amend -m "new message"    # amend last commit message

Branching

git branch                    # list local branches
git branch feature/login      # create branch
git checkout feature/login    # switch to branch
git checkout -b feature/login # create and switch
git switch -c feature/login   # modern alternative to checkout -b
git branch -d feature/login   # delete merged branch
git branch -D feature/login   # force delete branch
git branch -m old-name new-name  # rename branch

Merge & Rebase

git merge feature/login       # merge branch into current
git merge --no-ff feature/login  # merge with merge commit (no fast-forward)
git merge --abort             # abort conflicted merge

git rebase main               # rebase current branch onto main
git rebase -i HEAD~3          # interactive rebase last 3 commits
git rebase --continue         # continue after resolving conflict
git rebase --abort            # abort rebase

Remote

git remote -v                 # list remote URLs
git remote add origin https://github.com/user/repo.git  # add remote
git remote set-url origin git@github.com:user/repo.git  # change remote URL
git fetch origin              # fetch without merging
git pull origin main          # fetch and merge
git push origin main          # push to remote
git push -u origin feature/login  # push and set upstream
git push origin --delete feature/login  # delete remote branch

Log & Diff

git log --oneline --graph --all    # visual branch history
git log --oneline -10              # last 10 commits
git log --author="Alice"           # filter by author
git log --since="2 weeks ago"      # filter by date
git log -p file.txt                # commit history of a file

git diff                           # unstaged changes
git diff --staged                  # staged changes
git diff main..feature/login       # diff between branches
git diff HEAD~1                    # diff with last commit

Undo & Reset

git restore file.txt           # discard working directory changes
git restore --staged file.txt  # unstage file (keep changes)
git reset HEAD~1              # undo last commit, keep changes unstaged
git reset --soft HEAD~1       # undo last commit, keep changes staged
git reset --hard HEAD~1       # undo last commit, discard all changes
git revert abc1234            # create new commit that undoes abc1234
git clean -fd                 # remove untracked files and directories

Stash

git stash                      # stash current changes
git stash -u                   # stash including untracked files
git stash list                 # list all stashes
git stash pop                  # apply and remove latest stash
git stash apply stash@{1}      # apply specific stash (keep in list)
git stash drop stash@{0}      # drop specific stash
git stash branch feature/from-stash  # create branch from stash

Tags

git tag v1.0.0                 # lightweight tag
git tag -a v1.0.0 -m "Release 1.0.0"  # annotated tag with message
git tag                        # list all tags
git tag -d v1.0.0             # delete local tag
git push origin v1.0.0        # push tag to remote
git push origin --tags        # push all tags to remote

Cherry-pick & Reflog

git cherry-pick abc1234        # apply specific commit to current branch
git cherry-pick abc1234 def5678  # cherry-pick multiple commits
git cherry-pick --abort       # abort cherry-pick

git reflog                    # show all HEAD movements
git reflog show HEAD@{5}      # show reflog entry
git checkout HEAD@{5}         # restore to previous state

Conventional Commits

# format: <type>(<scope>): <description>
git commit -m "feat: add user login page"
git commit -m "fix: correct password validation"
git commit -m "docs: update API documentation"
git commit -m "refactor: extract auth middleware"
git commit -m "test: add unit tests for user service"
git commit -m "chore: upgrade dependencies"
git commit -m "feat(auth): support OAuth2 login"
git commit -m "fix(api): handle null response"

Commit types:

  • `feat` new feature
  • `fix` bug fix
  • `docs` documentation
  • `style` formatting (no code change)
  • `refactor` code restructuring
  • `test` adding tests
  • `chore` build/tooling changes
feat: add login page

Implement OAuth2 login with GitHub provider.
Includes redirect callback and session handling.

Closes #123

Useful Tips

git shortlog -sn              # contributor commit count
git log --pretty=format:"%h - %an : %s" --since="1 day ago"  # custom log format
git diff --stat               # show changed files summary
git stash clear               # remove all stashes
git rm file.txt               # remove file and stage deletion
git mv old.txt new.txt        # rename file and stage
git worktree add ../hotfix hotfix-branch  # create linked working tree

初始配置

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor vim
git config --global init.defaultBranch main
git config --list

创建与克隆

git init
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
git clone --depth 1 https://github.com/user/repo.git    # 浅克隆
git clone -b develop https://github.com/user/repo.git    # 克隆指定分支

暂存与提交

git add file.txt
git add .
git add -p                  # 交互式暂存
git commit -m "feat: 添加登录页"
git commit -am "fix: 修正拼写错误"
git commit --amend -m "新提交信息"

分支操作

git branch                  # 列出分支
git branch feature/login    # 创建分支
git checkout feature/login  # 切换分支
git checkout -b feature/login  # 创建并切换
git switch -c feature/login    # 新版切换命令
git branch -d feature/login   # 删除已合并分支
git branch -D feature/login   # 强制删除分支
git branch -m old-name new-name  # 重命名分支

合并与变基

git merge feature/login
git merge --no-ff feature/login   # 禁用快进合并
git merge --abort

git rebase main                   # 变基到 main
git rebase -i HEAD~3              # 交互式变基最近3条
git rebase --continue
git rebase --abort

远程操作

git remote -v
git remote add origin https://github.com/user/repo.git
git remote set-url origin git@github.com:user/repo.git
git fetch origin
git pull origin main
git push origin main
git push -u origin feature/login   # 推送并设置上游
git push origin --delete feature/login  # 删除远程分支

日志与差异

git log --oneline --graph --all
git log --oneline -10
git log --author="Alice"
git log --since="2 weeks ago"
git log -p file.txt

git diff                    # 工作区与暂存区差异
git diff --staged           # 暂存区与最新提交差异
git diff main..feature/login  # 两个分支差异
git diff HEAD~1

撤销与重置

git restore file.txt            # 恢复工作区文件
git restore --staged file.txt   # 取消暂存
git reset HEAD~1               # 软重置 (保留修改)
git reset --soft HEAD~1        # 保留暂存区
git reset --hard HEAD~1        # 彻底重置 (丢弃修改)
git revert abc1234             # 创建反向提交
git clean -fd                  # 清理未跟踪文件

暂存工作区

git stash
git stash -u                   # 包含未跟踪文件
git stash list
git stash pop                  # 恢复并删除
git stash apply stash@{1}      # 恢复但保留记录
git stash drop stash@{0}
git stash branch feature/from-stash

标签管理

git tag v1.0.0                  # 轻量标签
git tag -a v1.0.0 -m "发布 1.0.0"  # 附注标签
git tag                         # 列出标签
git tag -d v1.0.0              # 删除本地标签
git push origin v1.0.0         # 推送标签
git push origin --tags         # 推送所有标签

搜索与追溯

git blame file.txt              # 查看每行最后修改者
git blame -L 10,20 file.txt    # 指定行范围
git log -S "function_name"     # 搜索包含该字符串的提交
git log -G "pattern"           # 用正则搜索差异
git grep "TODO"                # 在工作区搜索
git bisect start               # 二分查找引入bug的提交
git bisect bad
git bisect good abc1234

精选与引用日志

git cherry-pick abc1234         # 将指定提交应用到当前分支
git cherry-pick abc1234 def5678
git cherry-pick --abort

git reflog                     # 查看所有操作记录
git reflog show HEAD@{5}
git checkout HEAD@{5}          # 恢复到历史状态

Conventional Commits

# 格式: <类型>(<范围>): <描述>
git commit -m "feat: 添加用户登录页"
git commit -m "fix: 修正密码校验逻辑"
git commit -m "docs: 更新 API 文档"
git commit -m "refactor: 提取认证中间件"
git commit -m "test: 添加用户服务单元测试"
git commit -m "chore: 升级依赖版本"
git commit -m "feat(auth): 支持 OAuth2 登录"
git commit -m "fix(api): 处理空值响应"

提交类型:

  • `feat` 新功能
  • `fix` 修复缺陷
  • `docs` 文档变更
  • `style` 格式调整(不影响逻辑)
  • `refactor` 重构(不新增功能也不修复)
  • `test` 测试相关
  • `chore` 构建/工具变更
feat: 添加登录页

实现基于 GitHub 的 OAuth2 登录。
包含回调处理和会话管理。

Closes #123

实用技巧

git shortlog -sn               # 按提交数统计贡献者
git log --pretty=format:"%h - %an : %s" --since="1 day ago"
git diff --stat                # 显示变更文件统计
git stash clear                # 清空所有 stash
git rm file.txt                # 删除并暂存
git mv old.txt new.txt         # 重命名并暂存
git worktree add ../hotfix hotfix-branch  # 创建工作树