git命令

git 命令 #

git是一个分布式版本控制系统,用于跟踪文件的更改,协调多人协作开发项目。它允许开发者保存项目的多个版本,查看更改历史,以及在不同版本之间切换。

语法 #

git [--version] [--help] [-C <path>] [-c <name>=<value>]
    [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
    [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
    [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
    [--super-prefix=<path>] [--config-env=<name>=<envvar>]
    <command> [<args>]

基本配置 #

设置用户信息 #

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

查看配置 #

git config --list

设置默认编辑器 #

git config --global core.editor "vim"

基本命令 #

初始化仓库 #

git init

克隆仓库 #

git clone <repository-url>

克隆特定分支:

git clone -b <branch-name> <repository-url>

查看状态 #

git status

添加文件到暂存区 #

添加特定文件:

git add <file-name>

添加所有更改:

git add .

提交更改 #

git commit -m "Commit message"

添加并提交所有更改:

git commit -am "Commit message"

查看提交历史 #

git log

简洁显示:

git log --oneline

图形化显示:

git log --graph --oneline --decorate

查看更改 #

查看工作区与暂存区的差异:

git diff

查看暂存区与最后一次提交的差异:

git diff --staged

分支操作 #

列出分支 #

列出本地分支:

git branch

列出所有分支(包括远程):

git branch -a

创建分支 #

git branch <branch-name>

切换分支 #

git checkout <branch-name>

创建并切换到新分支:

git checkout -b <branch-name>

合并分支 #

git merge <branch-name>

删除分支 #

删除已合并的分支:

git branch -d <branch-name>

强制删除分支:

git branch -D <branch-name>

远程仓库操作 #

添加远程仓库 #

git remote add <name> <url>

例如:

git remote add origin https://github.com/username/repository.git

查看远程仓库 #

git remote -v

从远程仓库获取更新 #

git fetch <remote>

拉取更新并合并 #

git pull <remote> <branch>

推送到远程仓库 #

git push <remote> <branch>

首次推送并设置上游分支:

git push -u <remote> <branch>

删除远程分支 #

git push <remote> --delete <branch>

撤销更改 #

撤销工作区更改 #

git checkout -- <file>

或使用更新的语法:

git restore <file>

撤销暂存区更改 #

git reset HEAD <file>

或使用更新的语法:

git restore --staged <file>

修改最后一次提交 #

git commit --amend

重置到特定提交 #

软重置(保留更改在工作区):

git reset --soft <commit>

混合重置(保留更改但不在暂存区):

git reset --mixed <commit>

硬重置(丢弃所有更改):

git reset --hard <commit>

还原提交 #

创建一个新的提交来撤销指定提交的更改:

git revert <commit>

标签操作 #

列出标签 #

git tag

创建标签 #

创建轻量标签:

git tag <tag-name>

创建附注标签:

git tag -a <tag-name> -m "Tag message"

查看标签信息 #

git show <tag-name>

推送标签到远程 #

推送特定标签:

git push <remote> <tag-name>

推送所有标签:

git push <remote> --tags

删除标签 #

删除本地标签:

git tag -d <tag-name>

删除远程标签:

git push <remote> :refs/tags/<tag-name>

高级操作 #

储藏更改 #

储藏当前更改:

git stash

储藏并添加消息:

git stash save "Stash message"

列出储藏:

git stash list

应用最近的储藏:

git stash apply

应用特定储藏:

git stash apply stash@{n}

应用并删除最近的储藏:

git stash pop

删除储藏:

git stash drop stash@{n}

变基 #

git rebase <branch>

交互式变基:

git rebase -i <commit>

拣选提交 #

git cherry-pick <commit>

查找引入Bug的提交 #

git bisect start
git bisect bad    # 当前版本有问题
git bisect good <commit>  # 已知的好版本
# Git会检出中间的提交,然后你测试并标记为good或bad
git bisect good  # 或 git bisect bad
# 重复直到找到第一个坏提交
git bisect reset  # 完成后重置

子模块 #

添加子模块:

git submodule add <repository-url> [path]

初始化子模块:

git submodule init

更新子模块:

git submodule update

克隆包含子模块的仓库:

git clone --recursive <repository-url>

清理工作区 #

git clean -f  # 删除未跟踪的文件
git clean -fd  # 删除未跟踪的文件和目录
git clean -n  # 预览将被删除的文件

工作流示例 #

基本工作流 #

# 克隆仓库
git clone https://github.com/username/repository.git
cd repository

# 创建功能分支
git checkout -b feature-branch

# 进行更改
echo "New feature" > feature.txt
git add feature.txt
git commit -m "Add new feature"

# 推送到远程
git push -u origin feature-branch

# 切换回主分支
git checkout main

# 拉取最新更改
git pull

# 合并功能分支
git merge feature-branch

# 推送到远程
git push

# 删除功能分支
git branch -d feature-branch
git push origin --delete feature-branch

处理合并冲突 #

# 当合并出现冲突时
git merge feature-branch
# 编辑冲突文件解决冲突
vim conflicted-file.txt
# 标记为已解决
git add conflicted-file.txt
# 完成合并
git commit

常用选项和标志 #

选项/标志 描述
--global 应用于全局Git配置
--local 应用于当前仓库配置(默认)
-f, --force 强制执行操作
-v, --verbose 显示详细输出
-q, --quiet 抑制输出
-n, --dry-run 预览操作而不实际执行
--all 应用于所有项目
--no-verify 跳过钩子脚本

提示和技巧 #

1. 创建别名 #

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 config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

2. 使用.gitignore #

创建.gitignore文件来忽略不需要版本控制的文件:

# 忽略编译输出
*.o
*.out

# 忽略日志文件
*.log

# 忽略目录
node_modules/
dist/

3. 自动修复行尾 #

git config --global core.autocrlf true  # Windows
git config --global core.autocrlf input  # Mac/Linux

4. 设置默认拉取行为 #

git config --global pull.rebase false  # 合并(默认)
git config --global pull.rebase true   # 变基
git config --global pull.ff only       # 仅快进

5. 保存凭证 #

git config --global credential.helper cache  # 临时保存
git config --global credential.helper store  # 永久保存(明文)

6. 查找特定更改 #

git log -S "search string"  # 搜索添加或删除特定字符串的提交
git log -p -- path/to/file  # 查看特定文件的更改历史

7. 使用reflog恢复丢失的提交 #

git reflog  # 查看引用日志
git checkout HEAD@{2}  # 检出之前的状态

常见问题排查 #

1. 无法推送到远程 #

# 拉取最新更改
git pull --rebase origin main
# 然后再次尝试推送
git push origin main

2. 意外提交到错误分支 #

# 保存当前提交的哈希
git log -n 1 --pretty=format:"%H"
# 切换到正确的分支
git checkout correct-branch
# 拣选提交
git cherry-pick <commit-hash>
# 返回原分支并重置
git checkout wrong-branch
git reset --hard HEAD~1

3. 撤销已推送的提交 #

# 创建一个新提交来撤销更改
git revert <commit>
git push origin main

4. 清理大型二进制文件 #

# 查找大文件
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
# 从历史中删除(需要重写历史)
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path/to/large/file' --prune-empty --tag-name-filter cat -- --all

与其他版本控制系统的比较 #

特性 Git SVN Mercurial
类型 分布式 集中式 分布式
分支操作 轻量、快速 重量级 轻量、快速
离线工作 完全支持 有限支持 完全支持
存储模型 快照 差异 修订集
学习曲线 较陡 较平缓 中等
性能 非常快 中等 快
大型仓库 良好支持 可能变慢 良好支持

扩展阅读 #