Git常用命令速查

Git 是程序员必备的版本控制工具,本文整理了一些常用的 Git 命令,方便日常查阅。

基础配置

1
2
3
4
5
6
# 设置用户名和邮箱
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

# 查看配置
git config --list

常用命令

克隆与初始化

1
2
3
4
5
# 克隆远程仓库
git clone <url>

# 初始化本地仓库
git init

日常操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看状态
git status

# 添加文件到暂存区
git add .
git add <file>

# 提交更改
git commit -m "提交信息"

# 推送到远程
git push origin main

# 拉取更新
git pull origin main

分支管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看分支
git branch

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

# 切换分支
git checkout <branch-name>

# 创建并切换
git checkout -b <branch-name>

# 合并分支
git merge <branch-name>

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

回退操作

1
2
3
4
5
6
7
8
9
10
11
# 撤销工作区修改
git checkout -- <file>

# 撤销暂存区
git reset HEAD <file>

# 回退到上一次提交
git reset --hard HEAD^

# 查看提交历史
git log --oneline

小技巧

  • 使用 git stash 暂存当前工作
  • 使用 git cherry-pick <commit> 选择性合并提交
  • 使用 .gitignore 忽略不需要跟踪的文件

持续更新中…