git 和 github 的使用说明

Git 和 GitHub 使用说明

1. 什么是 Git 和 GitHub?

Git

Git 是一个分布式版本控制系统,用于跟踪文件的变化,便于多人协作开发和代码管理。它可以记录文件的每一次修改,方便回滚到之前的版本,比较不同版本之间的差异等。

GitHub

GitHub 是一个基于 Git 的代码托管平台,提供了远程仓库存储、代码审查、问题跟踪、协作功能等。它允许开发者将本地 Git 仓库推送到远程服务器,实现代码的备份和共享。

2. Git 安装

Windows

  1. 访问 Git 官网
  2. 下载适合 Windows 的安装程序
  3. 运行安装程序,按照默认选项安装即可
  4. 安装完成后,打开命令提示符或 Git Bash,输入 git --version 验证安装

macOS

  1. 使用 Homebrew 安装:brew install git
  2. 或从 Git 官网 下载安装包
  3. 安装完成后,打开终端,输入 git --version 验证安装

Linux

  1. Ubuntu/Debian:sudo apt-get install git
  2. CentOS/Fedora:sudo yum install gitsudo dnf install git
  3. 安装完成后,输入 git --version 验证安装

3. Git 基本配置

配置用户信息

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

配置默认编辑器

1
2
3
4
5
# 使用 Vim 作为默认编辑器
git config --global core.editor vim

# 使用 VS Code 作为默认编辑器
git config --global core.editor "code --wait"

查看配置信息

1
git config --list

4. Git 基本操作

初始化仓库

1
2
3
4
5
# 在当前目录初始化 Git 仓库
git init

# 在指定目录初始化 Git 仓库
git init /path/to/directory

克隆远程仓库

1
git clone https://github.com/username/repository.git

查看仓库状态

1
git status

添加文件到暂存区

1
2
3
4
5
6
7
8
# 添加单个文件
git add filename

# 添加所有文件
git add .

# 添加指定目录
git add directory/

提交文件到本地仓库

1
2
3
4
git commit -m "提交信息"

# 提交所有已跟踪的文件(跳过暂存区)
git commit -a -m "提交信息"

查看提交历史

1
2
3
4
5
6
7
8
# 查看完整提交历史
git log

# 查看简洁提交历史
git log --oneline

# 查看最近 n 次提交
git log -n

查看文件修改内容

1
2
3
4
5
# 查看未暂存的修改
git diff

# 查看已暂存的修改
git diff --staged

撤销修改

1
2
3
4
5
6
7
8
9
10
11
# 撤销工作区的修改
git checkout -- filename

# 撤销暂存区的修改(放回工作区)
git reset HEAD filename

# 撤销最后一次提交(保留修改)
git reset --soft HEAD^1

# 撤销最后一次提交(不保留修改)
git reset --hard HEAD^1

5. GitHub 注册和设置

注册 GitHub 账号

  1. 访问 GitHub 官网
  2. 点击 “Sign up” 按钮
  3. 填写用户名、邮箱和密码
  4. 完成验证和注册流程

设置 SSH 密钥

为了避免每次推送都输入密码,可以设置 SSH 密钥:

1
2
3
4
5
# 生成 SSH 密钥对
ssh-keygen -t ed25519 -C "your.email@example.com"

# 查看公钥内容
cat ~/.ssh/id_ed25519.pub

将公钥内容复制到 GitHub 的 SSH and GPG keys 设置页面(Settings > SSH and GPG keys > New SSH key)。

6. 远程仓库操作

添加远程仓库

1
git remote add origin https://github.com/username/repository.git

查看远程仓库

1
git remote -v

推送本地仓库到远程

1
2
3
4
5
# 推送并设置默认上游分支
git push -u origin main

# 推送指定分支
git push origin branchname

从远程仓库拉取

1
2
3
4
5
# 拉取并合并到当前分支
git pull origin main

# 仅拉取不合并
git fetch origin

克隆远程仓库

1
git clone https://github.com/username/repository.git

7. 分支管理

查看分支

1
2
3
4
5
# 查看本地分支
git branch

# 查看所有分支(包括远程)
git branch -a

创建分支

1
git branch branchname

切换分支

1
2
3
4
git checkout branchname

# 或使用新命令
git switch branchname

创建并切换分支

1
2
3
4
git checkout -b branchname

# 或使用新命令
git switch -c branchname

合并分支

1
2
3
4
5
# 切换到目标分支
git checkout main

# 合并指定分支到当前分支
git merge branchname

删除分支

1
2
3
4
5
6
7
8
# 删除本地分支
git branch -d branchname

# 强制删除本地分支
git branch -D branchname

# 删除远程分支
git push origin --delete branchname

8. 协作工作流

Fork 工作流

  1. Fork 别人的仓库到自己的 GitHub
  2. 克隆 Fork 后的仓库到本地
  3. 创建新分支进行修改
  4. 提交修改并推送到自己的远程仓库
  5. 发起 Pull Request 到原仓库

Pull Request 流程

  1. 在 GitHub 上打开仓库页面
  2. 点击 “New Pull Request” 按钮
  3. 选择源分支和目标分支
  4. 填写 Pull Request 描述
  5. 点击 “Create Pull Request” 按钮

9. 常见问题和解决方案

冲突解决

当合并分支或拉取代码时可能会遇到冲突:

  1. 使用 git status 查看冲突文件
  2. 打开冲突文件,手动解决冲突(冲突部分会有特殊标记)
  3. 解决冲突后,使用 git add 添加文件
  4. 使用 git commit 提交解决冲突

忘记提交信息

如果提交时忘记写提交信息,可以使用:

1
git commit --amend

推送被拒绝

如果推送被拒绝,可能是因为远程仓库有本地没有的提交:

1
2
git pull origin main
git push origin main

10. GitHub Actions 简介

什么是 GitHub Actions

GitHub Actions 是 GitHub 提供的持续集成/持续部署 (CI/CD) 平台,允许你自动化软件开发工作流程。你可以创建工作流来构建、测试、打包、发布或部署项目。

工作流文件结构

GitHub Actions 工作流由 YAML 文件定义,存储在仓库的 .github/workflows/ 目录中。

基本工作流文件结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
name: 工作流名称

on: # 触发条件
push: # 当推送代码时触发
branches: [main] # 仅当推送到 main 分支时触发
pull_request: # 当创建或更新 Pull Request 时触发
branches: [main]

jobs: # 工作流包含的任务
build: # 任务名称
runs-on: ubuntu-latest # 在 Ubuntu 环境中运行
steps: # 任务包含的步骤
- uses: actions/checkout@v3 # 检出代码
- name: 设置 Node.js # 步骤名称
uses: actions/setup-node@v3 # 使用 GitHub 提供的 Node.js 设置动作
with: # 动作参数
node-version: "16"
- name: 安装依赖
run: npm install # 运行命令
- name: 构建项目
run: npm run build

常用触发条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
on:
# 定时触发(Cron 表达式)
schedule:
- cron: "0 0 * * *" # 每天 UTC 时间 0 点触发

# 手动触发
workflow_dispatch:
inputs:
environment:
description: "环境"
required: true
default: "staging"

# 发布事件触发
release:
types: [published]

# 问题事件触发
issues:
types: [opened, reopened]

常用示例

1. Node.js 项目 CI/CD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
name: Node.js CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v3
- name: 使用 Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: npm run build --if-present
- run: npm test

2. 自动部署到 GitHub Pages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
name: 部署到 GitHub Pages

on:
push:
branches: [main]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 设置 Node.js
uses: actions/setup-node@v3
with:
node-version: "16"
- run: npm install
- run: npm run build
- name: 部署到 GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist

常用 Actions

11. 常用 Git 命令速查表

命令 功能
git init 初始化 Git 仓库
git clone 克隆远程仓库
git status 查看仓库状态
git add 添加文件到暂存区
git commit 提交文件到本地仓库
git push 推送本地仓库到远程
git pull 从远程仓库拉取
git branch 管理分支
git checkout 切换分支
git merge 合并分支
git log 查看提交历史
git diff 查看文件修改内容
git remote 管理远程仓库

常见问题

Git文件名大小写更改无法提交的解决方案

在开发过程中,你是否遇到过这样的情况:明明修改了文件名的大小写,Git却提示”working tree clean”,没有任何变更可以提交?这个问题在macOS和Windows系统上尤为常见,

1
2
3
4
5
6
# 方法1:使用git mv命令
$ git mv UserService.js userService.js
$ git status
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: UserService.js -> userService.js

希望这份 Git 和 GitHub 使用说明对你有所帮助!如果有任何问题,请随时查阅 Git 官方文档GitHub 帮助中心