
简介:Git 到底是什么先建立正确认知Git 是一个版本管理工具作用就像游戏存档每写完一段代码就存一个档叫commit任何时候都能回到之前的存档能看谁、什么时候、改了什么它管的是文件的历史版本。三个地方最核心的概念务必记住你的代码改动会依次经过三个地方① 工作区(你的文件夹) --git add-- ② 暂存区(待提交清单) --git commit-- ③ 本地仓库(存档历史) --git push-- ④ 远程仓库Gitee工作区你正在编辑的文件暂存区你选定要存档的文件清单本地仓库已经存档的历史远程仓库Gitee用git push把本地存档同步上去。记住 4 个核心命令命令动作通俗比喻git status看状态看看现在什么情况git add暂存把文件放进待存档篮子git commit提交存档git push推送把存档同步到 Gitee第 1 章开始前的准备只做一次1.1 确认 Git 已安装git --version看到版本号如git version 2.50.1就说明装好了。1.2 配置你的身份git config --global user.name Alice git config --global user.email Aliceexample.com【含义】这两行告诉 Git你是谁每次存档都会记上你的名字。--global表示对电脑上所有项目生效只需设置一次。⚠️ 这里的名字只是存档署名随便填都行不是Gitee 登录账号。1.3 拿到 Gitee 仓库地址打开 Gitee 上你建的仓库页面 → 点「克隆/下载」→ 复制 HTTPS 地址形如https://gitee.com/你的用户名/仓库名.githttps://gitee.com/fencecat/todo.git第 2 章把仓库拉到本地git clonecd ~ # 进入你的家目录放代码的地方 # 克隆把 Gitee 上的仓库复制到本地strugglestruggledeMac-mini my-todo % git clone https://gitee.com/fencecat/todo.git Cloning into todo... Username for https://gitee.com: fencecat Password for https://fencecatgitee.com: warning: You appear to have cloned an empty repository. strugglestruggledeMac-mini my-todo % #文件目录下有克隆的仓库目录 strugglestruggledeMac-mini my-todo % ls todohttps://gitee.com/你的用户名/仓库名.git # 进入克隆出来的文件夹cd todo【含义】git clone 地址会做两件事本地创建一个和仓库同名的文件夹把这个文件夹变成一个本地仓库里面有个隐藏的.git文件夹存放所有历史如果仓库是空的会看到提示warning: You appear to have cloned an empty repository.——正常意思是这是空仓库。第 3 章写第一份代码strugglestruggledeMac-mini todo % vim hello.py strugglestruggledeMac-mini todo % git status On branch master No commits yet Untracked files: (use git add file... to include in what will be committed) hello.py nothing added to commit but untracked files present (use git add to track)在文件夹里新建一个文件hello.py内容print(你好世界) 然后运行 git status 会看到类似Untracked files: (use git add file... to include in what will be committed) hello.py【含义】hello.py显示UntrackedGit 还不认识这个新文件没被管理。下一步用git add让它被管理。第 4 章暂存git addgit add hello.py 再运行git statusstrugglestruggledeMac-mini todo % git add hello.py strugglestruggledeMac-mini todo % git status On branch master No commits yet Changes to be committed: (use git rm --cached file... to unstage) new file: hello.py strugglestruggledeMac-mini todo %【含义】hello.py从Untracked变成Changes to be committed意思是已经放进待存档篮子准备存档。第 5 章提交/存档git commitgit commit -m 第一次提交hello 程序strugglestruggledeMac-mini todo % git commit -m 第一次提交hello 程序 [master (root-commit) d333df5] 第一次提交hello 程序 1 file changed, 1 insertion() create mode 100644 hello.py【含义】git commit 存档-m ... 存档说明message写清楚这次改了什么看历史git log --onelinetrugglestruggledeMac-mini todo % git log --oneline d333df5 (HEAD - master) 第一次提交hello 程序 strugglestruggledeMac-mini todo %会看到a1b2c3d 第一次提交hello 程序前面那串a1b2c3d是这次存档的编号哈希git log就是翻存档记录。第 6 章推送到 Giteegit pushgit push -u origin masterstrugglestruggledeMac-mini todo % git push -u origin master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 261 bytes | 261.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag c340d382 To https://gitee.com/fencecat/todo.git * [new branch] master - master branch master set up to track origin/master. strugglestruggledeMac-mini todo %【含义】git push 把本地存档同步到 Giteeorigin 远程仓库别名clone 时自动起的指 Gitee 那个仓库master 分支名主干分支-u 关联起来以后只需敲git push第一次会要登录用户名 Gitee 账号名密码 Gitee 登录密码输密码时屏幕不显示任何字符正常盲打后回车。成功后刷新 Gitee 网页就能看到hello.py和这次提交了第 7 章日常开发循环最重要的习惯以后每次改代码都是这套循环# 1. 改代码用编辑器改文件 # 2. 看改了什么 git status # 看哪些文件变了 git diff # 看具体改了哪几行 # 3. 暂存 git add 文件名 # 或 git add . 表示全部暂存 # 4. 提交 git commit -m 说明这次改了什么 # 5. 推送 git push练习给hello.py加一个函数然后完整走一遍这个循环再刷新 Gitee 看看是不是多了一次提交。strugglestruggledeMac-mini todo % vim hello.py strugglestruggledeMac-mini todo % git status On branch master Your branch is up to date with origin/master. Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: hello.py no changes added to commit (use git add and/or git commit -a) strugglestruggledeMac-mini todo % git diff diff --git a/hello.py b/hello.py index 1a619a3..862876a 100644 --- a/hello.py b/hello.py -1 1 -print(你好世界) print(你好世界!) strugglestruggledeMac-mini todo % git add . strugglestruggledeMac-mini todo % strugglestruggledeMac-mini todo % git commit -m 加了标点符号 [master 08d3aa5] 加了标点符号 1 file changed, 1 insertion(), 1 deletion(-) strugglestruggledeMac-mini todo % git push Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Writing objects: 100% (3/3), 285 bytes | 285.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag 9f45f0a3 To https://gitee.com/fencecat/todo.git d333df5..08d3aa5 master - master第 8 章分支✨进阶分支 给自己一个独立工作台改坏了不影响主干 main。真实开发里永远不要直接在 main 上改。git switch -c feature/新功能 # 新建并切换分支-c create # ...在这个分支上改代码、add、commit... git push -u origin feature/新功能 # 推分支到 Gitee git switch main # 切回主干 git merge feature/新功能 # 把功能合并进主干 git push # 推送合并结果详细的分支、合并、冲突教程见git-basics-todo-scenario.md和git-merge-conflict.md。实操步骤(在我的todo仓库中执行)第一步:创建并切换到新分支# 确保在 todo 仓库目录下 strugglestruggledeMac-mini todo % pwd /tmp/git-basics/my-todo/todo # 查看当前分支应该显示 * main strugglestruggledeMac-mini todo % git branch * master # 创建并切换到新分支 feature/hello git switch -c feature/hellostrugglestruggledeMac-mini todo % git switch -c feature/hello Switched to a new branch feature/hello第二步在新分支上开发修改代码# 创建一个新文件模拟新功能开发 strugglestruggledeMac-mini todo % echo print(Hello from feature branch!) hello.py # 查看状态应该显示新文件未被追踪 strugglestruggledeMac-mini todo % git status On branch feature/hello Changes not staged for commit: (use git add file... to update what will be committed) (use git restore file... to discard changes in working directory) modified: hello.py no changes added to commit (use git add and/or git commit -a) # 添加并提交 strugglestruggledeMac-mini todo % git add hello.py strugglestruggledeMac-mini todo % git commit -m feat: 添加 hello.py 新功能 [feature/hello f46ee98] feat: 添加 hello.py 新功能 1 file changed, 1 insertion(), 1 deletion(-) # 再修改一下README模拟功能迭代 strugglestruggledeMac-mini todo % echo # 这是我在 feature 分支添加的注释 README.md strugglestruggledeMac-mini todo % git add README.md strugglestruggledeMac-mini todo % git commit -m docs: 更新 README 说明 [feature/hello 8197774] docs: 更新 README 说明 1 file changed, 1 insertion() create mode 100644 README.md第三步推送分支到 Gitee# 推送到远程并建立追踪关系 strugglestruggledeMac-mini todo % git push -u origin feature/hello Enumerating objects: 8, done. Counting objects: 100% (8/8), done. Delta compression using up to 10 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (6/6), 614 bytes | 614.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag 154110a6 remote: Create a pull request for feature/hello on Gitee by visiting: remote: https://gitee.com/fencecat/todo/pull/new/fencecat:feature/hello...fencecat:master To https://gitee.com/fencecat/todo.git * [new branch] feature/hello - feature/hello branch feature/hello set up to track origin/feature/hello.第四步切回主干合并功能# 切回 master 分支 strugglestruggledeMac-mini todo % git switch master Switched to branch master Your branch is up to date with origin/master. # 确认你在 master 分支上 strugglestruggledeMac-mini todo % git branch feature/hello * master # 把 feature/hello 分支的修改合并进来 strugglestruggledeMac-mini todo % git merge feature/hello Updating 08d3aa5..8197774 Fast-forward README.md | 1 hello.py | 2 - 2 files changed, 2 insertions(), 1 deletion(-) create mode 100644 README.md strugglestruggledeMac-mini todo % git push Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag d24106ad To https://gitee.com/fencecat/todo.git 08d3aa5..8197774 master - master第五步推送合并结果到 Gitee# 推送本地的 master 分支到远程 strugglestruggledeMac-mini todo % git push Everything up-to-date⚠️ 如果合并时出现冲突怎么办合并冲突是团队协作中的常态。如果合并时提示冲突CONFLICT你可以# 1. 查看冲突文件 git status # 2. 手动编辑冲突文件IDE 会高亮标记冲突区域 # 3. 解决冲突后重新标记为已解决 git add 冲突文件 git commit -m 合并 feature/hello 并解决冲突 # 4. 继续推送 git push 一个实用的分支管理命令# 查看所有分支及其最后一次提交 git branch -v # 删除本地已合并的分支清理用 git branch -d feature/hello # 删除远程已合并的分支 git push origin --delete feature/hello第 9 章撤销救命命令git restore 文件名 # 改错了还没 add丢弃改动 git restore --staged 文件名 # 已经 add 了撤回暂存内容还在 git revert 提交哈希 # 已经 commit 了安全撤销这次提交第 10 章命令速查表想做什么命令拿代码到本地git clone 地址看状态git status看历史git log --oneline看改了什么git diff暂存git add 文件名或git add .提交git commit -m 说明推送git push拉取更新git pull建/切分支git switch -c 分支/git switch 分支合并git merge 分支丢弃改动git restore 文件名撤销提交git revert 哈希第 11 章常见报错与解决报错原因解决Authentication failed账号密码不对用户名用 Gitee 账号名密码用登录密码或私人令牌rejected ... non-fast-forward远程有本地没有的提交先git pull --rebase再git pushUntracked files新文件没被管理正常git add即可no changes added to commit没暂存就想提交先git add再git commit关于rejected ... non-fast-forward场景:non-fast-forward你的历史和远程分叉了远程有同事的新提交你本地也有自己的新提交两边各走各的Git 不敢直接覆盖所以拒绝你的 push。为什么叫 fast-forward快进想象提交历史是一条线。如果远程是v1你本地是v1 → 你的提交那 push 就是顺着线往前走Git 可以直接快进没矛盾。但现在的情况是复制* 你也加了一行 ← 你的本地提交基于 v1 | v1 ──┤ | * 同事加了一行 ← 同事已推到远程也基于 v1两边都从v1各自往前走了一步分叉了。Git 无法快进只能停下来让你处理 → 报non-fast-forward。解决git pull --rebase它的作用是把你的提交搬到远程最新提交的上面把分叉捋成一条直线复制处理前分叉 处理后一条直线可 push * 你 * 你也加了一行 ← 你的提交被搬到同事之上 | * 同事 * 同事加了一行 ← 远程最新 |/ * v1 * v1现在你的历史 远程历史 你的新提交就能快进了git push成功。git pull --rebase origin master # 先拉取远程把自己的提交搬到上面 git push # 再推送成功