Git 入门:从零开始的版本控制之旅

1. 什么是 Git?为什么你需要它?

想象一下,你正在写一篇超长的论文,改来改去,最后电脑里存了十几个文件:

  • 论文_最终版.docx
  • 论文_最终版2.docx
  • 论文_最终版3_真的不改了.docx
  • 论文_最终版3_打死也不改了.docx

是不是很眼熟?Git 就是来解决这个问题的------它是一个版本控制工具,帮你记录每一次修改,随时可以回到任意历史版本,还能多人协作不打架。

简单来说,Git 就像你写代码时的时光机后悔药

2. 安装 Git

Windows

git-scm.com 下载安装包,一路「下一步」即可。安装完成后,在开始菜单找到 Git Bash,打开它。

macOS

如果你装了 Homebrew,一行命令搞定:

bash 复制代码
brew install git

Linux(Ubuntu/Debian)

bash 复制代码
sudo apt update
sudo apt install git

安装完成后,验证一下:

bash 复制代码
git --version

看到版本号输出,就说明安装成功了!

3. 第一次使用前的配置

Git 需要知道「你是谁」,这样每次提交时才能记录作者信息。打开终端,执行:

bash 复制代码
git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"

小提示:--global 表示全局配置,对所有仓库生效。如果只想对当前仓库配置,去掉 --global 即可。

4. 创建你的第一个仓库

仓库(Repository)就是 Git 管理项目的地方。有两种方式:

方式一:从零开始

bash 复制代码
# 创建一个项目文件夹并进入
mkdir my-project
cd my-project

# 初始化 Git 仓库
git init

方式二:克隆别人的项目

bash 复制代码
git clone https://github.com/用户名/仓库名.git

5. 核心三连:add、commit、push

这是 Git 最常用的三个命令,记住它们,你就掌握了 Git 的 80%。

第一步:git add ------ 把文件放进「暂存区」

bash 复制代码
# 添加单个文件
git add index.html

# 添加所有文件
git add .

第二步:git commit ------ 把暂存区的内容「拍照存档」

bash 复制代码
git commit -m "添加了首页"

-m 后面是提交说明,一定要写清楚这次改了什么,方便以后回顾。

第三步:git push ------ 把本地提交推送到远程仓库

bash 复制代码
git push origin main

小提示:origin 是远程仓库的默认名字,main 是默认分支名(旧版本叫 master)。

6. 查看状态和历史的常用命令

bash 复制代码
# 查看当前仓库状态(哪些文件改了、哪些还没提交)
git status

# 查看提交历史
git log

# 查看某个文件的改动内容
git diff

7. 时光倒流:回退版本

改坏了?别慌,Git 给你后悔药。

bash 复制代码
# 查看历史提交记录,找到你想回去的那个版本号
git log

# 回退到指定版本(版本号是 commit 的哈希值)
git reset --hard 版本号

⚠️ 警告:--hard 会丢弃当前所有未提交的修改,使用前请三思!

8. 分支:平行宇宙的魔法

分支让你可以在不影响主线的前提下,尝试新想法。就像游戏里的存档点,你可以开一条新路去探索。

bash 复制代码
# 创建并切换到新分支
git checkout -b feature-login

# 查看当前分支
git branch

# 切换回主分支
git checkout main

# 合并分支(把 feature-login 的改动合并到当前分支)
git merge feature-login

9. 一张图看懂 Git 工作流程

#mermaid-svg-FLw3trVkgt4Cg5Ll{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-FLw3trVkgt4Cg5Ll .error-icon{fill:#552222;}#mermaid-svg-FLw3trVkgt4Cg5Ll .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-FLw3trVkgt4Cg5Ll .marker{fill:#333333;stroke:#333333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .marker.cross{stroke:#333333;}#mermaid-svg-FLw3trVkgt4Cg5Ll svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-FLw3trVkgt4Cg5Ll p{margin:0;}#mermaid-svg-FLw3trVkgt4Cg5Ll .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .cluster-label text{fill:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .cluster-label span{color:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .cluster-label span p{background-color:transparent;}#mermaid-svg-FLw3trVkgt4Cg5Ll .label text,#mermaid-svg-FLw3trVkgt4Cg5Ll span{fill:#333;color:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .node rect,#mermaid-svg-FLw3trVkgt4Cg5Ll .node circle,#mermaid-svg-FLw3trVkgt4Cg5Ll .node ellipse,#mermaid-svg-FLw3trVkgt4Cg5Ll .node polygon,#mermaid-svg-FLw3trVkgt4Cg5Ll .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .rough-node .label text,#mermaid-svg-FLw3trVkgt4Cg5Ll .node .label text,#mermaid-svg-FLw3trVkgt4Cg5Ll .image-shape .label,#mermaid-svg-FLw3trVkgt4Cg5Ll .icon-shape .label{text-anchor:middle;}#mermaid-svg-FLw3trVkgt4Cg5Ll .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .rough-node .label,#mermaid-svg-FLw3trVkgt4Cg5Ll .node .label,#mermaid-svg-FLw3trVkgt4Cg5Ll .image-shape .label,#mermaid-svg-FLw3trVkgt4Cg5Ll .icon-shape .label{text-align:center;}#mermaid-svg-FLw3trVkgt4Cg5Ll .node.clickable{cursor:pointer;}#mermaid-svg-FLw3trVkgt4Cg5Ll .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .arrowheadPath{fill:#333333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-FLw3trVkgt4Cg5Ll .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-FLw3trVkgt4Cg5Ll .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-FLw3trVkgt4Cg5Ll .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-FLw3trVkgt4Cg5Ll .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .cluster text{fill:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll .cluster span{color:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-FLw3trVkgt4Cg5Ll .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-FLw3trVkgt4Cg5Ll rect.text{fill:none;stroke-width:0;}#mermaid-svg-FLw3trVkgt4Cg5Ll .icon-shape,#mermaid-svg-FLw3trVkgt4Cg5Ll .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-FLw3trVkgt4Cg5Ll .icon-shape p,#mermaid-svg-FLw3trVkgt4Cg5Ll .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-FLw3trVkgt4Cg5Ll .icon-shape .label rect,#mermaid-svg-FLw3trVkgt4Cg5Ll .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-FLw3trVkgt4Cg5Ll .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-FLw3trVkgt4Cg5Ll .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-FLw3trVkgt4Cg5Ll :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} git add
git commit
git push
git pull
工作区(Working Directory)
暂存区(Staging Area)
本地仓库(Local Repo)
远程仓库(Remote Repo)

10. 常用命令速查表

命令 作用
git init 初始化仓库
git clone <地址> 克隆远程仓库
git status 查看状态
git add . 添加所有改动到暂存区
git commit -m "说明" 提交改动
git push 推送到远程
git pull 拉取远程更新
git log 查看提交历史
git branch 查看/管理分支
git checkout -b <分支名> 创建并切换分支
git merge <分支名> 合并分支

11. 总结

Git 的核心思想其实很简单:

  1. add:把改动装进篮子
  2. commit:给篮子拍个照存档
  3. push:把照片上传到云端

记住这三个动作,配合分支和回退功能,你就能轻松驾驭版本控制了。剩下的命令,用到的时候再查也不迟------Git 不是背出来的,是用出来的

现在,打开终端,创建你的第一个仓库吧!🚀

相关推荐
AI大模型-小华1 小时前
Codex CLI第一次怎么用?从安装到读取本地项目完整教程
git·node.js·ai编程·开发工具·代码分析·codex·codex cli
2401_861678621 小时前
Git 在 Windows 使用
windows·git·elasticsearch
青禾8375 小时前
Git 与 SVN 完全指南:从集中式到分布式的版本控制
分布式·git·svn
2301_800954995 小时前
Git 版本控制核心概念详解:从集中式到分布式
分布式·git
Draw Stars14 小时前
Git BASH安装教程
开发语言·git·bash
Akiyama_Mio-Kon14 小时前
2026 最新:Dify 本地 Docker 部署完整教程(Windows + Git Bash)
windows·git·docker
福如意如我心意19 小时前
Git Worktree开发指南
git
Patrick_Wilson1 天前
为什么gitlab的MR会默认有一个merge commit
前端·git·gitlab
是乐乐啊呀1 天前
版本控制 GIT 和 SVN
git·svn