Git基础命令速查表
1. 仓库初始化与配置
初始化仓库
git init
git clone <repository-url>
git clone https://gitee.com/username/repository.git
配置用户信息
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱@example.com"
git config --list
git config user.name
git config user.email
2. 基本操作
查看状态
git status
git diff
git diff --staged
添加文件
git add <filename>
git add file1.txt file2.txt
git add .
git add -u
提交更改
git commit -m "提交信息"
git commit -am "提交信息"
查看历史
git log
git log --oneline
git log --graph
git log -p
3. 分支操作
分支管理
git branch
git branch -a
git branch <branch-name>
git branch feature/new-feature
git checkout <branch-name>
git checkout -b <branch-name>
git branch -d <branch-name>
git branch -D <branch-name>
合并分支
git merge <branch-name>
git add .
git commit -m "解决合并冲突"
4. 远程仓库操作
远程仓库管理
git remote -v
git remote add origin <repository-url>
git push origin <branch-name>
git push -u origin main
git pull origin <branch-name>
git fetch origin
标签管理
git tag <tag-name>
git tag -a v1.0.0 -m "版本1.0.0"
git push origin <tag-name>
git push origin --tags
5. 撤销操作
撤销工作区更改
git checkout -- <filename>
git restore <filename>
git checkout -- .
git restore .
撤销暂存区
git reset HEAD <filename>
git restore --staged <filename>
撤销提交
git reset --soft HEAD^
git reset --hard HEAD^
git commit --amend
6. 常用技巧
暂存工作
git stash
git stash save "工作描述"
git stash list
git stash pop
git stash apply stash@{0}
查看信息
git blame <filename>
git log --graph --oneline --all
git branch -r
7. 常见场景
首次使用Git
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
ssh-keygen -t rsa -C "你的邮箱"
日常开发流程
git pull origin main
git checkout -b feature/new-feature
git add .
git commit -m "添加新功能"
git push origin feature/new-feature
解决冲突
git pull origin main
git add .
git commit -m "解决冲突"
git push origin <branch-name>