Git常用命令

一、Git基础配置

在开始使用Git之前,需要进行一些基础配置:

# 设置全局用户名
git config --global user.name "Your Name"

# 设置全局邮箱
git config --global user.email "your.email@example.com"

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

# 设置默认编辑器为VS Code
git config --global core.editor "code --wait"

二、仓库创建与克隆

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

# 初始化并指定目录名
git init <directory-name>
  1. 克隆现有仓库
# 克隆远程仓库
git clone <repository-url>

# 克隆指定分支
git clone -b <branch-name> <repository-url>

# 克隆并指定本地目录名
git clone <repository-url> <directory-name>

三、基本工作流程

Git的基本工作流程包括:修改文件、暂存更改、提交更改。

# 查看仓库状态
git status

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

# 添加所有更改文件
git add .

# 提交暂存区的更改
git commit -m "提交信息"

# 一次性添加并提交所有更改(不包括未跟踪文件)
git commit -a -m "提交信息"

四、分支管理

分支是Git的核心功能之一,允许多线开发。

# 查看所有分支
git branch

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

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

# 创建并切换到新分支
git checkout -b <branch-name>

# 删除分支(已合并)
git branch -d <branch-name>

# 强制删除分支(未合并)
git branch -D <branch-name>

# 重命名当前分支
git branch -m <new-name>

五、远程仓库操作

# 查看远程仓库
git remote -v

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

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

# 移除远程仓库
git remote remove <remote-name>

# 获取远程仓库更新但不合并
git fetch <remote-name>

# 拉取远程分支并合并
git pull <remote-name> <branch-name>

# 推送本地分支到远程
git push <remote-name> <branch-name>

# 推送并设置上游分支
git push -u <remote-name> <branch-name>

六、撤销与回退

# 撤销工作区修改(未暂存)
git checkout -- <file-name>

# 撤销暂存区的修改(已add未commit)
git reset HEAD <file-name>

# 修改最后一次提交(未推送)
git commit --amend

# 回退到指定提交(保留更改)
git reset <commit-hash>

# 回退到指定提交(丢弃更改)
git reset --hard <commit-hash>

# 回退远程分支(谨慎使用)
git push -f <remote-name> <branch-name>

七、日志与历史

# 查看提交历史
git log

# 简洁显示提交历史
git log --oneline

# 显示分支图
git log --graph --oneline --all

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

# 查看某人的提交
git log --author="name"

# 搜索提交信息
git log --grep="keyword"

# 显示更改统计
git log --stat

八、标签管理

# 列出所有标签
git tag

# 创建轻量标签
git tag <tag-name>

# 创建含注释的标签
git tag -a <tag-name> -m "message"

# 推送标签到远程
git push <remote-name> <tag-name>

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

# 删除本地标签
git tag -d <tag-name>

# 删除远程标签
git push <remote-name> :refs/tags/<tag-name>

九、高级操作

  1. 储藏更改
# 储藏当前工作目录
git stash

# 查看储藏列表
git stash list

# 恢复最近储藏
git stash pop

# 恢复指定储藏
git stash apply stash@{n}

# 删除储藏
git stash drop stash@{n}
  1. 变基操作
# 变基当前分支到目标分支
git rebase <target-branch>

# 交互式变基
git rebase -i <commit-hash>
  1. 子模块
# 添加子模块
git submodule add <repository-url> <path>

# 初始化子模块
git submodule init

# 更新子模块
git submodule update

十、差异比较

git diff                # 工作区与暂存区差异
git diff --cached       # 暂存区与最新提交差异
git diff HEAD           # 工作区与最新提交差异
git diff <commit1> <commit2> # 比较两个提交之间的差异。可以使用提交哈希、分支名或标签名。
git diff <commit1> <commit2> -- <file-path>  # 比较特定文件的差异

# 查看当前分支比master多了哪些修改
git diff master..HEAD
# 查看feature分支比develop分支少了哪些内容
git diff develop..feature
#显示变更的统计信息,包括哪些文件被修改以及增删行数。
git diff --stat

十一、精选提交 cherry-pick

# 切换到目标分支
git checkout main

# 精选单个提交
git cherry-pick <commit-hash>

# 精选多个提交(按顺序应用)
git cherry-pick <commit1> <commit2>
# 精选但不自动提交(允许修改)
git cherry-pick -n <commit-hash>

# 精选并编辑提交信息
git cherry-pick -e <commit-hash>

# 精选并签名提交
git cherry-pick -s <commit-hash>

# 精选一个范围内的提交(不包括start)
git cherry-pick <start-commit>..<end-commit>

# 精选一个范围内的提交(包括start)
git cherry-pick <start-commit>^..<end-commit>

滚动至顶部