高級 Git 指南:Git Stash、Reset、Rebase 等

已發表: 2022-03-11

每個開發者都應該對版本控制有很好的了解,而 Git 已經成為軟件開發中版本控制的事實標準。

不過,通常情況下,開發人員只學習了一些簡單的命令,而忽略了 Git 歷史的強大功能以及 Git 可以做的其他事情來提高您的效率。 例如,使用 Git 使用git tag管理髮布非常容易。

我參加了在線 Git 高級課程(使用 Github),然後與 Github 一起教授初學者的 Git 課程。 當我注意到關於我最喜歡的 Git 功能的技術文章並不多時,我抓住機會與我的開發人員同行分享。 在這篇文章中,您將學習如何利用以下高級 Git 功能:

  • git stash ,它會臨時保存您的代碼
  • git reset ,它可以讓你在提交之前整理你的代碼
  • git bisect ,一個允許你找出錯誤提交的函數
  • git squash ,它允許你合併你的提交
  • git rebase ,它允許將更改從一個分支應用到另一個分支

Git 藏匿處

Git stash 使您無需提交即可保存代碼。 這有什麼用? 想像以下場景:

你已經做了三個整潔的提交,但是你還有一些非常混亂的未提交代碼; 如果不先刪除調試代碼,您將不想提交它。 然後,由於某種原因,你突然需要處理另一項任務,不得不切換分支。 如果您在main分支上,並且忘記為您的功能創建新分支,則通常會發生這種情況。 現在,您的代碼如下所示:

 $ git status On branch my-feature Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: css/common.scss no changes added to commit (use "git add" and/or "git commit -a")
 $ git diff diff --git a/css/common.scss b/css/common.scss index 2090cc4..90fd457 100644 --- a/css/common.scss +++ b/css/common.scss @@ -13,6 +13,6 @@ body { font-family: "Proxima Nova", Arial, sans-serif; font-size: 13px; - color: #333; + color: red; background-color: #f00; }

當您運行git stash時,未提交的代碼會在沒有提交的情況下消失。 存儲就像將臨時本地提交保存到您的分支。 無法將存儲推送到遠程存儲庫,因此存儲僅供您個人使用。

 $ git stash Saved working directory and index state WIP on my-feature: 49ee696 Change text color

您的分支現在顯示為您上次提交時的樣子。 現在,您可以安全地更改分支,而不會丟失代碼或提交混亂。 當您切換回您的分支並運行git stash list時,您將看到如下所示的存儲列表:

 $ git stash list stash@{0}: WIP on my-feature: 49ee696 Change text color

您可以通過運行git stash apply輕鬆地重新應用隱藏的內容。 您還可以通過運行git stash apply stash@{1}應用特定的存儲(如果您已經存儲了多次)(“1”表示最後一個存儲之前的第二個)。 這是一個存儲多個提交並應用不同存儲的示例:

 $ git diff diff --git a/css/common.scss b/css/common.scss index 2090cc4..90fd457 100644 --- a/css/common.scss +++ b/css/common.scss @@ -13,6 +13,6 @@ body { font-family: "Proxima Nova", Arial, sans-serif; font-size: 13px; - color: #333; + color: red; background-color: #f00; } $ git stash Saved working directory and index state WIP on my-feature: 49ee696 Change text color $ git diff diff --git a/css/common.scss b/css/common.scss index 2090cc4..b63c664 100644 --- a/css/common.scss +++ b/css/common.scss @@ -13,6 +13,6 @@ body { font-family: "Proxima Nova", Arial, sans-serif; font-size: 13px; - color: #333; + color: red; background-color: #f00; } $ git stash Saved working directory and index state WIP on my-feature: 49ee696 Change text color
 $ git stash list stash@{0}: WIP on my-feature: 49ee696 Change text color stash@{1}: WIP on my-feature: 49ee696 Change text color stash@{2}: WIP on my-feature: 49ee696 Change text color $ git stash apply stash@{2} On branch my-feature Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: css/common.scss no changes added to commit (use "git add" and/or "git commit -a")

git stash apply stash@{2}應用了最舊的隱藏代碼,當我們將文本的顏色更改為紅色時。

 $ git diff diff --git a/css/common.scss b/css/common.scss index 2090cc4..90fd457 100644 --- a/css/common.scss +++ b/css/common.scss @@ -13,6 +13,6 @@ body { font-family: "Proxima Nova", Arial, sans-serif; font-size: 13px; - color: #333; + color: red; background-color: #f00; }

如果您決定在恢復存儲後不提交您的工作,您可以運行git checkout . ,它會重置所有未提交的代碼。

作為如何使用 Git stash 的另一個示例:假設您有一些新文件,其中一個有錯誤。 除了可疑的錯誤文件之外的所有文件都保持未暫存(必須暫存代碼才能隱藏),然後您可以隱藏該文件並解決問題。 如果存儲的文件不是問題,您可以恢復存儲。

 $ git status On branch my-feature Untracked files: (use "git add <file>..." to include in what will be committed) css/colors.scss nothing added to commit but untracked files present (use "git add" to track)
 $ git add css/colors.scss $ git stash Saved working directory and index state WIP on my-feature: 0d8deef delete colors $ git status On branch my-feature nothing to commit, working tree clean $ git stash apply stash@{0} On branch my-feature Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: css/colors.scss

您還可以使用git stash branch將隱藏的提交轉移到新功能分支或調試分支:

 $ git status On branch my-feature Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: css/common.scss no changes added to commit (use "git add" and/or "git commit -a") $ git stash Saved working directory and index state WIP on my-feature: 66f3f3b Add colors file $ git stash branch debugging-branch M css/common.scss Switched to a new branch 'debugging-branch' Unstaged changes after reset: M css/common.scss On branch debugging-branch Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: css/common.scss Dropped refs/stash@{0} (d140624f60d8deef7bceb0d11fc80ed4fd47e0a1)

請注意,當您應用存儲時,不會刪除存儲。 您可以使用git drop單獨刪除存儲,或使用git stash clear刪除所有存儲:

 $ git stash list stash@{0}: WIP on my-feature: 66f3f3b Add colors file stash@{1}: WIP on my-feature: 0d8deef delete colors stash@{2}: WIP on my-feature: 49ee696 Change text color $ git stash drop stash@{2} Dropped stash@{2} (8ed6d2ce101aa2e28c8ccdc94cb12df8e5c468d6) $ git stash list stash@{0}: WIP on my-feature: 66f3f3b Add colors file stash@{1}: WIP on my-feature: 0d8deef delete colors $ git stash clear $ git stash list $

Git 重置

如果您確實發現自己不小心提交了一些雜亂的代碼,您可以進行“軟”重置。 這意味著代碼看起來好像還沒有提交。 然後,您可以在進行更清晰的提交之前在 IDE 中整理您的代碼。 為此,您可以運行git reset --soft HEAD~1 。 這將重置最近的提交。 您可以通過更改~之後的數字來重置多個提交,例如git reset --soft HEAD~2

 $ git reset --soft HEAD~1 $ git status On branch debugging-branch Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: css/common.scss Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: css/common.scss
 $ git diff diff --git a/css/common.scss b/css/common.scss index 2090cc4..90fd457 100644 --- a/css/common.scss +++ b/css/common.scss @@ -13,6 +13,6 @@ body { font-family: "Proxima Nova", Arial, sans-serif; font-size: 13px; - color: $grey; + color: red; background-color: #f00; }

Git 重置有點混亂,尤其是在教新的 Git 用戶時。 軟重置應該為真正的錯誤保留,而存儲可以用於交換代碼。

您還可以執行硬重置( git reset --hard HEAD~1 )。 這種類型的重置基本上會刪除您的最後一次提交。 你應該非常小心地執行硬重置,特別是如果你推送你的分支,因為沒有辦法恢復你的提交。

Git 平分

我最喜歡的 Git 工具是git bisect 。 我只需要它幾次,但當我這樣做時,它是無價的! 我主要在大型代碼庫上使用它,即使經過一些積極的調試,也沒有人找到根本原因。

git bisect本質上在兩個給定的提交之間執行二進制搜索,然後為您提供特定提交的詳細信息。 你首先需要給 Git 一個好的提交,你知道你的功能在哪里工作,以及一個錯誤的提交。 請注意,只要您有一個好的提交和一個壞的提交,這些提交可以相隔數年(儘管時間越早,它變得越困難!)。

git bisect最有趣的地方在於,當你開始的時候,你通常並不真正知道是誰編寫了 buggy 提交。 發現錯誤被引入的位置的興奮不止一次導致一些同事擠在我的電腦周圍!

要開始,請查看 buggy 分支並找到好的提交。 您需要查看您的提交歷史並找到提交哈希,然後檢查該特定提交並測試您的分支。 一旦你找到了一個好的和壞的工作點,你可以運行git bisect

在這種情況下,我們製作的這個網站上的文本是紅色的(儘管它可以使用 UI 設計器),但我們不知道它是如何或何時變成紅色的。 這是一個非常簡單的示例,在現實生活場景中,您可能會遇到一個不太明顯的問題,例如表單無法提交/運行。

當我們運行git log時,我們可以看到可供選擇的提交列表。

 $ git log commit a3cfe7f935c8ad2a2c371147b4e6dcd1a3479a22 (HEAD -> main) Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:52:57 2021 +0100 Update .gitignore file for .DS_Store commit 246e90977790967f54e878a8553332f48fae6edc Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:51:23 2021 +0100 Change styling of page commit d647ac489ad43b3c6eaea5aceb02b0a7d7e5cf8e Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:50:48 2021 +0100 Change text color commit 032a41136b6653fb9f7d81aef573aed0dac3dfe9 Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:42:57 2021 +0100 Change text color commit 246e90977790967f54e878a8553332f48fae6edc Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:41:23 2021 +0100 delete colors commit d647ac489ad43b3c6eaea5aceb02b0a7d7e5cf8e Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:50:48 2021 +0100 Change text color commit ce861e4c6989a118aade031020fd936bd28d535b Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:07:36 2021 +0100 ...

如果我在最近的提交哈希上打開我的網頁,文本是紅色的,所以我知道我有問題。

Git bisect,第 1 步:一個帶有紅色文本的網頁。

現在我們開始 bisect 並告訴 Git 我們有一個錯誤的提交。

 $ git bisect start $ git bisect bad 8d4615b9a963ef235c2a7eef9103d3b3544f4ee1

現在我們回到過去,嘗試找到文本不是紅色的提交。 在這裡,我嘗試檢查我的第一個提交......

 $ git checkout ce861e4c6989a118aade031020fd936bd28d535b Note: checking out 'ce861e4c6989a118aade031020fd936bd28d535b'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at ce861e4 Add CSS styles

…並刷新網頁…

Git bisect,第 2 步:帶有黑色文本的網頁。

文本不再是紅色的,所以這是一個很好的提交! 提交越新,即越接近錯誤提交越好:

 $ git checkout d647ac489ad43b3c6eaea5aceb02b0a7d7e5cf8e Previous HEAD position was ce861e4c6989a118aade031020fd936bd28d535b Add CSS styles HEAD is now at d647ac4 Change text color

Git 現在會告訴你在找到正確的提交之前它必須搜索多少次提交。 Git 將遍歷的提交數量取決於好提交和壞提交之間的提交數量(時間越長,Git 需要迭代的次數越多)。

現在,您需要再次測試您的分支,看看您的問題是否已經消失。 如果您定期更新模塊,有時這可能會有點麻煩,因為您可能需要在前端存儲庫中重新安裝節點模塊。 如果有數據庫更新,您可能還需要更新這些。

git bisect在你的好提交和壞提交中間自動檢查一個提交。 這裡是估計找到錯誤提交的一步。

 $ git bisect good 1cdbd113cad2f452290731e202d6a22a175af7f5 Bisecting: 1 revision left to test after this (roughly 1 step) [ce861e4c6989a118aade031020fd936bd28d535b] Add CSS styles $ git status HEAD detached at ce861e4 You are currently bisecting, started from branch '8d4615b'. (use "git bisect reset" to get back to the original branch)

刷新頁面,看看你的問題是否消失了。 問題仍然存在,所以我們告訴 Git 這仍然是一個錯誤的提交。 這次不需要引用提交哈希,因為 Git 將使用您簽出的提交。 我們需要重複這個過程,直到 Git 遍歷了所有可能的步驟。

 $ git bisect bad Bisecting: 0 revisions left to test after this (roughly 0 steps) [cbf1b9a1be984a9f61b79ae5f23b19f66d533537] Add second paragraph to page

刷新頁面,我們的問題又消失了,所以這是一個很好的提交:

Git bisect,第 3 步:帶有一些額外黑色文本的同一網頁。

在這個階段,Git 發現了第一個錯誤提交:

 $ git bisect good ce861e4c6989a118aade031020fd936bd28d535b is the first bad commit commit ce861e4c6989a118aade031020fd936bd28d535b Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:52:57 2021 +0100 Add CSS styles :000000 100644 0000000000000000000000000000000000000000 092bfb9bdf74dd8cfd22e812151281ee9aa6f01a M css

現在我們可以使用git show來顯示提交本身並識別問題:

 $ git show ce861e4c6989a118aade031020fd936bd28d535b commit ce861e4c6989a118aade031020fd936bd28d535b Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:52:57 2021 +0100 Add CSS styles diff --git a/css/base.scss b/css/base.scss index e69de29..26abf0f 100644 --- a/css/base.scss +++ b/css/base.scss @@ -1,7 +1,7 @@ body { background-color: $white; margin: 0px; line-height: 20px; - color: $grey; + color: red; }

完成後,您可以運行git bisect reset將您的分支重置為正常工作狀態。

提交越接近,Git 就越容易找到問題,但我之前已經採取了 10 步,仍然很容易找到錯誤的提交。 它不能保證有效,但它在大多數情況下都為我找到了問題。 恭喜,現在你是密碼考古學家了!

擠壓你的提交

我之前在一個全球組織的一個開源項目上全職工作,我很快就知道壓縮或合併你的提交是多麼重要。 我認為這是一個很好的習慣,即使你的雇主不需要它。 對於其他需要審查和編輯您稍後構建的功能的開發人員來說,這尤其體貼。

為什麼要壓縮你的提交?

  • 您的存儲庫的貢獻者更容易閱讀。 想像一下,如果你有一個這樣的提交列表:
    • 實施輪播滑塊
    • 為輪播添加樣式
    • 將按鈕添加到輪播
    • 使用輪播修復 IE 中的奇怪問題
    • 調整輪播中的邊距

    將這些壓縮成一個“將輪播添加到主頁”的提交要容易得多。

  • 如果每次發出拉取請求時,都必須將提交壓縮為一個,它會鼓勵您保持提交消息的可理解性和相關性。 你見過多少次提交標題為“WIP”、“登錄頁面的錯誤修復”或“修復錯字”? 擁有相關的提交名稱很重要,例如“#444 登錄頁面的錯誤修復 - 修復由於缺少 $scope 函數而導致的閃爍”。

您可能不想壓縮提交的一個原因可能是因為您正在開發一個非常詳細且冗長的功能,並且希望為自己保留每日曆史記錄,以防以後遇到錯誤。 然後您的功能更容易調試。 但是,當您將功能檢入主分支並且您確信它沒有錯誤時,壓縮仍然是有意義的。

在這種情況下,我做了五次提交,但它們都與一個特性相關。 我的提交信息也與單獨的優點密切相關——我所有的提交都是關於為這個新功能設置頁面樣式:

 $ git log commit a8fbb81d984a11adc3f72ce27dd0c39ad24403b7 (HEAD -> main) Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 11:16:10 2021 +0100 Import colors commit e2b3ddd5e8b2cb1e61f88350d8571df51d43bee6 Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 11:15:32 2021 +0100 Add new color commit d647ac489ad43b3c6eaea5aceb02b0a7d7e5cf8e Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 10:50:48 2021 +0100 Change text color commit c005d9ceeefd4a8d4e553e825fa40aaafdac446e Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 09:59:57 2021 +0100 Add CSS styles commit 9e046b7df59cef07820cc90f694fabc666731bd2 Author: Ursula Clarke <[email protected]> Date: Tue Jan 11 09:56:28 2021 +0100 Add second paragraph to page commit 5aff973577d67393d914834e8af4c5d07248d628 Author: Ursula Clarke <[email protected]> Date: Mon Jan 10 16:04:22 2021 +0100 Add colors CSS file and edit background color

您也可以使用git merge --squash ,但我認為使用rebase更清晰,因為當您選擇提交時,更容易查看提交描述。 如果您運行git merge --squash ,您首先必須對您的提交進行硬重置( git reset --hard HEAD~1 ),並且很容易對您需要執行此操作的確切提交次數感到困惑。 我發現git rebase更直觀。

首先運行git rebase -i --root ,命令行上的默認文本編輯器將打開您的提交列表:

 pick eb1eb3c Update homepage pick 5aff973 Add colors CSS file and edit background color pick 9e046b7 Add second paragraph to page pick c005d9c Add CSS styles pick d647ac4 Change text color pick e2b3ddd Add new color pick a8fbb81 Import colors # Rebase a8fbb81 onto b862ff2 (7 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out

您可能只想壓縮最後幾個提交,在這種情況下,您可以運行git rebase -i HEAD~3並顯示最後三個提交:

 pick eb1eb3c Update homepage pick 5aff973 Add colors CSS file and edit background color pick 9e046b7 Add second paragraph to page # Rebase b862ff2..9e046b7 onto b862ff2 (3 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out

現在我們可以將所有提交壓縮到第一個提交中,如下所示。

 pick eb1eb3c Update homepage squash 5aff973 Add colors CSS file and edit background color squash 9e046b7 Add second paragraph to page # Rebase b862ff2..9e046b7 onto b862ff2 (3 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out

當您保存文件時,Git 將打開您的提交消息進行編輯。

 # This is a combination of 3 commits. # This is the 1st commit message: Update homepage # This is the commit message #2: Add colors CSS file and edit background color # This is the commit message #3: Add second paragraph to page # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Wed Jan 13 18:31:28 2021 +0100 # # interactive rebase in progress; onto b862ff2 # Last commands done (3 commands done): # squash 5aff973 Add colors CSS file and edit background color # squash 9e046b7 Add second paragraph to page # No commands remaining. # You are currently rebasing branch 'main' on 'b862ff2'. # # Changes to be committed: # new file: .gitignore # new file: css/base.css # new file: css/base.scss # new file: css/colors.css # new file: css/colors.css.map # new file: css/colors.scss # new file: css/common.css # new file: css/common.scss # new file: index.html #

在我們進行 rebase 的同時,我們還可以編輯提交描述,使其更易於閱讀。

 Implement new design for homepage. Add .gitignore file for Sass folder. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. #

再次保存此文件,您就完成了! 當我們再次查看 Git 日誌時,我們可以看到只有一個乾淨的提交。

 [detached HEAD 574ec7e] Implement new design for homepage. Add .gitignore file for Sass folder. Date: Wed Jan 13 18:31:28 2021 +0100 10 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 css/base.css create mode 100644 css/base.scss create mode 100644 css/colors.css create mode 100644 css/colors.css.map create mode 100644 css/colors.scss create mode 100644 css/common.css create mode 100644 css/common.scss create mode 100644 index.html create mode 100644 verylargefile.txt Successfully rebased and updated refs/heads/main. $ git log commit 574ec7e5d7d7a96427e049cad9806cdef724aedd (HEAD -> main) Author: Ursula Clarke <[email protected]> Date: Wed Jan 13 18:31:28 2021 +0100 Implement new design for homepage. Add .gitignore file for Sass folder.

Git 變基

開發人員通常對使用git rebase猶豫不決,因為他們知道 rebase 可用於從代碼庫中永久刪除文件。

正如我們在上面看到的, git rebase可用於保留和整理代碼以及刪除 - 但是如果您真的想從歷史記錄中永久刪除文件怎麼辦?

我曾經目睹過一個場景,我們的開發團隊的一名成員不小心將一個非常大的文件提交到了代碼庫中。 它是一個更大的分支的一部分,所以這個大文件在代碼審查中沒有被注意到,並且被錯誤地簽入了主分支。 每當有人想要重新克隆存儲庫時,這都會成為一個問題 - 下載需要很長時間! 當然,有問題的文件是不必要的。 如果該文件是對主分支的最後一次提交,則不會有問題 - 在這種情況下,您可以運行硬重置( git reset --hard HEAD~1 )並強制推送分支。

同樣,如果文件是給定提交中的唯一更改,您可以通過運行git reset --hard <commit-id>刪除整個提交。 但是,在我們的場景中,大文件與我們希望保留在歷史記錄中的其他代碼一起提交,作為倒數第二次提交。

找到麻煩的提交後,使用git checkout和提交哈希檢查它:

 $ git checkout ce861e4c6989a118aade031020fd936bd28d535b Note: checking out 'ce861e4c6989a118aade031020fd936bd28d535b'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at ce861e4 Add CSS styles

刪除文件,或編輯您的代碼,並保留您想要保持不變的代碼(或文件)。

 $ rm verylargefile.txt $ git status HEAD detached at ce861e4 Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: verylargefile.txt no changes added to commit (use "git add" and/or "git commit -a")

確保運行git add -A以便暫存已刪除的文件並且 Git 知道將其刪除。 現在運行git commit --amend -v ,Git 會要求你編輯提交信息。

在此之後,運行git rebase --onto HEAD <commit-id> main 。 這是您可能會遇到一些合併衝突的地方,這意味著您的新提交和舊代碼之間存在衝突。 Git 會要求你解決衝突:

 $ git add -A $ git status HEAD detached at ce861e4 Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: verylargefile.txt $ git commit --amend -v [detached HEAD 7c9516a] Add CSS styles Date: Thu Jan 14 14:43:54 2021 +0100 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 css/common.css.map delete mode 100644 verylargefile.txt $ git status HEAD detached from ce861e4 nothing to commit, working tree clean $ git rebase --onto HEAD ce861e4 First, rewinding head to replay your work on top of it... Fast-forwarded HEAD to HEAD.

如果您在文本編輯器中打開該文件,您會看到 Git 已標記出兩個版本的索引文件。 您只需刪除一項或編輯一項即可保留您喜歡的更改。

 <p> Toptal was created by engineers. We are entrepreneurs, all passionate about working with top tech talent and exciting companies from all over the world. </p> <<<<<<< HEAD <p> Toptal connects the top 3% of freelance talent all over the world. </p> </main> </body> </html> ======= </main> </body> </html> >>>>>>> Add index file

<<<<<<< HEAD和等號行之間的代碼是一個版本,等號和>>>>>>> Add index file之間的代碼是“添加索引文件”提交的版本. 所以你可以看到一個版本有“Toptal連接全球前3%的自由職業者”的附加段落,而另一個沒有。

保存您編輯的文件並運行git add filename後跟git rebase --continue 。 如果沒有更改,您也可以運行git rebase --skip 。 如果您的“大文件”提交和 main 上的最新提交之間有很多提交,則可能需要一段時間才能完成變基。

請耐心等待,如果您在一個大型團隊中,請確保獲得第二意見! 如果可能的話,與編寫您要合併的提交的人協商尤其重要。

請記住,合併更改與歷史記錄中的特定提交相關,而不是您最新的更改。 即,如果您正在編輯網站上的文本為 Arial 時的提交,而現在是 Verdana,您仍應將該提交保留為 Arial 的歷史作為字體。

另請注意,如果 Git 看到空格或行尾字符,可能會導致合併衝突,所以要小心!

不僅僅是提交和拉取

Git 比許多開發人員想像的更強大。 如果您碰巧要介紹一個新手,請確保給他們一些關於這些寶貴功能的提示。 它使工作流程更加高效。

想要了解更多關於 Git 的信息? 看看 Toptal 的 Git 技巧和實踐頁面。