网站首页 > 博客文章 正文
1. 学习 Git 基本命令
Git 是一个强大的分布式版本控制系统,用于跟踪代码更改和团队协作。以下是课程要求的四个基本命令的说明和使用方法:
(1) git clone
- 作用:从远程仓库(如 GitHub)克隆一个仓库到本地。
- 用法:bash
- git clone <仓库URL>
- 示例:bash这会将远程仓库克隆到本地,生成一个名为 repository 的文件夹。
- git clone <https://github.com/username/repository.git>
(2) git commit
- 作用:将本地的代码更改保存到本地仓库,生成一个版本快照。
- 用法:
- 使用 git add 将更改添加到暂存区:bash
- git add <文件名> # 添加特定文件 git add . # 添加所有更改
- 提交更改:bash
- git commit -m "提交信息"
- 示例:bash
- git add index.html git commit -m "Add initial HTML structure"
(3) git push
- 作用:将本地的提交(commits)上传到远程仓库。
- 用法:bash
- git push origin <分支名>
- 示例:bash这会将本地 main 分支的更改推送到远程仓库的 main 分支。
- git push origin main
(4) git pull
- 作用:从远程仓库拉取最新代码并合并到本地仓库。
- 用法:bash
- git pull origin <分支名>
- 示例:bash这会拉取远程 main 分支的最新更改并合并到本地。
- git pull origin main
2. 学习 Git 合并与 Rebase 操作
合并(merge)和变基(rebase)是 Git 中用于整合分支更改的两种常见方法。以下是它们的详细教程:
(1) git merge
- 作用:将一个分支的更改合并到当前分支,生成一个合并提交(merge commit)。
- 用法:
- 切换到目标分支(通常是 main):bash
- git checkout main
- 合并指定分支:bash
- git merge <分支名>
- 示例: 假设有一个特性分支 feature:bash
- git checkout main git merge feature
- 如果没有冲突,Git 会自动合并。
- 如果有冲突,Git 会提示冲突文件,手动编辑后运行:bash
- git add <冲突文件> git commit
- 特点:
- 保留所有分支的提交历史。
- 可能生成额外的合并提交,导致历史稍显复杂。
(2) git rebase
- 作用:将当前分支的提交“重新应用”到目标分支的最新提交上,生成一个线性的提交历史。
- 用法:
- 切换到需要变基的分支:bash
- git checkout feature
- 变基到目标分支:bash
- git rebase <目标分支>
- 如果有冲突,Git 会暂停并提示,手动解决后继续:bash
- git add <冲突文件> git rebase --continue
- 更新远程分支(强制推送):bash
- git push --force
- 示例: 将 feature 分支变基到 main:bash解决冲突(如果有),然后:bash
- git checkout feature git rebase main git push --force
- 特点:
- 产生更干净的线性历史,没有合并提交。
- 适合本地分支或个人开发,但需谨慎用于多人协作的分支(因 --force 可能覆盖他人提交)。
(3) merge vs rebase
特性 | merge | rebase |
历史记录 | 保留分支历史,生成合并提交 | 线性历史,无合并提交 |
复杂性 | 简单,适合团队协作 | 稍复杂,适合个人或小型团队 |
冲突处理 | 一次性解决 | 可能多次解决(逐个提交) |
远程分支 | 安全 | 需谨慎使用 --force |
3. 练习:在 GitHub 上创建一个仓库并提交项目代码
以下是完整的实践步骤,包括基本操作和合并/变基的模拟。
步骤 1:安装和配置 Git
- 安装 Git:
- 下载并安装 Git:Git 官网。
- 验证安装:bash
- git --version
- 配置 Git: 设置用户名和邮箱:bash
- git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
步骤 2:在 GitHub 上创建一个仓库
- 登录 GitHub(如无账号,需注册)。
- 点击右上角 + 图标,选择 New repository。
- 填写仓库信息: Repository name:例如 my-first-repo。 Description(可选):如“Learning Git and GitHub”。 Public/Private:选择公开或私有。 Initialize this repository with a README:勾选。 Add .gitignore(可选):选择适合的模板(如 Python)。 Add a license(可选):选择 MIT。
- 点击 Create repository。
- 复制仓库的 HTTPS 链接(例如:https://github.com/username/my-first-repo.git)。
步骤 3:克隆仓库并提交代码
- 克隆仓库:bash
- git clone <https://github.com/username/my-first-repo.git> cd my-first-repo
- 创建文件: 创建一个简单文件(如 index.html):bash
- echo "<h1>Hello, Git!</h1>" > index.html
- 提交更改:bash
- git add index.html git commit -m "Add initial HTML structure" git push origin main
步骤 4:模拟分支操作与合并
- 创建特性分支:bash
- git checkout -b feature
- 修改文件: 编辑 index.html,添加内容:bash
- echo "<p>Feature content</p>" >> index.html
- 提交更改:bash
- git add index.html git commit -m "Add feature content" git push origin feature
- 合并到主分支(使用 merge):bash
- git checkout main git merge feature git push origin main
- 如果有冲突,手动编辑冲突文件,然后:bash
- git add index.html git commit git push origin main
步骤 5:模拟 Rebase 操作
- 创建另一个特性分支:bash
- git checkout -b feature-rebase
- 修改文件: 编辑 index.html,添加新内容:bash
- echo "<p>Rebase content</p>" >> index.html
- 提交更改:bash
- git add index.html git commit -m "Add rebase content"
- 变基到 main:bash
- git rebase main
- 如果有冲突,Git 会暂停,编辑冲突文件后:bash
- git add index.html git rebase --continue
- 推送更改:bash
- git push --force
- 将变基后的分支合并到 main:bash
- git checkout main git merge feature-rebase git push origin main
步骤 6:拉取远程更改
- 模拟协作,假设远程仓库有新更改:bash
- git pull origin main
- 如果有冲突,解决后提交。
4. 验证完成
- 访问 GitHub 仓库,确认:
- index.html 包含所有更改(初始内容、feature 内容、rebase 内容)。
- 提交历史显示合并提交(merge)和线性历史(rebase)。
- 运行以下命令检查本地状态:bash应显示 Your branch is up to date with 'origin/main'.
- git status
- 查看提交历史:bash
- git log --oneline --graph
5. 常见问题与解决方案
- 推送失败(401/403 错误):
- 确保配置了 GitHub 个人访问令牌(PAT)或 SSH 密钥。
- 生成 PAT:GitHub → Settings → Developer settings → Personal access tokens → Generate new token。
- 分支不匹配:
- 如果远程分支是 main,本地是 master,重命名分支:bash
- git branch -m master main
- 冲突处理:
- 合并或变基时,Git 会标记冲突文件。编辑后运行:bash
- git add <文件> git commit # 对于 merge git rebase --continue # 对于 rebase
- 强制推送风险:
- 使用 git push --force 时,确保分支无人协作,否则可能覆盖他人提交。
- 撤销 rebase:
- 如果 rebase 出错,恢复到变基前的状态:bash
- git rebase --abort
6. 总结
通过本教程,你已完成:
- 基本命令:掌握 git clone、commit、push、pull 的使用。
- 高级操作:学会使用 git merge 和 git rebase 整合分支。
- 实践:在 GitHub 上创建仓库,提交代码,模拟分支协作。
- 验证:确认代码和提交历史正确同步到远程仓库。
如果需要进一步指导、代码调试或更复杂的 Git 工作流(如多人协作、Pull Request),请提供具体需求,我会为你定制解答!
猜你喜欢
- 2025-05-10 Git 主仓库下开发子模块的流程和注意事项
- 2025-05-10 32位单片机定时器入门介绍(52单片机定时器三种工作模式)
- 2025-05-10 Git分布式版本控制器常用命令和使用
- 2025-05-10 务必收藏的常用Git命令大全,彻底搞定Git!
- 2025-05-10 Git 从入门到放不下(github)
- 2025-05-10 轻松部署Gemma3-27B,L20服务器+最新版vLLM高效推理
- 2025-05-10 如何在github建一个自己的博客和git本地常规操作?
- 2025-05-10 抖音品质建设 - iOS启动优化《原理篇》
- 2025-05-10 掌握这 20 个 Git 命令,成为团队协作高手!
- 2025-05-10 Git从入门到精通:拯救你的代码仓库的终极指南
你 发表评论:
欢迎- 367℃用AI Agent治理微服务的复杂性问题|QCon
- 358℃初次使用IntelliJ IDEA新建Maven项目
- 356℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 351℃Maven技术方案最全手册(mavena)
- 348℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 346℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 345℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 342℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)