What happens when Git rebase with master?

When you perform a Git rebase with the master branch, you are essentially integrating changes from the master branch into your current branch by moving or “replaying” your branch’s commits on top of the latest commit in the master branch. Here’s a step-by-step explanation of what happens during a typical rebase operation:

  1. Check out your branch: First, make sure you are on the branch that you want to rebase. You should run git checkout your-branch to switch to that branch.
  2. Start the rebase: Run the rebase command with the master branch as the reference. You can do this with git rebase master.
  3. Git identifies the common ancestor: Git will find the common ancestor commit between your branch and the master branch. This is the point at which your branch diverged from master.
  4. Git saves your branch’s changes: Git temporarily saves your branch’s changes (commits) as a series of patches.
  5. Your branch is updated: Git moves your branch pointer to point to the same commit as the latest commit on the master branch.
  6. Your changes are applied: Git takes the patches that were saved in step 4 and applies them one by one on top of the latest commit in the master branch. This effectively “replays” your changes on top of the current state of master.
  7. Resolve conflicts: If there are any conflicts between your changes and the changes in the master branch, Git will pause the rebase and allow you to resolve these conflicts manually. After resolving conflicts, you can continue the rebase with git rebase –continue.
  8. Completion: Once all your changes have been successfully replayed, the rebase is complete, and your branch is now based on the latest commit in the master branch.
# Command
git rebase master

In summary, a git rebase master rewrites your branch’s commit history to make it appear as though you made your changes on top of the latest changes in the master branch. This can result in a cleaner and more linear history, but it should be used with caution, especially when collaborating with others, as it can alter commit history and potentially cause conflicts.