28 Controlling How You Replay Commits

Rebasing in Git replays one set of commits on top of another. We covered the basic case in Task 16, Rewriting History by Rebasing. There is an interactive mode to git rebase that lets you control how the commits are replayed.

Like a regular rebase, git rebase -i takes the commits in your current branch and replays them against another point in your repository’s history. You can use this to change the order of commits, squash commits together, or edit a previous commit. Consider the following example.

You add a new feature and then start working on the next feature. You realize a little while later that you could have implemented it in a cleaner fashion. You could amend the commit (see Task 35, Fixing Commits) if it’s the last commit, but since some time has elapsed, you’ve already created other commits.

This is where interactive rebasing comes into play. Create the second commit with the fix you want to use, and then launch git rebase -i and pass it the commit ID of the original commit ID minus one. For example, if the commit ID was 322dafc, you should use git rebase -i 322dafc^.

Git rebase’s interactive mode launches your editor with a list of commits. You can use the editor to move the last line (the commit you just made) up to immediately after the commit you want to merge it into; then change the first word to say fixup to merge it into the original commit.

There five different options: pick, reword, edit, squash, and fixup. You can use any of these as the first word or the first letter of a line to adjust that commit when it is replayed.

Like a regular git rebase, be careful how you rebase. You can cause issues for other people who have synced off of your repository if you rebase code that you’ve already shared. Remember this simple rule of thumb: don’t rebase code you’ve shared.

What To Do...
  • Rebase interactively.
     
    prompt>​ git rebase -i <commit ID
     
    ...​ example ...
     
    prompt>​ git rebase -i HEAD~5

    For example, to interactively rebase the last five commits, use this:

     
    prompt>​ git rebase -i HEAD~5
     
    ...​ or ...
     
    prompt>​ git rebase -i HEAD^^^^^
  • See an example interactive rebase.

    The following is the result of a git rebase -i on one of my projects:

     
    pick 6b24857 remove theme from repo
     
    pick 29e2376 add in dependency on external repo for theme
     
    pick 7ce4bdf add code to handle deployment of new docs
     
     
    # Rebase 86968d5..7ce4bdf onto 86968d5
     
    #
     
    # 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
     
    #
     
    # If you remove a line here THAT COMMIT WILL BE LOST.
     
    # However, if you remove everything, the rebase will be aborted.
     
    #

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.226