
簡介: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 # 再推送成功