跳到主要内容

Git 常用命令简介

· 阅读需 8 分钟
XingHunm
Tech Enthusiast

Git 是现代软件开发中最重要的版本控制系统。本文整理了日常开发中常用的 Git 命令,帮助快速查阅和使用。

Git工作流程图

1. 仓库初始化

1.1 创建本地仓库

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

# 创建新目录并初始化仓库
git init <project-name>

1.2 克隆远程仓库

# 克隆默认分支
git clone <url>

# 克隆指定分支到指定目录
git clone <url> -b <branch-name> <local-folder>

# 示例:克隆 dev 分支到 MyProject 目录
git clone https://github.com/user/repo.git -b dev MyProject

2. 配置管理

Git 配置有三个级别:

  • 系统级别/etc/gitconfig - 对所有用户生效
  • 用户级别~/.gitconfig - 对当前用户生效
  • 项目级别.git/config - 只对当前项目生效

优先级:项目级别 > 用户级别 > 系统级别

2.1 基本配置命令

# 查看所有配置
git config --list

# 编辑配置文件,--global:适用于当前用户, --system:适用于所有用户,不带参数: 当前项目
git config -e [--global|--system]

# 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 删除配置
git config --unset --global user.name
git config --unset --global user.email

# 设置大小写敏感
git config core.ignorecase false

3. 文件操作

3.1 添加文件到暂存区

# 添加指定文件
git add <file1> <file2>

# 添加当前目录所有文件
git add .

# 添加指定目录
git add <directory>

3.2 提交更改

# 提交暂存区文件
git commit -m "commit message"

# 修改上次提交
git commit --amend -m "new message"

# 快速提交(跳过暂存区)
git commit -am "commit message"

3.3 文件管理

# 文件重命名(大小写敏感)
git mv <oldfile> <newfile>

# 删除文件并从暂存区移除
git rm <file>

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

# 清理未跟踪的文件
git clean -f # 删除文件
git clean -df # 删除文件和目录

3.4 撤销操作

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

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

# 撤销某次提交(创建新的反向提交)
git revert <commit-hash>

4. 查看信息

4.1 状态和历史

# 查看工作区状态
git status

# 查看提交历史
git log
git log --oneline # 简洁格式
git log --graph # 图形化显示分支
git log --pretty=oneline # 单行显示

# 查看所有操作记录(包括已删除的提交)
git reflog

# 查看指定文件的提交历史
git log --pretty=oneline <filename>

4.2 查看差异

# 查看工作区与暂存区的差异
git diff

# 查看暂存区与最后一次提交的差异
git diff --cached

# 查看工作区与最后一次提交的差异
git diff HEAD

# 查看两次提交之间的差异
git diff <commit1> <commit2>

# 查看与上次提交的差异
git diff HEAD^

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

5. 分支管理

5.1 分支基本操作

# 查看本地分支
git branch

# 查看远程分支
git branch -r

# 查看所有分支
git branch -a

# 创建新分支
git branch <branch-name>
# 示例:创建功能分支
git branch feature/user-login

# 切换分支
git checkout <branch-name>
# 示例:切换到开发分支
git checkout develop

# 创建并切换分支
git checkout -b <branch-name>
# 示例:创建并切换到新功能分支
git checkout -b feature/payment-system

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

# 删除分支
git branch -d <branch-name> # 安全删除
git branch -D <branch-name> # 强制删除
# 示例:删除已合并的功能分支
git branch -d feature/user-login

# 批量删除包含特定名称的分支
git branch -D `git branch | grep <pattern>`
# 示例:删除所有 feature 分支
git branch -D `git branch | grep feature`

5.2 分支合并

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

# 强制生成合并提交(即使是快进合并)
git merge <branch-name> --no-ff

# 压缩合并(将多个提交压缩为一个)
git merge --squash <branch-name>
git commit -m "合并 <branch-name> 分支"

# 选择性合并指定提交
git cherry-pick <commit-hash>

6. 远程仓库

6.1 远程仓库管理

# 查看远程仓库
git remote

# 显示详细信息
git remote -v
# 输出示例:
# origin git@github.com:user/repo.git (fetch)
# origin git@github.com:user/repo.git (push)

# 添加远程仓库
git remote add <name> <url>

# 修改远程仓库地址
git remote set-url <name> <new-url>

# 删除远程仓库关联
git remote remove <name>

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

# 实际使用例子:
# git remote add upstream https://github.com/original/repo.git
# git remote set-url origin git@github.com:newuser/repo.git
# git remote remove upstream
# git remote rename origin main-repo

6.2 数据同步

# 获取远程数据(不自动合并)
git fetch
git fetch <remote>
git fetch <remote> <branch>:<local-branch>

# 拉取并合并
git pull
git pull <remote> <branch>

# 设置上游分支
git pull --set-upstream <remote> <branch>

# 使用 rebase 方式拉取(保持线性历史)
git pull --rebase

# 推送到远程
git push
git push <remote> <branch>
git push <remote> <local-branch>:<remote-branch>

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

# 推送标签
git push <remote> <tag>
git push <remote> --tags # 推送所有标签

7. 版本回退与暂存

7.1 暂存工作区

# 暂存当前工作区
git stash
git stash save "描述信息"

# 查看暂存列表
git stash list

# 应用暂存
git stash apply # 应用最新暂存
git stash apply stash@{n} # 应用指定暂存

# 弹出暂存(应用并删除)
git stash pop

# 删除暂存
git stash drop stash@{n} # 删除指定暂存
git stash clear # 清空所有暂存

7.2 版本回退

# 查看文件的版本历史
git reflog <filename>

# 回退单个文件到指定版本
git reset <commit-hash> <filename>

# 软回退(保留工作区和暂存区)
git reset --soft <commit-hash>

# 混合回退(保留工作区,清空暂存区)- 默认方式
git reset --mixed <commit-hash>

# 硬回退(清空工作区和暂存区)
git reset --hard <commit-hash>

8. 标签管理

# 创建轻量级标签
git tag <tag-name>
# 示例:创建版本标签
git tag v1.0.0

# 创建带注释的标签
git tag -a <tag-name> -m "标签描述"
# 示例:创建发布版本标签
git tag -a v1.1.0 -m "发布版本 1.1.0,修复了关键bug"

# 查看标签信息
git show <tag-name>
# 示例:查看标签详情
git show v1.0.0

# 列出所有标签
git tag

# 删除标签
git tag -d <tag-name>
# 示例:删除错误的标签
git tag -d v1.0.0-beta

# 推送标签到远程
git push origin <tag-name>
git push origin --tags # 推送所有标签
# 示例:推送特定标签
git push origin v1.1.0

9. 高级操作

9.1 不同仓库分支合并

当需要将不同仓库的分支合并时:

# 1. 添加远程仓库
git remote add <other-repo-name> <other-repo-url>

# 2. 获取远程分支
git fetch <other-repo-name> <branch>:<local-branch>

# 3. 合并(允许不相关历史)
git merge --allow-unrelated-histories <local-branch>

9.2 实用技巧

# 查看命令帮助
git help <command>

# 创建 .gitkeep 文件保留空目录
touch .gitkeep

# 忽略文件配置 (.gitignore)
echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore

# 临时忽略已跟踪文件的修改
git update-index --skip-worktree <file>

# 恢复跟踪
git update-index --no-skip-worktree <file>

10. Git 工作流程说明

  1. 工作区 (Working Directory): 你正在编辑的文件
  2. 暂存区 (Staging Area): 使用 git add 添加的文件
  3. 版本库 (Repository): 使用 git commit 提交的文件
  4. 远程仓库 (Remote Repository): 使用 git push 推送的文件

理解这四个区域的关系是掌握 Git 的关键。

11. 常见问题解决

11.1 git pull 和 git pull --rebase 的区别

# 普通 pull (会产生合并提交)
git pull

# Rebase pull (保持线性历史)
git pull --rebase

提交线图对比说明:

假设提交线图在执行 pull 前是这样的:

C1━━━D1━━━E1 remotes/origin/master
/
A━━━B━━━C2━━━D2 master

执行 git pull 则提交线图会变成这样:

C1━━━D1━━━E1 remotes/origin/master
/ \
A━━━B━━━C2━━━D2━━━F master

即会产生一个多余的合并历史 F。

如果执行 git pull --rebase 则提交线图会变成这样:

remotes/origin/master
|
A━━━B━━━C1━━━D1━━━E1━━━C2'━━━D2' master

可以看到,应用 rebase 则不会有新的提交 F。C2,D2 删除后重新提交为 C2',D2'了。

使用 --rebase 可以避免不必要的合并提交,保持提交历史的整洁。


提示: 建议在重要操作前先备份代码,熟练掌握这些命令将大大提高开发效率。实际使用中可以根据具体场景调整命令参数。