git命令大全

Git常用命令大全

基础配置

配置用户信息

# 设置全局用户名
git config --global user.name "Your Name"

# 设置全局邮箱
git config --global user.email "your.email@example.com"

# 查看配置信息
git config --list

# 编辑配置文件
git config -e --global

配置别名

# 设置常用命令别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

仓库操作

初始化仓库

# 在当前目录初始化Git仓库
git init

# 克隆远程仓库
git clone <repository-url>

# 克隆指定分支
git clone -b <branch-name> <repository-url>

仓库信息

# 查看仓库状态
git status

# 查看提交历史
git log

# 简洁的提交历史
git log --oneline

# 图形化显示分支历史
git log --graph --oneline --all

文件操作

添加文件到暂存区

# 添加指定文件到暂存区
git add <file-name>

# 添加所有修改和新增的文件
git add .

# 添加所有文件(包括删除的文件)
git add -A

# 交互式添加文件
git add -p

移除文件

# 从暂存区移除文件,保留工作区文件
git rm --cached <file-name>

# 彻底删除文件
git rm <file-name>

# 移除目录
git rm -r <directory-name>

分支管理

查看分支

# 列出本地分支
git branch

# 列出远程分支
git branch -r

# 列出所有分支(本地+远程)
git branch -a

# 查看当前所在分支
git branch --show-current

创建分支

# 创建新分支
git branch <branch-name>

# 创建并切换到新分支
git checkout -b <branch-name>
# 或者使用新命令
git switch -c <branch-name>

切换分支

# 切换到指定分支
git checkout <branch-name>
# 或者使用新命令
git switch <branch-name>

# 切换到上一个分支
git checkout -

合并分支

# 将指定分支合并到当前分支
git merge <branch-name>

# 合并时创建新的合并提交
git merge --no-ff <branch-name>

# 取消合并
git merge --abort

删除分支

# 删除本地分支
git branch -d <branch-name>

# 强制删除本地分支
git branch -D <branch-name>

# 删除远程分支
git push origin --delete <branch-name>

提交操作

提交更改

# 提交暂存区的更改
git commit -m "commit message"

# 提交所有修改(跳过git add)
git commit -a -m "commit message"

# 修改最后一次提交
git commit --amend -m "new commit message"

# 修改提交但不修改提交信息
git commit --amend --no-edit

提交日志

# 查看指定文件的提交历史
git log <file-name>

# 查看两个提交之间的差异
git log <commit1>..<commit2>

# 查看某段时间的提交
git log --since="2 weeks ago" --until="1 week ago"

远程仓库

添加远程仓库

# 添加远程仓库
git remote add origin <repository-url>

# 查看远程仓库
git remote -v

# 重命名远程仓库
git remote rename <old-name> <new-name>

# 移除远程仓库
git remote remove <remote-name>

推送代码

# 推送本地分支到远程
git push origin <branch-name>

# 推送所有分支
git push --all origin

# 推送标签
git push --tags

# 删除远程分支
git push origin --delete <branch-name>

拉取代码

# 从远程获取最新代码
git pull origin <branch-name>

# 获取远程更新但不合并
git fetch origin

# 合并远程分支
git merge origin/<branch-name>

状态查看

查看状态

# 查看工作区和暂存区状态
git status

# 查看文件差异
git diff

# 查看暂存区和HEAD的差异
git diff --cached

# 查看工作区和HEAD的差异
git diff HEAD

查看对象

# 查看指定提交的详细信息
git show <commit-hash>

# 查看分支的最后一次提交
git show <branch-name>

# 查看标签信息
git show <tag-name>

撤销操作

撤销工作区修改

# 撤销工作区指定文件的修改
git checkout -- <file-name>

# 撤销工作区所有修改
git checkout .

# 使用新命令撤销
git restore <file-name>

撤销暂存区修改

# 将文件从暂存区移回工作区
git reset HEAD <file-name>

# 使用新命令撤销
git restore --staged <file-name>

撤销提交

# 撤销最后一次提交,保留修改
git reset --soft HEAD~1

# 撤销最后一次提交,不保留修改
git reset --hard HEAD~1

# 撤销到指定提交
git reset --hard <commit-hash>

标签管理

创建标签

# 创建轻量标签
git tag <tag-name>

# 创建附注标签
git tag -a <tag-name> -m "tag message"

# 为指定提交创建标签
git tag <tag-name> <commit-hash>

查看标签

# 列出所有标签
git tag

# 查看指定标签的详细信息
git show <tag-name>

删除标签

# 删除本地标签
git tag -d <tag-name>

# 删除远程标签
git push origin --delete <tag-name>

合并与冲突

合并策略

# 使用rebase方式合并
git pull --rebase origin <branch-name>

# 合并时使用ours策略
git merge -X ours <branch-name>

# 合并时使用theirs策略
git merge -X theirs <branch-name>

解决冲突

# 手动编辑冲突文件后
git add <conflicted-file>
git commit -m "resolve merge conflict"

其他实用命令

搜索

# 在提交历史中搜索文本
git log -S "search-text"

# 搜索提交信息
git log --grep="pattern"

储藏

# 储藏当前修改
git stash

# 查看储藏列表
git stash list

# 应用最近的储藏
git stash apply

# 弹出最近的储藏
git stash pop

# 应用指定的储藏
git stash apply stash@{n}

重命名

# 重命名文件并添加到暂存区
git mv <old-name> <new-name>

忽略文件

# 创建.gitignore文件
touch .gitignore

# 常见忽略模式
# node_modules/
# *.log
# .env
# dist/

清理

# 清理未跟踪的文件
git clean -n  # 先预览
git clean -f  # 确认后执行

# 清理未跟踪的目录
git clean -fd

常用组合命令

日常开发流程

# 1. 更新代码
git pull origin main

# 2. 创建新功能分支
git checkout -b feature/new-feature

# 3. 开发完成后提交
git add .
git commit -m "add new feature"

# 4. 推送到远程
git push origin feature/new-feature

修复紧急bug

# 1. 储藏当前修改
git stash

# 2. 切换到主分支
git checkout main

# 3. 创建修复分支
git checkout -b hotfix/bug-fix

# 4. 修复后提交
git add .
git commit -m "fix critical bug"

# 5. 推送到远程
git push origin hotfix/bug-fix

# 6. 回到原分支继续开发
git checkout -
git stash pop