Git

5 分钟学会 Git 版本控制基础

TL;DR

What: 跟踪代码变更,与他人协作。

Why: 永不丢失代码,可以撤销错误,方便团队协作。

Quick Start

安装:

macOS:

brew install git

Windows: 下载 Git for Windows

Linux:

sudo apt install git  # Debian/Ubuntu

配置:

git config --global user.name "你的名字"
git config --global user.email "you@example.com"

第一个仓库:

git init
git add .
git commit -m "初始提交"

Cheatsheet

命令说明
git init创建新仓库
git clone URL克隆仓库
git status查看当前状态
git add FILE暂存文件
git add .暂存所有更改
git commit -m "msg"提交更改
git push推送到远程
git pull从远程拉取
git log查看历史
git diff查看差异

Gotchas

提交前忘记添加文件

git add forgotten-file
git commit --amend

提交信息写错了

git commit --amend -m "新的信息"

撤销上次提交(保留更改)

git reset --soft HEAD~1

合并冲突

编辑冲突文件后:

git add .
git commit

Next Steps