常见 Git 问题的 4 个实用解决方案

2025-05-25

常见 Git 问题的 4 个实用解决方案

封面图片:克里斯蒂娜·莫里洛(Christina Morillo)创作的圆形棕色木桌上的黑色和银色笔记本电脑
如今,大多数开发人员都在使用某种形式的版本控制,而当今最流行的版本控制系统是 git。

我写了一些实用的 git 文章:

这篇文章将是这些文章的延续,但会更加注重实用性。在之前的文章中,我分享了一些可以用 Git 做的有趣的事情。在本文中,我将重点介绍每个开发人员都应该掌握的知识。

1. 在最后一次提交中添加更改

对于忘记暂存更改的人来说(也就是曾经使用过 git 的每个人),修改最后一次提交非常方便。

这真的很简单。你只需要暂存你忘记暂存的文件,然后使用该git commit --amend命令。

假设我忘记将我的更改暂存到 README 文件中,我会这样做:

git add README.md
git commit --amend
Enter fullscreen mode Exit fullscreen mode

系统会提示我修改提交消息,然后:Tada!README.md 更改已添加到最后一次提交。

2. 将最后一次提交拆分为多次提交

假设你遇到了另一个问题。如果你不小心暂存了某些内容,然后又提交了一次,而你原本打算进行两次提交。

您需要做的就是使用以下命令返回一次提交git reset

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

然后使用git add来暂存您的更改(我建议使用git add --patch如果您不熟悉,我写了一篇关于它的文章),然后提交:

git add --patch
git commit -m "This is the first commit"
Enter fullscreen mode Exit fullscreen mode

然后根据需要重复该过程多次:

git add --patch
git commit -m "This is the second commit"
Enter fullscreen mode Exit fullscreen mode

一次提交变成了多次提交。💥

3. 将一些先前的提交压缩为一个提交

每个开发者都应该知道如何压缩提交。如果你为开源项目做出贡献,并且提交了一些冗余或措辞不当的提交信息,那么你可能会被要求这样做。

我将分享几种方法,一种方法使用git rebase,另一种不使用。

无需git rebase,您可以轻松地将最近几个提交压缩成一个。首先,您需要使用git reset命令 来回移动要压缩的提交。

HEAD~x您可以通过传递命令来执行此操作git rebase,其中x是您想要压缩的提交次数。

例如,如果您想将前三个提交压缩为一个提交,您可以运行以下命令:

git reset HEAD~3
Enter fullscreen mode Exit fullscreen mode

如果您检查日志,您会注意到您现在比之前的位置落后了三次提交。

快速git log显示,前三次提交的所有更改仍然存在于您的机器上,但现在已取消暂存。

要将它们压缩成一个提交,您需要做的就是将它们暂存并提交:

git commit -am "Some commit message"
Enter fullscreen mode Exit fullscreen mode

恭喜!🎉 您已将三个提交合并为一个!

或者,你可以使用git rebaseinteractive 标志。如下所示:

git rebase -i master
Enter fullscreen mode Exit fullscreen mode

将 master 替换为您最终合并的分支。

git rebase -i将会把你带入一个类似这样的文本编辑器:

pick 1eabc17a8 Add data-attributes to container elements
pick 90c58b487 Fix react-dom warning

Rebase 5f5e140ea..90c58b487 onto 5f5e140ea (2 commands)

Commands:
p, pick <commit> = use commit
r, reword <commit> = use commit, but edit the commit message
e, edit <commit> = use commit, but stop for amending
s, squash <commit> = use commit, but meld into previous commit
f, fixup <commit> = like "squash", but discard this commit's log message
x, exec <command> = run command (the rest of the line) using shell
b, break = stop here (continue rebase later with 'git rebase --continue')
d, drop <commit> = remove commit
l, label <label> = label current HEAD with a name
t, reset <label> = reset HEAD to a label
m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
.       create a merge commit using the original merge commit's
.       message (or the oneline, if no original merge commit was
.       specified). Use -c <commit> to reword the commit message.

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
Enter fullscreen mode Exit fullscreen mode

现在,如果您以前从未执行过变基操作,这可能会有点令人生畏,但实际上它非常简单。

pick要压缩这两个提交,只需将第二行的单词替换为squash

pick 1eabc17a8 Add data-attributes for styling checkout
squash 90c58b487 Fix react-dom warning; non-critical, but annoying
Enter fullscreen mode Exit fullscreen mode

保存文件(如果您使用的是 Vim,则使用 完成:wq),然后您将进入文本编辑器来编写提交消息。当您满意后,保存提交,就大功告成了!

哇喔!🤠 现在您已经了解了两种不同的压缩提交的方法。

rebase你可能会想,既然第一种方法这么简单,为什么还要选择使用?这可以理解。使用这种方法git rebase可以让你更好地控制重大变更,我建议你慢慢习惯这种方法来解决这些问题。

4. 从另一个分支获取最新的更改

当您使用功能分支时,您可能每天都会多次执行此操作!

也有两种方法可以做到这一点。

最常用的方法是使用git merge命令。你只需要运行git merge命令,并传入你想要拉取更改的分支名称即可。例如,如果你要从 master 获取最新的更改:

git merge master
Enter fullscreen mode Exit fullscreen mode

这会创建一个新的提交,称为合并提交。合并提交本质上只是合并发生的一个指示,将这些提交保存在 git 历史记录中并没有什么问题(在我看来)。

然而,有些人不喜欢在仓库历史记录中看到合并提交。对于这类人,你可以使用这个功能git rebase来跟上另一个分支。我通常喜欢用下面的方法让我的功能分支跟上主分支:

git rebase master
Enter fullscreen mode Exit fullscreen mode

只要没有发生任何奇怪的事情,这将轻松地在主分支上重播您的更改,而无需创建合并提交。

🤗

还有更多...

这些天我写了很多文章,我运营着一个播客,并且我已经开始发送一份关于我听到的所有精彩故事的新闻通讯摘要。

您还可以在Twitter上关注我,在那里我会制作一些有趣的表情包并谈论作为一名开发人员的感受。

文章来源:https://dev.to/jacobherrington/4-useful-patterns-in-git-19ac
PREV
2分钟内掌握5个实用的Bash技巧
NEXT
你应该知道的另外 10 个 Git 技巧