git rebase

Rebasing onto another branch

Rebasing takes the commits on your current branch and replays them on top of another branch, producing a linear history without a merge commit. This is an alternative to git merge, which preserves the branching structure. For background on both approaches, see branching and merging.

$ git rebase main

Note: Never rebase commits that have already been pushed to a shared or public branch. Rewriting history will cause problems for anyone who has based work on those commits.

Interactive rebase

Interactive mode lets you edit, reorder, squash, or drop commits before they are replayed. Useful for cleaning up a feature branch before merging. For a focused guide on combining commits, see how to squash multiple commits.

Open the last 3 commits for editing

$ git rebase -i HEAD~3

Git opens an editor listing the commits. Change pick to squash (or s) to fold a commit into the one above it, or reword (or r) to edit a commit message.

Handling conflicts during a rebase

When a conflict occurs, the rebase pauses and lets you resolve it manually.

After resolving conflicts, continue

$ git add <resolved-file>
$ git rebase --continue

Abort and return to the original state

$ git rebase --abort

Rebasing onto a specific commit

You can rebase onto any commit hash, not just a branch name:

$ git rebase <commit-hash>

If you only need to apply one or two specific commits rather than replaying an entire branch, git cherry-pick may be a better fit.

Once your code is ready, DeployHQ can deploy it to your servers automatically from any Git repository.