Chapter 17. Tips, Tricks, and Techniques

With a plethora of commands and options, Git is a rich resource for performing varied and powerful changes to a repository. Sometimes, though, the actual steps to accomplish a particular task are a bit elusive. Sometimes the purpose of a particular command and option isn’t really clear or becomes lost in a technical description.

This chapter provides a collection of tips, tricks, and techniques that highlight Git’s ability to do interesting transformations.

Interactive Rebase with a Dirty Working Directory

Frequently, when developing a multicommit change sequence on a local branch, we realize we need to make an additional modification to a commit we made earlier in the sequence. Rather than scribbling a note about it on the side and coming back to it later, as an option we can immediately edit and introduce that change directly into a new commit and add a note in the commit log entry reminding us that it should be squashed into a previous commit.

However, when we eventually get around to cleaning up our commit sequence and want to use git rebase -i, we might find ourselves with a dirty working directory. In this case, Git will refuse to do the rebase:

    $ 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.
    

If this happens, ...

Get Version Control with Git, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.