May 2025
Beginner to intermediate
548 pages
7h 40m
Chinese
本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com
Git 拥有大量的命令和选项,是对仓库进行各种强大修改的丰富资源。但有时,完成特定任务的实际步骤有点难以捉摸。有时,特定命令和选项的目的并不明确,或者在技术描述中变得模糊不清。
本章收集了一些技巧、窍门和技术,重点介绍 Git 进行有趣转换的能力。
在本地分支上开发多提交变更 序列时,我们经常会意识到需要对序列中较早的提交进行额外修改。与其在旁边潦草地写个备注,然后再回来看,我们可以选择立即编辑并将该修改直接引入新的提交中,并在提交日志条目中添加备注,提醒我们应将其压入之前的提交中。
不过,当我们最终清理完提交序列并想使用git rebase -i 时,可能会发现自己的工作目录很脏。在这种情况下,Git 会拒绝进行重置:
$ git show-branch --more=10
[main] Tinker bar
[main^] Squash into 'More foo and bar'
[main~2] Modify bar
[main~3] More foo and bar
[main~4] Initial foo and bar.
$ git rebase -i main~4
error: cannot rebase: You have unstaged changes.
error: Please commit or stash them.
如果出现这种情况,只需先用git stash 清理掉不干净的工作目录即可!
$ git stash
Saved working directory and index state WIP on main: ed6e906 Tinker bar
$ git rebase -i main~4
# In the editor, move main^ next to main~3
# and mark it for squashing.
pick 1a4be28 More foo and bar
squash 6195b3d Squash into 'more foo and bar'
pick 488b893 Modify bar
pick ed6e906 Tinker bar
# Follow instructions in your text editor
[detached HEAD e3c46b8] More foo and bar with additional stuff.
Date: Sun Mar 27 16:18:02 2022 +0200
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2
create mode 100644 file4
Successfully rebased and updated refs/heads/main.
当然,您现在需要恢复工作目录的更改:
$ git stash pop # On branch main # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard ...Read now
Unlock full access