
要点 | 描述 |
|---|---|
痛点 | 不知道如何使用Git平台?不熟悉Git的全面操作?不会高效协作开发? |
方案 | 本教程详细讲解GitHub、GitCode、Gitee三大平台的使用方法,以及Git支持的所有核心操作 |
驱动 | 掌握Git平台和全部Git操作是2025年程序员必备技能,提升团队协作效率和代码管理能力! |
在当今的软件开发领域,Git已经成为了版本控制的标准工具。而GitHub、GitCode和Gitee作为主流的Git托管平台,更是开发者日常工作中不可或缺的一部分。无论你是个人开发者还是团队协作,全面掌握Git的所有操作以及这些平台的使用方法都能极大地提高你的开发效率和代码管理能力。在本教程中,我们将详细学习Git支持的所有核心操作以及这三大平台的使用方法。
章节 | 内容 |
|---|---|
1 | Git基础知识与环境配置 |
2 | Git仓库操作(创建、克隆、初始化) |
3 | Git工作区操作(查看状态、添加文件、提交更改) |
4 | Git分支管理(创建、切换、推送、拉取、合并、删除) |
5 | Git远程仓库操作(添加、查看、同步、推送、拉取) |
6 | Git历史记录操作(查看日志、回滚、重置) |
7 | Git高级操作(暂存、标签、子模块、变基) |
8 | GitHub平台详解 |
9 | GitCode平台详解 |
10 | Gitee平台详解 |
11 | Issue管理与Pull Request/Merge Request |
12 | 平台特性对比与选择建议 |
13 | 团队协作最佳实践 |
14 | 自动化工作流配置 |
15 | 常见问题解决与实用技巧 |
Git是一个分布式版本控制系统,它可以帮助你跟踪代码的变化、协作开发、回滚到之前的版本等。与集中式版本控制系统不同,Git是分布式的,这意味着每个开发者都有完整的代码库副本。
在使用Git平台之前,你需要先在本地安装Git。
Windows安装:
git --version检查是否安装成功macOS安装:
brew install gitgit --version检查是否安装成功Linux安装:
sudo apt-get install gitsudo yum install gitgit --version检查是否安装成功安装完成后,你需要进行一些基本配置,设置你的用户名和邮箱:
# 设置你的用户名
git config --global user.name "Your Name"
# 设置你的邮箱
git config --global user.email "your.email@example.com"
# 查看配置信息
git config --list
# 设置默认文本编辑器
git config --global core.editor "code --wait" # 使用VS Code作为编辑器
# 设置差异比较工具
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
# 设置自动换行处理(Windows用户建议设置)
git config --global core.autocrlf trueGit提供了丰富的帮助文档,可以随时查询:
# 获取Git命令的一般帮助
git help
# 获取特定命令的帮助
git help <command>
# 例如:git help commit
git <command> --help
# 例如:git commit --help在本地初始化新仓库:
# 在当前目录初始化Git仓库
git init
# 在指定目录初始化Git仓库
git init <directory>
# 例如:git init my-project从远程仓库克隆:
# 克隆远程仓库
git clone <repository-url>
# 例如:git clone https://github.com/user/repo.git
# 克隆远程仓库到指定目录
git clone <repository-url> <directory>
# 例如:git clone https://github.com/user/repo.git my-local-repo
# 克隆时指定分支
git clone -b <branch-name> <repository-url>
# 例如:git clone -b develop https://github.com/user/repo.git
# 克隆时不下载历史记录(浅克隆)
git clone --depth 1 <repository-url># 查看当前仓库的状态
git status
# 查看仓库的配置信息
git config --list
# 查看仓库的远程连接
git remote -v# 创建基本的.gitignore文件示例(Python项目)
echo "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# Virtual environments\nvenv/\n.env\n.env.local\n.env.development.local\n.env.test.local\n.env.production.local\n\n# IDE directories and files\n.idea/\n.vscode/\n*.swp\n*.swo\n*~\n" > .gitignore# 查看当前工作区的状态
git status
# 以简洁模式查看状态
git status -s
# 或 git status --short# 添加指定文件到暂存区
git add <file>
# 例如:git add index.html
# 添加多个文件到暂存区
git add <file1> <file2>
# 例如:git add index.html style.css
# 添加当前目录下的所有文件(包括子目录)到暂存区
git add .
# 添加当前目录下的所有.py文件到暂存区
git add *.py
# 交互式添加文件
git add -i
# 或 git add --interactive
# 仅添加文件的一部分到暂存区
git add -p <file>
# 或 git add --patch <file># 从暂存区移除指定文件,但保留工作区中的文件
git reset HEAD <file>
# 例如:git reset HEAD index.html
# 从暂存区和工作区同时移除指定文件
git rm <file>
# 例如:git rm index.html
# 从暂存区移除文件,但保留工作区中的文件(更现代的方式)
git restore --staged <file>
# 例如:git restore --staged index.html# 提交暂存区的更改,需要输入提交信息
git commit
# 提交暂存区的更改,并指定提交信息
git commit -m "Commit message"
# 例如:git commit -m "Add index.html"
# 提交时显示diff信息
git commit -v
# 或 git commit --verbose
# 提交所有已跟踪文件的更改(跳过git add步骤)
git commit -a
# 或 git commit --all
# 修改最后一次提交的信息
git commit --amend
# 修改最后一次提交的信息,不打开编辑器
git commit --amend -m "New commit message"
# 将当前更改添加到最后一次提交
git commit --amend --no-edit# 查看工作区与暂存区的差异
git diff
# 查看指定文件在工作区与暂存区的差异
git diff <file>
# 例如:git diff index.html
# 查看暂存区与本地仓库的差异
git diff --staged
# 或 git diff --cached
# 以统计信息显示差异
git diff --stat# 查看所有本地分支
git branch
# 查看所有本地和远程分支
git branch -a
# 或 git branch --all
# 查看分支的详细信息(如最后一次提交)
git branch -v
# 或 git branch --verbose
# 查看已合并到当前分支的分支
git branch --merged
# 查看未合并到当前分支的分支
git branch --no-merged# 创建一个新的分支
git branch <branch-name>
# 例如:git branch feature/login
# 基于指定提交创建新分支
git branch <branch-name> <commit-id>
# 例如:git branch feature/fix-bug abc123
# 基于远程分支创建新分支
git branch <branch-name> origin/<remote-branch-name>
# 例如:git branch develop origin/develop# 切换到指定分支
git checkout <branch-name>
# 例如:git checkout develop
# 创建并切换到新分支
git checkout -b <new-branch-name>
# 例如:git checkout -b feature/dashboard
# 创建并切换到基于指定提交的新分支
git checkout -b <new-branch-name> <commit-id>
# 例如:git checkout -b fix/issue-123 abc123
# 创建并切换到基于远程分支的新分支
git checkout -b <new-branch-name> origin/<remote-branch-name>
# 例如:git checkout -b develop origin/develop
# 使用更现代的命令切换分支
git switch <branch-name>
# 例如:git switch develop
# 创建并切换到新分支(更现代的方式)
git switch -c <new-branch-name>
# 例如:git switch -c feature/profile# 推送本地分支到远程仓库
git push origin <branch-name>
# 例如:git push origin feature/login
# 设置上游分支(使后续的git push和git pull不需要指定分支名)
git push --set-upstream origin <branch-name>
# 或简写为 git push -u origin <branch-name>
# 例如:git push -u origin feature/login
# 强制推送分支到远程仓库(谨慎使用)
git push --force origin <branch-name>
# 或简写为 git push -f origin <branch-name>
# 安全地强制推送(保留远程分支的提交)
git push --force-with-lease origin <branch-name># 从远程仓库拉取最新的更改并合并到当前分支
git pull origin <branch-name>
# 例如:git pull origin develop
# 从远程仓库拉取最新的更改但不合并
git fetch origin
# 拉取远程分支并在本地创建对应的分支
git checkout -b <local-branch-name> origin/<remote-branch-name>
# 例如:git checkout -b feature/new-design origin/feature/new-design
# 使用fetch和merge组合拉取远程分支(更安全的方式)
git fetch origin
# 然后切换到目标分支
# 然后合并远程分支到当前分支
git merge origin/<branch-name># 切换到目标分支(例如,要将feature分支合并到main分支)
git checkout main
# 合并指定分支到当前分支
git merge <branch-name>
# 例如:git merge feature/login
# 合并时创建合并提交(即使可以快进合并)
git merge --no-ff <branch-name>
# 例如:git merge --no-ff feature/login
# 使用压缩合并(将多个提交压缩为一个)
git merge --squash <branch-name>
# 例如:git merge --squash feature/login
# 中止合并过程
git merge --abort
# 解决冲突后,标记冲突已解决
git add <conflicted-file>
# 然后提交解决冲突的结果
git commit -m "Resolve merge conflict"# 删除本地分支(分支必须已合并)
git branch -d <branch-name>
# 例如:git branch -d feature/login
# 强制删除本地分支(即使分支有未合并的更改)
git branch -D <branch-name>
# 例如:git branch -D feature/login
# 删除远程分支
git push origin --delete <branch-name>
# 或简写为 git push origin :<branch-name>
# 例如:git push origin --delete feature/login# 查看所有远程仓库
git remote
# 查看所有远程仓库的详细信息(包括URL)
git remote -v
# 或 git remote --verbose
# 查看指定远程仓库的详细信息
git remote show <remote-name>
# 例如:git remote show origin# 添加远程仓库
git remote add <remote-name> <remote-url>
# 例如:git remote add origin https://github.com/user/repo.git
# 添加多个远程仓库
git remote add <remote-name> <remote-url>
# 例如:git remote add upstream https://github.com/org/repo.git# 修改远程仓库的URL
git remote set-url <remote-name> <new-url>
# 例如:git remote set-url origin git@github.com:user/repo.git
# 修改远程仓库的名称
git remote rename <old-name> <new-name>
# 例如:git remote rename origin upstream# 删除指定的远程仓库
git remote remove <remote-name>
# 或 git remote rm <remote-name>
# 例如:git remote remove upstream# 从所有远程仓库获取最新的更改
git fetch --all
# 从指定远程仓库获取最新的更改
git fetch <remote-name>
# 例如:git fetch origin
# 从指定远程仓库获取特定分支的更改
git fetch <remote-name> <branch-name>
# 例如:git fetch origin develop# 推送到默认远程仓库和分支
git push
# 推送到指定远程仓库和分支
git push <remote-name> <branch-name>
# 例如:git push origin main
# 推送所有分支到远程仓库
git push --all <remote-name>
# 例如:git push --all origin
# 推送所有标签到远程仓库
git push --tags <remote-name>
# 例如:git push --tags origin
# 强制推送(谨慎使用)
git push --force <remote-name> <branch-name>
# 或简写为 git push -f <remote-name> <branch-name># 同步远程仓库的所有分支和标签
git remote update
# 检查本地分支与远程分支的差异
git diff <branch-name> origin/<branch-name>
# 例如:git diff main origin/main# 查看所有提交历史
git log
# 查看简洁的提交历史
git log --oneline
# 查看最近n次提交
git log -n <number>
# 例如:git log -n 5
# 查看分支合并图
git log --graph
# 查看分支合并图(简洁模式)
git log --graph --oneline --decorate --all
# 查看指定文件的提交历史
git log <file>
# 例如:git log index.html
# 查看提交历史并显示每次提交的更改内容
git log -p
# 查看提交历史中的差异统计
git log --stat
# 按作者筛选提交历史
git log --author="Author Name"
# 例如:git log --author="John Doe"
# 按提交信息筛选提交历史
git log --grep="keyword"
# 例如:git log --grep="fix bug"
# 查看指定时间段内的提交历史
git log --since="2 weeks ago"
git log --until="2025-09-01"# 创建一个新的提交,撤销指定提交的更改
git revert <commit-id>
# 例如:git revert abc123
# 交互式回滚多个提交
git revert -i <commit-id1> <commit-id2>
# 例如:git revert -i abc123 def456
# 回滚到指定提交,但保留工作区的更改
git reset --soft <commit-id>
# 例如:git reset --soft abc123
# 回滚到指定提交,重置暂存区,但保留工作区的更改
git reset --mixed <commit-id>
# 或简写为 git reset <commit-id>
# 例如:git reset abc123
# 回滚到指定提交,重置暂存区和工作区(谨慎使用)
git reset --hard <commit-id>
# 例如:git reset --hard abc123
# 回滚到上一个提交
git reset --hard HEAD^ # 一个^表示上一个提交,多个^表示多个
# 或 git reset --hard HEAD~1 # 数字表示回滚的步数# 查看指定提交的详细信息
git show <commit-id>
# 例如:git show abc123
# 查看指定提交中特定文件的更改
git show <commit-id>:<file>
# 例如:git show abc123:index.html
# 查看最新提交的信息
git show HEAD# 查看Git的引用日志(所有HEAD移动记录)
git reflog
# 使用reflog恢复丢失的分支或提交
git checkout -b <branch-name> <commit-id>
# 例如:git checkout -b recover-branch abc123# 暂存当前工作区的更改
git stash
# 暂存当前工作区的更改并添加描述
git stash save "Description"
# 例如:git stash save "Work in progress"
# 查看所有暂存
git stash list
# 查看特定暂存的详细内容
git stash show stash@{n}
# 例如:git stash show stash@{0}
# 查看特定暂存的完整diff
git stash show -p stash@{n}
# 例如:git stash show -p stash@{0}
# 恢复指定的暂存,但保留暂存记录
git stash apply stash@{n}
# 例如:git stash apply stash@{0}
# 恢复最新的暂存
git stash apply
# 恢复指定的暂存,并删除暂存记录
git stash pop stash@{n}
# 例如:git stash pop stash@{0}
# 删除指定的暂存
git stash drop stash@{n}
# 例如:git stash drop stash@{0}
# 删除所有暂存
git stash clear
# 创建一个新分支,并在该分支上应用指定的暂存
git stash branch <branch-name> stash@{n}
# 例如:git stash branch feature/new stash@{0}# 列出所有标签
git tag
# 创建轻量级标签
git tag <tag-name>
# 例如:git tag v1.0
# 创建带注释的标签
git tag -a <tag-name> -m "Tag message"
# 例如:git tag -a v1.0 -m "Version 1.0 release"
# 创建指向特定提交的标签
git tag -a <tag-name> <commit-id> -m "Tag message"
# 例如:git tag -a v1.0 abc123 -m "Version 1.0 release"
# 查看标签的详细信息
git show <tag-name>
# 例如:git show v1.0
# 推送指定标签到远程仓库
git push origin <tag-name>
# 例如:git push origin v1.0
# 推送所有标签到远程仓库
git push origin --tags
# 删除本地标签
git tag -d <tag-name>
# 例如:git tag -d v1.0
# 删除远程标签
git push origin --delete <tag-name>
# 或简写为 git push origin :refs/tags/<tag-name>
# 例如:git push origin --delete v1.0# 在现有仓库中添加子模块
git submodule add <repository-url> <path>
# 例如:git submodule add https://github.com/user/library.git libs/library
# 克隆包含子模块的仓库
git clone <repository-url>
cd <repository>
git submodule update --init --recursive
# 一次性克隆包含子模块的仓库
git clone --recurse-submodules <repository-url>
# 更新子模块到最新版本
git submodule update --remote
# 查看子模块的状态
git submodule status
# 进入子模块目录
cd <submodule-path>
# 在父仓库中提交子模块的更新
git add <submodule-path>
git commit -m "Update submodule"
# 移除子模块
git submodule deinit <submodule-path>
git rm <submodule-path># 变基当前分支到指定分支
git rebase <branch-name>
# 例如:git rebase develop
# 交互式变基(可以编辑、合并、删除提交)
git rebase -i <commit-id>
# 或 git rebase --interactive <commit-id>
# 例如:git rebase -i HEAD~5 # 变基最近5次提交
# 继续变基过程(解决冲突后)
git rebase --continue
# 跳过当前提交
git rebase --skip
# 中止变基过程
git rebase --abort
# 变基时保留合并提交
git rebase --preserve-merges <branch-name>
# 或 git rebase -p <branch-name>
# 将一个分支的特定提交变基到另一个分支
git rebase --onto <target-branch> <start-commit> <end-commit>
# 例如:git rebase --onto develop abc123 def456GitHub是全球最大的Git托管平台,拥有超过8300万开发者和2亿多个仓库。它提供了代码托管、项目管理、团队协作等丰富功能。
在GitHub上打开你要克隆的仓库
点击"Code"按钮,复制仓库的URL
打开命令行工具,导航到你要存放代码的目录
执行以下命令克隆仓库:
git clone https://github.com/your-username/your-repository.git克隆完成后,导航到克隆的仓库目录:
cd your-repository在本地创建或修改代码文件
使用以下命令将更改添加到暂存区:
git add . # 添加所有更改的文件
# 或者 git add filename # 添加特定的文件提交更改:
git commit -m "Commit message" # 提交信息应该简洁明了地描述更改的内容推送到GitHub:
git push origin main # 推送到main分支GitCode是一个专注于开发者服务的代码托管平台,提供了代码托管、项目管理、CI/CD等功能,特别适合国内开发者使用。
在GitCode上打开你要克隆的仓库
点击"克隆"按钮,复制仓库的URL
打开命令行工具,导航到你要存放代码的目录
执行以下命令克隆仓库:
git clone https://gitcode.com/your-username/your-repository.git克隆完成后,导航到克隆的仓库目录:
cd your-repository在本地创建或修改代码文件
使用以下命令将更改添加到暂存区:
git add . # 添加所有更改的文件
# 或者 git add filename # 添加特定的文件提交更改:
git commit -m "Commit message" # 提交信息应该简洁明了地描述更改的内容推送到GitCode:
git push origin main # 推送到main分支Gitee(码云)是国内最大的代码托管平台,由开源中国推出,提供了代码托管、项目管理、代码质量分析等功能。
在Gitee上打开你要克隆的仓库
点击"克隆/下载"按钮,复制仓库的URL
打开命令行工具,导航到你要存放代码的目录
执行以下命令克隆仓库:
git clone https://gitee.com/your-username/your-repository.git克隆完成后,导航到克隆的仓库目录:
cd your-repository在本地创建或修改代码文件
使用以下命令将更改添加到暂存区:
git add . # 添加所有更改的文件
# 或者 git add filename # 添加特定的文件提交更改:
git commit -m "Commit message" # 提交信息应该简洁明了地描述更改的内容推送到Gitee:
git push origin master # 推送到master分支(Gitee默认分支名称是master)GitHub:
GitCode:
Gitee:
Pull Request(GitHub)或Merge Request(GitCode/Gitee)是团队协作中的重要功能,它允许开发者提交自己的代码更改,并请求将这些更改合并到主分支。
创建Pull Request/Merge Request:
代码审查:
合并Pull Request/Merge Request:
特性 | GitHub | GitCode | Gitee |
|---|---|---|---|
用户规模 | 全球最大(8300万+开发者) | 国内较大 | 国内最大 |
仓库数量 | 2亿+ | 数百万 | 数百万 |
访问速度(国内) | 较慢 | 较快 | 很快 |
免费额度 | 私有仓库限制(3人以下) | 私有仓库限制(5人以下) | 私有仓库限制(5人以下) |
CI/CD服务 | GitHub Actions | GitCode CI/CD | Gitee Go |
代码审查 | 支持 | 支持 | 支持 |
项目管理 | 支持 | 支持 | 支持 |
文档服务 | GitHub Pages | GitCode Pages | Gitee Pages |
特色功能 | Copilot、CodeSpaces | 代码片段、在线IDE | 代码质量分析、开源中国社区 |
国际化支持 | 强 | 中 | 中 |
企业级功能 | 丰富 | 适中 | 丰富 |
安全性 | 高 | 高 | 高 |
集成能力 | 强 | 强 | 强 |
type(scope): subject 自动化工作流可以帮助你节省时间,提高效率。下面介绍如何在三大平台上配置基本的自动化工作流。
GitHub Actions是GitHub提供的自动化工作流服务,可以帮助你自动化构建、测试和部署等过程。
基本配置示例:
在仓库的.github/workflows/目录下创建一个名为ci.yml的文件:
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
python -m pytestGitCode CI/CD是GitCode提供的自动化工作流服务,可以帮助你自动化构建、测试和部署等过程。
基本配置示例:
在仓库的根目录下创建一个名为.gitlab-ci.yml的文件(GitCode使用与GitLab相同的CI/CD配置格式):
stages:
- test
run-tests:
stage: test
image: python:3.10
script:
- pip install --upgrade pip
- pip install -r requirements.txt
- python -m pytest
tags:
- gitcode-ciGitee Go是Gitee提供的自动化工作流服务,可以帮助你自动化构建、测试和部署等过程。
基本配置示例:
在Gitee仓库页面,点击"CI/CD" -> “Gitee Go” -> “新建流水线”,然后配置:
version: '1.0'
stages:
- stage:
name: Test
jobs:
- job: Test
steps:
- checkout
- run: |
pip install --upgrade pip
pip install -r requirements.txt
python -m pytest
strategy:
matrix:
python: [ '3.10' ]
container: python:${{ python }}问题1:推送代码时出现权限错误
解决方案:
问题2:合并分支时出现冲突
解决方案:
git add命令标记冲突已解决问题3:忘记提交某些文件
解决方案:
git commit --amend命令修改最近的提交问题4:误删分支
解决方案:
git checkout -b branch-name origin/branch-namegit checkout -b branch-name commit-idgit reflog查找丢失的分支引用问题5:Git工作区变得混乱
解决方案:
git stash暂存当前的更改git reset --hard重置工作区(谨慎使用)git clean -f删除未跟踪的文件git clean -fd删除未跟踪的文件和目录# 查看Git状态
git status
# 查看提交历史
git log
# 简洁的提交历史
git log --oneline
# 查看分支合并图
git log --graph --oneline --decorate --all
# 撤销工作区的更改
git checkout -- filename
# 撤销暂存区的更改
git reset HEAD filename
# 回滚到某个提交
git reset --hard commit-id
# 查看文件的更改历史
git blame filename
# 暂存当前工作区的更改
git stash
# 查看所有暂存
git stash list
# 恢复暂存的更改
git stash apply
# 恢复并删除暂存
git stash pop
# 查找文件中的内容
git grep "search-term"
# 例如:git grep "function"
# 计算代码统计信息
git diff --shortstat HEAD~1
# 显示简短的Git状态
git status -s
# 显示最近的提交
git show
# 显示本地分支与远程分支的关联
git branch -vv
# 列出所有远程分支
git branch -r
# 清理未引用的对象
git gc --prune=now
# 检查Git仓库的健康状况
git fsck要点 | 描述 |
|---|---|
价值 | 全面掌握GitHub、GitCode、Gitee三大平台的使用方法和Git的所有核心操作,提升团队协作效率和代码管理能力 |
行动 | 在实际项目中应用所学知识,不断积累经验,探索更多Git高级功能 |
恭喜你完成了Git平台和Git操作的全面学习!通过本教程,你已经详细了解了Git支持的所有核心操作,包括仓库操作、工作区操作、分支管理、远程仓库操作、历史记录操作和高级操作等。同时,你也学习了GitHub、GitCode和Gitee三大平台的使用方法、Issue管理、Pull Request/Merge Request、团队协作最佳实践和自动化工作流配置等内容。
掌握这些技能对于现代软件开发至关重要,无论是个人开发还是团队协作,都能极大地提高你的工作效率和代码质量。在实际项目中,你可能需要根据项目的具体需求和团队的偏好选择合适的平台,并不断积累使用经验。
记住,实践是掌握这些技能的关键,所以请务必多动手实践,在实际项目中应用所学知识!
来源 | 描述 |
|---|---|
Git官方文档 | 提供权威的Git使用指南 |
GitHub官方文档 | 提供GitHub平台的使用指南 |
GitCode官方文档 | 提供GitCode平台的使用指南 |
Gitee官方文档 | 提供Gitee平台的使用指南 |
Pro Git | Scott Chacon和Ben Straub的Git权威指南 |