Git 常用命令大全

Git 常用命令大全

1. 仓库创建与克隆

命令 说明
git init 初始化新仓库
git clone <repo_url> 克隆远程仓库
git clone --depth 1 <repo_url> 浅克隆(只获取最新版本)

2. 基本操作

命令 说明
git status 查看仓库状态
git add <file> 添加文件到暂存区
git add . 添加所有修改文件
git commit -m "message" 提交更改
git commit -am "message" 添加并提交所有已跟踪文件的修改
git restore <file> 撤销工作区修改
git restore --staged <file> 撤销暂存区修改

3. 分支操作

命令 说明
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 rebase <branch_name> 变基操作

4. 远程操作

命令 说明
git remote -v 查看远程仓库
git remote add <name> <url> 添加远程仓库
git remote remove <name> 移除远程仓库
git fetch <remote> 获取远程更新
git pull <remote> <branch> 拉取并合并远程分支
git push <remote> <branch> 推送本地分支到远程
git push -u <remote> <branch> 推送并设置上游分支
git push --force 强制推送(慎用)

5. 查看信息

命令 说明
git log 查看提交历史
git log --oneline 简洁版提交历史
git log --graph 图形化显示分支历史
git show <commit> 显示某次提交的详情
git diff 查看未暂存的更改
git diff --staged 查看已暂存的更改
git blame <file> 查看文件修改历史

6. 撤销与重置

命令 说明
git reset --soft <commit> 重置到指定提交(保留更改)
git reset --mixed <commit> 重置到指定提交(取消暂存)
git reset --hard <commit> 彻底重置到指定提交
git revert <commit> 撤销指定提交
git clean -fd 删除未跟踪的文件和目录

7. 标签操作

命令 说明
git tag 查看所有标签
git tag <tag_name> 创建轻量标签
git tag -a <tag_name> -m "message" 创建带注释标签
git push origin <tag_name> 推送标签到远程
git push origin --tags 推送所有标签
git tag -d <tag_name> 删除本地标签
git push origin :refs/tags/<tag_name> 删除远程标签

8. 储藏与清理

命令 说明
git stash 储藏当前工作
git stash list 查看储藏列表
git stash apply 恢复最近储藏
git stash pop 恢复并删除储藏
git stash drop 删除储藏
git stash clear 清除所有储藏

9. 子模块

命令 说明
git submodule add <repo_url> 添加子模块
git submodule update --init 初始化并更新子模块
git submodule update --remote 更新子模块到最新提交

10. 配置相关

命令 说明
git config --list 查看所有配置
git config --global user.name "name" 设置全局用户名
git config --global user.email "email" 设置全局邮箱
git config --global core.editor "vim" 设置默认编辑器

11. 其他实用命令

命令 说明
git reflog 查看所有操作记录
git cherry-pick <commit> 选择性地合并提交
git bisect 二分查找引入bug的提交
git grep "pattern" 在代码库中搜索内容
git fsck 检查仓库完整性

使用技巧

  1. 简化常用命令:

    bash 复制代码
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
  2. 查看帮助:

    复制代码
    git help <command>
    git <command> --help
  3. 忽略文件权限变化:

    bash 复制代码
    git config core.fileMode false
相关推荐
`流年づ2 天前
联邦学习-安全聚合
git·安全·github
喜欢吃燃面2 天前
Git 原理与使用(三):标签、多人协作与 Git Flow 落地
大数据·git·elasticsearch
喜欢吃燃面2 天前
Git 原理与使用(二):分支是平行宇宙,远程仓库是共享时空
大数据·git·elasticsearch
怪我冷i2 天前
Git主备分离,双仓库发布
git
解道Jdon2 天前
Rust重写OpenCode Rune:性能提升最高100倍
ide·windows·git·svn·eclipse·github·visual studio
黎羽_3 天前
终端/办公工具第二组:timewarrior + git-filter-repo + pre-commit + trafilatura + monolith
git·终端工具·开发效率·实战复盘
skywalk81633 天前
git上传的时候,它这个版的提交,是否包括了以前的改动? 比如下面这个,它前面的一些改动,会自动因为f91285c70dde 提交而提交吗?
git
Haooog4 天前
OpenHands 定时任务同步后还是关着?先查 Git Sync 的启停开关
git·agent·定时任务·配置管理·openhands
崖边看雾5 天前
Git 手册:三区、分支、撤销、冲突与团队协作
大数据·git·elasticsearch
李永奉5 天前
Git和GitHub-VS Code 图形化 Git 放弃更改、暂存与取消暂存、撤销上一次提交(VS Code 界面操作 + 命令行对照)
git·github