Git常用命令

Git 常用命令参考

本文档涵盖了 Git 从基础配置到进阶操作的常用命令速查。

1. 环境配置

在开始使用 Git 前,通常需要设置用户名和邮箱,这些信息将包含在每一次提交中。

1
2
3
4
git config --global user.name "Your Name"       # 设置全局用户名
git config --global user.email "email@example.com" # 设置全局邮箱
git config --global core.editor vim # 设置默认编辑器
git config --list # 查看当前配置

2. 基础操作

初始化与克隆

1
2
3
git init                    # 初始化本地仓库
git clone <url> # 克隆远程仓库到本地
git clone <url> <folder> # 克隆到指定目录

文件状态与查看

1
2
3
4
git status                  # 查看工作区和暂存区状态
git diff # 查看工作区与暂存区的差异
git diff --staged # 查看暂存区与上一次提交的差异
git log --oneline # 查看简洁的提交历史

暂存与提交

1
2
3
4
5
6
git add <file>              # 添加指定文件到暂存区
git add . # 添加当前目录下所有变更
git add -A # 添加所有变更(包括删除操作)
git commit -m "message" # 提交暂存区内容
git commit -am "message" # 跳过暂存,直接提交所有已跟踪文件的修改
git commit --amend # 修改最后一次提交(例如修改注释或补充文件)

3. 分支管理

💡 提示:Git 2.23+ 引入了 git switchgit restore,用来替代部分 git checkout 的功能,语义更清晰。

创建与切换

1
2
3
4
5
6
7
8
9
10
11
12
13
git branch                  # 查看本地分支
git branch -r # 查看远程分支
git branch <name> # 创建新分支
git branch -d <name> # 删除已合并的分支
git branch -D <name> # 强制删除分支(未合并也可删除)

# 切换分支 (传统方式)
git checkout <name>
git checkout -b <name> # 创建并切换到新分支

# 切换分支 (推荐方式 Git 2.23+)
git switch <name> # 切换到已有分支
git switch -c <name> # 创建并切换到新分支

合并与变基

1
2
3
git merge <name>           # 合并指定分支到当前分支
git merge --no-ff <name> # 禁用快进合并,保留分支历史
git rebase <name> # 将当前分支变基到指定分支(使历史更线性)

4. 远程操作

1
2
3
4
5
6
7
8
9
10
11
git remote -v              # 查看远程仓库详细信息
git remote add origin <url> # 添加远程仓库
git remote remove origin # 删除远程仓库

git push origin <branch> # 推送本地分支到远程
git push -u origin <branch># 推送并建立跟踪关系(首次推送推荐)
git push origin --delete <branch> # 删除远程分支

git pull origin <branch> # 拉取远程代码并自动合并
git pull --rebase origin <branch> # 拉取代码并使用 rebase 合并
git fetch origin # 获取远程更新,但不合并

5. 撤销与回退

这是最关键的部分,误操作时的救命稻草。

恢复文件

1
2
3
4
5
6
7
# 撤销工作区的修改(恢复到暂存区或上一次提交的状态)
git restore <file> # Git 2.23+ 新命令
git checkout -- <file> # 传统命令

# 取消暂存(从暂存区移回工作区)
git restore --staged <file> # Git 2.23+ 新命令
git reset HEAD <file> # 传统命令

重置提交

1
2
3
git reset --soft HEAD~1    # 撤销最后一次提交,保留修改在暂存区
git reset --mixed HEAD~1 # 撤销最后一次提交,保留修改在工作区(默认)
git reset --hard HEAD~1 # 撤销最后一次提交,**彻底丢弃**所有修改

安全回退

1
git revert <commit-id>     # 创建一个新提交来撤销指定的提交

6. 暂存

当你正在开发新功能,需要临时切换去修复 Bug,但又不想提交当前半成品的代码时使用。

1
2
3
4
5
6
git stash                  # 将当前工作区修改暂存起来
git stash save "message" # 暂存并添加备注
git stash list # 查看暂存列表
git stash pop # 恢复最近一次暂存并删除记录
git stash apply # 恢复最近一次暂存但不删除记录
git stash drop # 删除最近一次暂存记录

7. 标签管理

常用于标记版本号(如 v1.0.0)。

1
2
3
4
5
6
7
git tag                    # 查看所有标签
git tag <name> # 创建轻量标签
git tag -a <name> -m "msg" # 创建附注标签(推荐)
git tag -d <name> # 删除本地标签

git push origin <name> # 推送指定标签到远程
git push origin --tags # 推送所有标签到远程

8. 常用技巧与场景

查看指定文件的修改历史

1
2
git log -p <file>          # 查看文件每一次提交的详细修改
git blame <file> # 查看文件每一行是谁修改的

忽略文件权限变更

如果你在 Windows 和 Linux 之间切换,可能会遇到文件权限变更导致的 diff,可以使用:

1
git config core.fileMode false

解决冲突后继续

git mergegit rebase 出现冲突时:

  1. 手动编辑文件解决冲突。
  2. git add <file> 标记为已解决。
  3. git commit (merge时) 或 git rebase --continue (rebase时)。

别名配置

为了提高效率,可以配置简写命令:

1
2
3
4
5
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
# 使用: git st 等同于 git status
1