🐙
Git Developer Guide
About
  • Overview
  • Scope of this book
  • Table of Content
  • 🐢Introduction to Version Control
    • What is Version Control?
    • Overview of git and it's benefits
    • Setting up Git on Different Platforms
  • 🍼Git Fundamentals
    • Initialising a new Git repository
    • Understanding the Git Workflow
    • Committing Changes and Writing Good Commit Messages
    • Viewing and Navigating Commit History
    • Git Basics - Practice Scenarios
      • Initialising a Git Repository
      • Committing Changes
      • Exploring Commit History
      • Amending and Undoing Commits
  • 🦕Working With Git
    • What is Git Branch?
    • Creating and Switching Between Branches
    • Merging Branches and Resolving Conflicts
    • Best Practices for Branch Management
    • Git Workflows
    • Git Log
    • Git Stash
    • Working with Git - Practice Scenarios
      • Creating and Switching Between Branches
      • Merging Branches and Resolving Conflicts
      • Branching Strategies in a Team Project
      • Rolling Back to a Previous Version
      • Experimenting with Feature Branches
      • Working with Stash
  • 🤝Working with Remote Repositories
    • Cloning a Repository from Remote
    • Pushing and Pulling Changes to and from Remote Repositories
    • Collaborative Workflows - Forking, Branching, and Pull Requests
    • Resolving Conflicts in a Collaborative Environment
    • Collaborating with Git - Practice Scenarios
      • Cloning a Remote Repository
      • Pushing and Pulling Changes
      • Collaborative Workflow with Forking and Pull Requests
      • Resolving Conflicts in a Pull Request
  • 🏆Advanced Git Features
    • Aliases and Custom Configurations
    • Working with Tags and Releases
    • Rewriting Commit History with Interactive Rebase
    • Utilising Git Hooks for Automation
    • Advanced Git Features - Practice Scenarios
      • Creating Custom Git Aliases
      • Working with Tags and Releases
      • Rewriting Commit History with Interactive Rebase
      • Using Git Hooks for Automated Testing
  • 😎Git in Real-World
    • Managing a Project with Multiple Contributors
    • Integrating Git with Continuous Integration, Continuous Deployment (CI, CD)
    • Versioning Assets with Git LFS (Large File Storage)
    • Deploying a Web Application using Git
    • Git In Real World - Practice Scenarios
      • Managing a Project with Multiple Contributors
      • Integrating Git with CICD Pipelines
      • Versioning Assets with Git LFS
      • Deploying a Web Application using Git
  • Git Troubleshooting
    • Common Mistakes and Pitfalls When Using Git
    • Undoing Changes with Git - Reverting and Resetting
    • Recovering Lost Commits or Branches
    • Dealing with Repository Corruption or Other Issues
  • Git Best Practices and Tips
    • Creating efficient git workflows: writing clean code for faster reviews
    • The importance of clean code in collaborative development
    • Significance of consistent naming conventions & coding Standards
    • Good code documentation for better git workflows
    • Writing meaningful git commit messages
    • Atomic commits in git & it's benefits for software teams
    • Structuring code & managing dependencies for better git workflows
    • Git branching strategies for software teams
  • Conclusion & Next Steps
    • Recap of Key Concepts and Commands
    • Further Resources for Expanding Git Knowledge
    • Encouragement and Tips for Continued Learning and Practice
  • License Considerations
Powered by GitBook
On this page

Was this helpful?

  1. Advanced Git Features
  2. Advanced Git Features - Practice Scenarios

Rewriting Commit History with Interactive Rebase

Scenario

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

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:

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

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:

edit abcd123 Commit 1: Fix typo in README

Save and close the editor.

Step 4: Modifying the Selected Commit

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:

git add .

Then, amend the commit using the following command:

git commit --amend

Update the commit message if needed and save the changes.

Step 5: Continuing the Rebase Process

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

git rebase --continue

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

Step 6: Reordering Commits

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

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

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:

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.

PreviousWorking with Tags and ReleasesNextUsing Git Hooks for Automated Testing

Last updated 1 year ago

Was this helpful?

🏆