Skip to Content
《使用 Git 进行版本控制》第三版
book

《使用 Git 进行版本控制》第三版

by Prem Kumar Ponuthorai, Jon Loeliger
May 2025
Beginner to intermediate
548 pages
7h 40m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Git 进行版本控制》第三版

第 17 章 技巧、窍门和技术

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见: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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 GitHub Actions

学习 GitHub Actions

Brent Laster
《Kubernetes 最佳实践》第二版

《Kubernetes 最佳实践》第二版

Brendan Burns, Eddie Villalba, Dave Strebel, Lachlan Evenson
GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks

Publisher Resources

ISBN: 9798341657922