0%

Git 指南


理解 Git 基础

工作目录

这是你在本地机器上实际编辑文件的地方。

暂存区(Index)

工作目录与仓库之间的缓冲区。
由你决定哪些内容会进入下一次提交。

仓库

存储所有提交历史的数据库。

典型工作流:

1
Working Directory → Staging Area → Local Repository → Remote Repository

理解了这一流程之后,Git 就会变得简单许多。

常用 Git 命令

初始配置

1
2
3
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global --list

初始化仓库:

1
git init

克隆仓库

1
2
# HTTPS
git clone https://github.com/username/repo.git

SSH(推荐)

1
git clone git@github.com:username/repo.git

跟踪并提交更改

1
2
3
git status
git add .
git commit -m "feat: add login feature"

查看历史

1
git log --oneline

推荐的提交前缀

  • feat:新功能
  • fix:修复 Bug
  • docs:文档
  • refactor:重构

操作远程仓库

1
2
3
4
git remote add origin <repo-url>
git remote -v
git push origin main
git pull origin main

除非你非常清楚自己在做什么,否则避免强制推送:

1
git push --force-with-lease

分支与合并

分支让你可以安全地进行各种尝试。

1
2
3
4
5
git branch
git checkout -b feature/login
git checkout main
git merge feature/login
git branch -d feature/login

推荐的分支命名

  • main – 稳定的生产分支
  • feature/* – 新功能
  • hotfix/* – 紧急修复

撤销更改与恢复

Git 之所以强大,是因为几乎没有什么是真正丢失的。

1
2
3
4
5
git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>
git reflog
git reflog is your emergency exit.

标签与版本发布

1
2
git tag -a v1.0 -m "First release"
git push origin --tags

标签非常适合用来标记稳定的发布版本。

常见开发场景

新项目推送

1
2
3
4
5
git init
git add .
git commit -m "init: initial commit"
git remote add origin <repo>
git push -u origin main

团队功能开发

1
2
3
git checkout -b feature/payment
git commit -m "feat: payment integration"
git push origin feature/payment

热修复生产 Bug

1
2
3
4
git checkout main
git pull
git checkout -b hotfix/critical-bug
git commit -m "fix: critical production issue"

常见陷阱及避免方法

  • 忘记执行 git add
  • 忽略合并冲突
  • 执行 git reset –hard 后惊慌失措
  • 使用密码而不是令牌(Token)/SSH

提示:如果出了问题,先停下来查看 git reflog。

在 GitHub 中使用 SSH(推荐)

1
2
3
ssh-keygen -t ed25519 -C "you@example.com"
cat ~/.ssh/id_ed25519.pub
ssh -T git@github.com

成功时的输出:

1
Hi username! You've successfully authenticated.

Git 最佳实践

小步提交,每次提交应有明确意义

编写清晰的提交信息

积极使用分支

定期清理无用分支

优先使用 SSH 而非 HTTPS

自动化操作时使用细粒度令牌(Fine-grained Token)

总结

你不需要记住所有的 Git 命令。

Git 的核心不在于命令——而在于理解历史与协作。

一旦你理解了工作流,Git 就会成为一个强大的安全网,而不是焦虑的来源。

Author: Alex

Article Link: https://bodysuperman.github.io/zh-CN/2025/11/10/Git-guideline/

License: This article is licensed under CC BY-NC-SA 4.0. Please credit the original author and include the source link when reposting.