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

每个开发人员都应该对版本控制有很好的了解,而 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)

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 Reset

如果您确实发现自己不小心提交了一些混乱的代码,您可以进行“软”重置。这意味着代码看起来好像还没有提交。然后你可以在你的 IDE 中整理你的代码,然后再进行更清晰的提交。为此,您可以运行git reset --soft HEAD~1. 这将重置最近的提交。~您可以通过更改eg之后的数字来重置多个提交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 Bisect

我最喜欢的 Git 工具是git bisect. 我只需要它几次,但当我这样做时,它是无价的!我主要在大型代码库中使用它,其中存在一个问题,即使经过一些剧烈的调试,也没有人找到根本原因。

git bisect本质上是在两个给定的提交之间执行二进制搜索,然后为您提供特定提交的详细信息。您首先需要给 Git 一个好的提交,您知道您的功能正在运行,以及一个糟糕的提交。请注意,只要您有一个好的提交和一个坏的提交,这些提交就可以相隔数年(尽管时间越早,就越困难!)。

最有趣的git bisect是,当你开始时,你通常并不知道是谁写了错误的提交。发现 bug 出处的兴奋不止一次让几个同事围在我的电脑旁!

首先,检查有问题的分支并找到好的提交。您需要查看您的提交历史并找到提交哈希,然后检查该特定提交并测试您的分支。一旦找到工作的好点和坏点,就可以运行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 我们有一个错误的提交。

$ 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 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 发现了第一个错误的提交:

$ 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 函数而导致的闪烁”。

你可能不想压缩你的提交的一个原因可能是因为你正在处理一个非常详细和冗长的功能,并且想为自己保留每日历史记录以防你以后遇到错误。这样你的功能就更容易调试了。然而,当你将你的功能检查到你的主分支并且你确信它没有错误时,压缩仍然有意义。

在这个场景中,我做了五次提交,但它们都与一个功能相关。我的提交信息与 merit 的分离也太过密切相关——我所有的提交都是关于为这个新功能设计页面样式的:

$ 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因为当您挑选提交时,更容易看到提交描述。如果你运行 a 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 Rebase

开发人员通常不愿使用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 看到空格或行尾字符,可能会导致合并冲突,所以要小心!

猜你喜欢

转载自blog.csdn.net/qq_20173195/article/details/128248480
今日推荐