🐙
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

Rewriting Commit History with Interactive Rebase

The interactive rebase feature in Git is a powerful tool that allows you to modify, reorder, or combine commits in your commit history.

This feature is particularly useful when you want to clean up your commit history before sharing it with others or when preparing a clean set of commits for integration into a main branch.

In this section, we will explore how to use interactive rebase and discuss best practices and considerations when rewriting commit history.

  1. What is Interactive Rebase? Interactive rebase is a feature in Git that enables you to selectively modify the commit history by interacting with each commit in a step-by-step manner. It allows you to alter commit messages, combine multiple commits, reorder commits, or even remove commits from your commit history.

  2. Using Interactive Rebase: To start an interactive rebase, you need to specify the commit from which you want to begin the rebase. Open your terminal and navigate to your Git repository. Then, execute the following command:

git rebase -i <commit>

Replace <commit> with the commit hash or a reference to the commit you want to start the interactive rebase from. For example, you can use a branch name, such as master, or a relative reference like HEAD~3 to indicate the third last commit.

  1. Interactive Rebase Commands: Once you start the interactive rebase, Git will open a text editor with a list of commits and corresponding commands. Here are some commonly used commands during interactive rebase:

  • pick: Keeps the commit as is.

  • reword: Allows you to change the commit message.

  • edit: Pauses the rebase process to let you modify the commit content.

  • squash: Combines the commit with the previous commit.

  • fixup: Combines the commit with the previous commit, discarding the commit message.

  • drop: Removes the commit from the commit history.

  1. Modifying Commit Messages: To modify a commit message during an interactive rebase, change the command from pick to reword for the corresponding commit in the text editor. Save the changes and exit the editor. Git will then prompt you to modify the commit message. Edit the message, save, and exit the editor again to continue the rebase.

  2. Reordering Commits: To reorder commits, simply rearrange the lines representing the commits in the text editor. Git will apply the commits in the order they appear in the list.

  3. Combining Commits: You can combine multiple commits into a single commit using the squash or fixup commands. The squash command preserves the commit message, while the fixup command discards it. To combine commits, change the command of the commits you want to combine to squash or fixup in the text editor. Git will prompt you to modify the resulting commit message or discard it, respectively.

  4. Removing Commits: If you want to remove a commit from the commit history, simply delete the corresponding line from the text editor during the interactive rebase. Git will exclude the deleted commit from the final commit history.

  5. Best Practices and Considerations: When rewriting commit history with interactive rebase, keep the following best practices and considerations in mind:

  • Interactive rebase is best suited for local branches, as rewriting history on shared branches can cause issues for collaborators.

  • Be cautious when rewriting commits that have already been pushed to a shared repository. This can create conflicts for other team members.

  • Use interactive rebase primarily for cleaning up and organising your commits, rather than modifying important or critical parts of the commit history.

  • Keep your changes focused and logically grouped. Combining too many unrelated changes into a single commit can make it harder to understand the commit history.

Interactive rebase provides a flexible and powerful way to rewrite commit history in Git.

PreviousWorking with Tags and ReleasesNextUtilising Git Hooks for Automation

Last updated 1 year ago

Was this helpful?

🏆