Git 常用命令速查手册

Git 常用命令速查手册

Git 是目前最流行的分布式版本控制系统,以下是日常开发中最常用的命令整理。

基础操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 初始化仓库
git init

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

# 查看状态
git status

# 添加文件到暂存区
git add <file>
git add . # 添加所有更改

# 提交更改
git commit -m "commit message"

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

分支管理

1
2
3
4
5
6
7
8
9
10
11
# 查看分支
git branch -a

# 创建并切换分支
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 remote -v

# 推送到远程
git push origin <branch-name>

# 拉取远程更新
git pull origin <branch-name>

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

撤销操作

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

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

# 回退到某个提交
git reset --hard <commit-id>

实用技巧

1
2
3
4
5
6
7
8
9
# 暂存当前更改
git stash
git stash pop

# 查看某个文件的修改历史
git log -p <file>

# 创建标签
git tag -a v1.0 -m "version 1.0"

💡 提示: 建议将常用命令设置为 Git alias,提高效率!


更多 Git 进阶用法将在后续文章中分享,敬请关注!