Skip to content

Instantly share code, notes, and snippets.

@bkemper
Last active December 7, 2021 19:28
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bkemper/ca6ac68b174a047b5ccde3930c8568dc to your computer and use it in GitHub Desktop.
Save bkemper/ca6ac68b174a047b5ccde3930c8568dc to your computer and use it in GitHub Desktop.
How to edit a commit with interactive rebase

While on a branch with a couple of commits, you can edit a commit with interactive rebase. This should be used sparingly and only on branches and never on master.

  1. Checkout the branch

$ git checkout my-branch

  1. Get the ref of the commit that you want to edit from the commit log. (e.g. 67b191fc62eda52b5b208cc4de50df7144a03171)

$ git log

  1. Begin rebasing. (Note: The carot at the end of your commit ref is important to start from the commit before your commit.)

$ git rebase --interactive 67b191fc62eda52b5b208cc4de50df7144a03171^

  1. Change "pick" to "edit" for your commit and save.

  2. Do not be alarmed. Your commit will be applied first and then the command will pause to allow you to make amendments. Make your changes then commit them.

$ git commit --all --amend

  1. The command will remain paused for additional amendments. When all is good, continue to reaplly the rest of your commits.

$ git rebase --continue

  1. Not that your branch is in a good state. Push to origin.

$ git push --force

@droopycom
Copy link

What exactly happen when you say: "Your commit will be applied first". Is a new commit created ? Or are the changes applied to the working tree only? Or to the index?

@bryanbraun
Copy link

@droopycom, I had the same question so I dug into it and wrote up some more details about the process here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment