一、Git 基础配置
-
设置用户名
My terminal window git config --global user.name "Yang"
-
设置邮箱
My terminal window git config --global user.email "yang@example.com" -
查看配置信息
My terminal window git config --list -
初始化本地仓库
My terminal window git init -
克隆远程仓库
My terminal window git clone https://github.com/user/repo.git
二、Git 基本操作
-
查看当前仓库状态
My terminal window git status -
添加文件到暂存区
My terminal window git add index.html -
添加所有修改文件到暂存区
My terminal window git add . -
提交到本地仓库
My terminal window git commit -m "初始提交" -
查看提交历史
My terminal window git log -
简化查看提交历史
My terminal window git log --oneline
三、分支管理
-
查看本地分支
My terminal window git branch -
创建新分支
My terminal window git branch feature/login -
切换分支
My terminal window git checkout feature/login -
切换分支(Git 2.23+)
My terminal window git switch feature/login -
创建并切换新分支
My terminal window git checkout -b feature/login -
合并分支
My terminal window git merge feature/login -
删除分支
My terminal window git branch -d feature/login
四、远程仓库操作
-
查看远程仓库
My terminal window git remote -v -
添加远程仓库
My terminal window git remote add origin https://github.com/user/repo.git -
获取远程最新内容
My terminal window git fetch origin -
拉取远程更新并合并
My terminal window git pull origin main -
推送本地提交到远程仓库
My terminal window git push origin main -
推送新分支并设置上游分支
My terminal window git push -u origin feature/login
五、查看与比较
-
查看未暂存修改
My terminal window git diff -
查看已暂存修改
My terminal window git diff --staged -
查看某次提交内容
My terminal window git show abc123 -
图形化查看提交历史
My terminal window git log --graph --all --decorate
六、撤销与恢复
-
撤销暂存区的修改
My terminal window git reset index.html -
丢弃工作区修改
My terminal window git checkout -- index.html -
回退到某次提交,保留工作区修改
My terminal window git reset --soft abc123 -
回退到某次提交,丢弃工作区修改
My terminal window git reset --hard abc123 -
撤销某次提交,生成新提交
My terminal window git revert abc123
七、标签管理
-
查看标签
My terminal window git tag -
创建标签
My terminal window git tag v1.0 -
创建带注释的标签
My terminal window git tag -a v1.0 -m "版本1.0" -
推送标签
My terminal window git push origin v1.0 -
推送所有标签
My terminal window git push origin --tags
八、高级操作
-
暂存未提交的修改
My terminal window git stash -
恢复最近一次暂存的修改
My terminal window git stash pop -
变基,将当前分支移到目标分支后
My terminal window git rebase main -
应用某次提交到当前分支
My terminal window git cherry-pick abc123 -
二分查找引入 bug 的提交
My terminal window git bisect start
九、常用技巧
-
配置别名,提高操作效率
My terminal window git config --global alias.st statusgit config --global alias.ci commitgit config --global alias.co checkoutgit config --global alias.br branch -
快速查看提交日志
My terminal window git log --oneline --graph --decorate --all -
删除远程分支
My terminal window git push origin --delete feature/login -
临时切换到某个提交查看
My terminal window git checkout abc123