高级 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 技巧和实践页面。