当前位置: 首页 > news >正文

【git】git基础用法指南

文章目录

    • Git 简介
      • Git vs 其他版本控制系统
      • 基础配置
    • 基础概念
      • Git 的三个区域
      • 文件状态
      • Git 对象类型
    • 仓库操作
      • 创建仓库
      • 仓库信息
    • 文件操作
      • 添加文件到暂存区
      • 提交更改
      • 查看差异
      • 删除和移动文件
    • 分支管理
      • 分支基础操作
      • 分支管理
      • 分支合并
    • 远程仓库
      • 远程仓库管理
      • 推送和拉取
      • 跟踪远程分支
    • 标签管理
      • 创建标签
      • 标签操作
    • 总结

Git 简介

Git 是一个分布式版本控制系统,由 Linus Torvalds 在 2005 年创建。它具有以下特点:

  • 分布式:每个开发者都有完整的代码历史
  • 高性能:快速的分支创建和合并
  • 数据完整性:使用 SHA-1 哈希确保数据完整性
  • 非线性开发:支持复杂的分支和合并策略

Git vs 其他版本控制系统

特性GitSVNCVS
分布式
离线工作
分支成本
合并能力
性能

基础配置

# 设置用户信息(必需)gitconfig--globaluser.name"Your Name"gitconfig--globaluser.email"your.email@example.com"# 设置默认编辑器gitconfig--globalcore.editor"code --wait"# VS Codegitconfig--globalcore.editor"vim"# Vimgitconfig--globalcore.editor"nano"# Nano# 设置默认分支名gitconfig--globalinit.defaultBranch main# 设置行尾处理(Windows 推荐)gitconfig--globalcore.autocrlftrue# 设置行尾处理(macOS/Linux 推荐)gitconfig--globalcore.autocrlf input# 启用颜色输出gitconfig--globalcolor.ui auto# 设置别名gitconfig--globalalias.st statusgitconfig--globalalias.co checkoutgitconfig--globalalias.br branchgitconfig--globalalias.ci commitgitconfig--globalalias.unstage'reset HEAD --'gitconfig--globalalias.last'log -1 HEAD'gitconfig--globalalias.visual'!gitk'# 查看配置gitconfig--listgitconfig--global--listgitconfig user.name

基础概念

Git 的三个区域

工作区 (Working Directory) ↓ git add 暂存区 (Staging Area/Index) ↓ git commit 版本库 (Repository)

文件状态

未跟踪 (Untracked) ──git add──→ 已暂存 (Staged) ↓ git commit 已跟踪 (Tracked) ←──────────────── 已提交 (Committed) ↓ 修改文件 ↑ 已修改 (Modified) ──git add──────────┘

Git 对象类型

  • Blob:文件内容
  • Tree:目录结构
  • Commit:提交信息
  • Tag:标签引用

仓库操作

创建仓库

# 初始化新仓库gitinitgitinit my-projectgitinit--bare# 创建裸仓库(服务器用)# 克隆现有仓库gitclone https://github.com/user/repo.gitgitclone https://github.com/user/repo.git my-foldergitclone--depth1https://github.com/user/repo.git# 浅克隆gitclone--branchdevelop https://github.com/user/repo.git# 克隆特定分支

仓库信息

# 查看仓库状态gitstatusgitstatus-s# 简短格式gitstatus--porcelain# 机器可读格式# 查看提交历史gitloggitlog--oneline# 单行显示gitlog--graph# 图形化显示gitlog--stat# 显示统计信息gitlog--patch# 显示详细差异gitlog-n5# 显示最近 5 次提交gitlog--since="2 weeks ago"# 时间过滤gitlog--author="John"# 作者过滤gitlog--grep="fix"# 提交信息过滤# 查看文件历史gitlog filenamegitlog-pfilename# 显示文件的详细变更历史# 查看分支图gitlog--graph--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'--abbrev-commit

文件操作

添加文件到暂存区

# 添加单个文件gitaddfilename.txt# 添加多个文件gitaddfile1.txt file2.txt# 添加所有文件gitadd.gitadd-Agitadd--all# 添加所有 .txt 文件gitadd*.txt# 交互式添加gitadd-igitadd-p# 部分添加(patch 模式)# 添加目录gitadddirectory/# 强制添加被忽略的文件gitadd-fignored-file.txt

提交更改

# 基本提交gitcommit-m"提交信息"# 详细提交信息gitcommit# 打开编辑器# 跳过暂存区直接提交gitcommit-a-m"提交信息"gitcommit-am"提交信息"# 修改最后一次提交gitcommit--amendgitcommit--amend-m"新的提交信息"gitcommit--amend--no-edit# 不修改提交信息# 空提交(用于触发 CI/CD)gitcommit --allow-empty-m"Empty commit"# 提交时签名gitcommit-S-m"Signed commit"

查看差异

# 查看工作区与暂存区的差异gitdiff# 查看暂存区与最后一次提交的差异gitdiff--stagedgitdiff--cached# 查看工作区与最后一次提交的差异gitdiffHEAD# 查看两个提交之间的差异gitdiffcommit1 commit2gitdiffHEAD~1 HEAD# 查看特定文件的差异gitdifffilenamegitdiff--stagedfilename# 查看统计信息gitdiff--statgitdiff--numstat# 查看单词级别的差异gitdiff--word-diff

删除和移动文件

# 删除文件gitrmfilenamegitrm-ffilename# 强制删除gitrm--cachedfilename# 从暂存区删除但保留工作区文件# 删除目录gitrm-rdirectory/# 移动/重命名文件gitmvold-name new-namegitmvfile.txt directory/# 等价操作mvold-name new-namegitrmold-namegitaddnew-name

分支管理

分支基础操作

# 查看分支gitbranch# 本地分支gitbranch-r# 远程分支gitbranch-a# 所有分支gitbranch-v# 显示最后一次提交gitbranch-vv# 显示跟踪关系# 创建分支gitbranch new-branchgitbranch new-branch commit-hash# 从特定提交创建# 切换分支gitcheckout branch-namegitswitch branch-name# Git 2.23+# 创建并切换分支gitcheckout-bnew-branchgitswitch-cnew-branch# Git 2.23+# 从远程分支创建本地分支gitcheckout-blocal-branch origin/remote-branchgitswitch-clocal-branch origin/remote-branch

分支管理

# 重命名分支gitbranch-mold-name new-namegitbranch-Mold-name new-name# 强制重命名# 删除分支gitbranch-dbranch-name# 安全删除gitbranch-Dbranch-name# 强制删除gitbranch--deletebranch-name# 删除远程分支gitpush origin--deletebranch-namegitpush origin :branch-name# 设置上游分支gitbranch --set-upstream-to=origin/maingitbranch-uorigin/main# 查看分支合并状态gitbranch--merged# 已合并的分支gitbranch --no-merged# 未合并的分支

分支合并

# 合并分支gitmerge branch-name# 快进合并gitmerge --ff-only branch-name# 禁用快进合并gitmerge --no-ff branch-name# 压缩合并gitmerge--squashbranch-name# 合并时指定提交信息gitmerge-m"Merge message"branch-name# 中止合并gitmerge--abort

远程仓库

远程仓库管理

# 查看远程仓库gitremotegitremote-v# 显示详细信息# 添加远程仓库gitremoteaddorigin https://github.com/user/repo.gitgitremoteaddupstream https://github.com/original/repo.git# 修改远程仓库 URLgitremote set-url origin https://github.com/user/new-repo.git# 重命名远程仓库gitremoterenameorigin new-origin# 删除远程仓库gitremote remove origingitremotermorigin# 查看远程仓库信息gitremote show origin

推送和拉取

# 推送到远程仓库gitpush origin maingitpush origin branch-namegitpush-uorigin main# 设置上游并推送# 推送所有分支gitpush origin--all# 推送标签gitpush origin--tagsgitpush origin tag-name# 强制推送(危险操作)gitpush--forcegitpush --force-with-lease# 更安全的强制推送# 从远程仓库拉取gitpull origin maingitpull# 从跟踪的远程分支拉取# 拉取但不合并gitfetch origingitfetch--all# 拉取并变基gitpull--rebaseorigin main

跟踪远程分支

# 查看跟踪关系gitbranch-vv# 设置跟踪关系gitbranch --set-upstream-to=origin/main maingitbranch-uorigin/main main# 推送并设置跟踪gitpush-uorigin feature-branch# 删除远程跟踪分支gitbranch-drorigin/branch-name

标签管理

创建标签

# 轻量标签gittag v1.0gittag v1.0 commit-hash# 附注标签gittag-av1.0-m"Version 1.0"gittag-av1.0 commit-hash-m"Version 1.0"# 签名标签gittag-sv1.0-m"Signed version 1.0"

标签操作

# 查看标签gittaggittag-l"v1.*"# 模式匹配# 查看标签信息gitshow v1.0# 删除标签gittag-dv1.0# 删除远程标签gitpush origin--deletetag v1.0gitpush origin :refs/tags/v1.0# 推送标签gitpush origin v1.0gitpush origin--tags# 检出标签gitcheckout v1.0gitcheckout-bversion1 v1.0# 基于标签创建分支

总结

Git 是一个功能强大的版本控制系统,掌握其基础用法对于现代软件开发至关重要。本指南仅涵盖基础操作部分,而Git 的学习是一个持续的过程,随着经验的积累,你会发现更多高效的使用方法和高级用法。

http://www.jsqmd.com/news/200366/

相关文章:

  • 游戏NPC智能化:GLM-4.6V-Flash-WEB理解玩家截图反馈
  • 2026继续教育降AI率工具TOP9测评
  • 面向微服务分布式链路追踪与性能监控的互联网系统可观测性优化与多语言工程实践分享
  • 【编程】 C语言的前身-B语言介绍
  • 图文理解新标杆:GLM-4.6V-Flash-WEB在电商领域的应用前景
  • 盲人视觉辅助设备搭载GLM-4.6V-Flash-WEB实时语音描述环境
  • AR/VR内容生成引擎加入GLM-4.6V-Flash-WEB增强现实交互体验
  • 从能跑到可靠:Agent Engineering如何重塑AI智能体赛道
  • 多语言异步任务调度与性能优化实践:Python、Java、Go、C++实战解析
  • 淘宝商品详情 API 接入全流程实战指南(附完整代码示例)
  • 收藏!LangChain 1.1.0深度解析:从面向对象到函数式组合,夺回LLM应用控制权
  • 农田病虫害预警:GLM-4.6V-Flash-WEB识别叶片损伤模式
  • 深度测评10个AI论文软件,助本科生轻松搞定毕业论文!
  • 面向微服务分布式限流与熔断保护的互联网系统高可用设计与多语言工程实践分享
  • 跨境电商平台借助GLM-4.6V-Flash-WEB统一商品图像描述标准
  • 可穿戴设备健康监测:GLM-4.6V-Flash-WEB分析皮肤图像变化
  • 压力扫描阀故障频发?Gensors手把手教你快速定位与修复!
  • 服装搭配建议:GLM-4.6V-Flash-WEB分析用户衣橱图像
  • 脱发与毛囊的周期规律,看十肽-18 Decapeptide-18能不能帮上忙
  • 从零开始部署GLM-4.6V-Flash-WEB:Docker镜像快速上手教程
  • 共享出行调度:GLM-4.6V-Flash-WEB预测需求高峰区域
  • 2026漆面保护膜品牌公司排名及行业发展分析 - 品牌排行榜
  • 医疗影像初步筛查:GLM-4.6V-Flash-WEB跨模态推理尝试
  • 建筑工地安全管理:GLM-4.6V-Flash-WEB检测未佩戴安全帽行为
  • 健身课程直播互动:GLM-4.6V-Flash-WEB纠正学员动作偏差
  • 书籍推荐:从企业运作到战略决策,一套完整的管理阅读路径
  • chromedriver下载地址汇总:自动化测试GLM-4.6V-Flash-WEB网页界面
  • 用“数据炼金术”开启智能时代:一文掌握大数据分析核心框架与实践心法
  • 2026年做合同管理软件的公司推荐 - 品牌排行榜
  • 汽车外观改装:GLM-4.6V-Flash-WEB预览轮毂与贴膜效果