Git 使用手册

Posted by 谌中钱 on 2026-03-12

1 简介安装

Git 是一个免费的开源分布式版本控制系统,旨在处理从小型到快速高效的超大型项目。

2 常用命令

 1# 设置用户名和邮箱
 2git config --global user.email "templechan@126.com"
 3git config --global user.name "templechan"
 4
 5# 初始化本地 git 仓库
 6git init
 7# 添加文件到暂存区
 8git add .
 9# 提交更改到本地
10git commit -m "first commit"
11# 修改主分支名字
12git branch -M main
13
14# 添加远程仓库地址
15git remote add origin https://github.com/templechan/blog.git
16# 修改为 SSH
17git remote set-url origin git@github.com:templechan/blog.git
18# 推送代码到远程仓库 origin 的 main 分支,-u 是建立分支联系,远程仓库没有对应分支时使用
19git push -u origin main
20
21# 克隆远程仓库到本地
22git clone https://github.com/templechan/blog.git
23# 克隆远程仓库的特定分支到本地
24git clone -b dev https://github.com/templechan/blog.git
25
26# 获取远程分支的最新信息
27git fetch --all
28# 查看所有分支
29git branch -a
30# 查看本地分支
31git branch
32# 查看远程分支
33git branch -r
34
35# 创建并切换到本地分支
36git checkout -b dev
37# 切换分支
38git checkout main
39# 删除分支
40git branch -D dev
41
42# 合并 指定分支 到 当前分支
43git merge dev
44# 发生冲突后,解决冲突后,将文件提交到暂存区,完成本地合并
45git add .
46git commit -m "commit"
47# 推送到远程仓库(如果需要)
48git push
49# 如果远程仓库没有本地分支,则加上 -u 建立联系
50git push -u origin main

3 生成 SSH 密钥

配置好 SSH,以后 推送 代码不用输密码。

 1# 生成 公钥(id_ed25519.pub) 和 私钥(id_ed25519),一路回车即可
 2ssh-keygen -t ed25519 -C "templechan@126.com"
 3
 4# 找到公钥(id_ed25519.pub)并复制
 5# Windows
 6type %USERPROFILE%\.ssh\id_ed25519.pub
 7# macOS/Linux
 8cat ~/.ssh/id_ed25519.pub
 9
10# 把公钥粘贴到代码平台,GitHub/GitLab/码云 等
11# 如 GitHub → Settings → SSH and GPG keys,点击 New SSH key,标题随便填,粘贴公钥(id_ed25519.pub)全文