> For the complete documentation index, see [llms.txt](https://gitdeveloperguide.solomonmarvel.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitdeveloperguide.solomonmarvel.com/advanced-git-features/advanced-git-features-practice-scenarios/rewriting-commit-history-with-interactive-rebase.md).

# Rewriting Commit History with Interactive Rebase

#### <mark style="color:blue;">Scenario</mark>

### Modifying or Reordering Commits

In this example, let's assume you are working on a project and have made several commits. However, you realise that there is a mistake in one of the earlier commits, and you want to correct it. Additionally, you would like to reorder a few commits to improve the logical flow of the project's history.

#### Step 1: Starting the Interactive Rebase&#x20;

To begin the interactive rebase, open a terminal or Git Bash and navigate to the root directory of your Git repository. Execute the following command:

```bash
git rebase -i HEAD~n
```

Replace `n` with the number of commits you want to include in the interactive rebase. For example, to include the last 5 commits, use `git rebase -i HEAD~5`.

#### Step 2: Interactive Rebase Editor

After executing the command, Git will open an interactive rebase editor with a list of commits. Each commit is prefixed with the word "pick." This is where you can modify, reorder, or squash commits.

***Example Interactive Rebase Editor:***

```
pick abcd123 Commit 1: Fix typo in README
pick efgh456 Commit 2: Refactor code for performance
pick ijkl789 Commit 3: Add new feature
```

#### Step 3: Modifying Commits&#x20;

To modify a commit, replace the word "pick" with "edit" in front of the commit you wish to modify. For instance, if you want to modify "Commit 1: Fix typo in README," change it to:

```bash
edit abcd123 Commit 1: Fix typo in README
```

*Save and close the editor.*

#### Step 4: Modifying the Selected Commit&#x20;

Git will now pause the rebase process and allow you to modify the selected commit. Make the necessary changes using your preferred code editor. Once you are done, stage the changes by executing:

```bash
git add .
```

Then, amend the commit using the following command:

```bash
git commit --amend
```

Update the commit message if needed and save the changes.

#### Step 5: Continuing the Rebase Process&#x20;

After amending the commit, continue the rebase process by executing the following command:

```bash
git rebase --continue
```

Git will apply the changes made in the amended commit and proceed with the remaining commits.

#### Step 6: Reordering Commits&#x20;

To reorder commits, open the interactive rebase editor again by executing:

```bash
git rebase -i HEAD~n
```

Replace `n` with the number of commits you included in the initial interactive rebase.

Within the interactive rebase editor, simply rearrange the order of the commits by moving their respective lines. Save and close the editor.

#### Step 7: Squashing Commits&#x20;

Git's interactive rebase feature also allows you to squash commits together to create a more concise and coherent commit history.

To squash commits, open the interactive rebase editor once again:

```bash
git rebase -i HEAD~n
```

Within the editor, change the word "pick" to "squash" or "s" for the commits you want to squash.

*Example Interactive Rebase Editor with Squashing:*

```
pick abcd123 Commit 1: Fix typo in README
squash efgh456

 Commit 2: Refactor code for performance
squash ijkl789 Commit 3: Add new feature
```

*Save and close the editor.*

#### Step 8: Addressing Considerations and Best Practices

When rewriting commit history, it is essential to keep in mind a few considerations and best practices:

* **Collaboration**: Avoid rewriting commits that have been pushed to a shared repository. Rewriting history can cause conflicts for other team members who have based their work on the original commits.
* **Documentation**: Ensure that the rewritten commits maintain a clear and meaningful history, making it easier for future contributors to understand the project's evolution.
* **Backup**: Before performing any significant rewrite, create a backup or branch to preserve the original commit history in case anything goes wrong during the rebase process.
* **Communicate**: If you are working on a shared repository, communicate with your team before rewriting commit history to ensure everyone is aware of the changes.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gitdeveloperguide.solomonmarvel.com/advanced-git-features/advanced-git-features-practice-scenarios/rewriting-commit-history-with-interactive-rebase.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
