Git rebase workflow¶
The Git rebase workflow is a version control strategy used to integrate changes from one branch into another by rewriting commit history. It is often employed to maintain a linear project history or to synchronize local changes with a remote repository before pushing^[600-developer-tools-git-git-rebase.md].
Error Recovery¶
Rebasing rewrites history, which can lead to data loss if performed incorrectly. If a rebase operation fails or produces unintended results, it can be reverted using the following command^[600-developer-tools-git-git-rebase.md]:
git reset ORIG_HEAD --hard
A common error is rebasing the master branch onto a feature branch (e.g., git rebase dev), which should be avoided^[600-developer-tools-git-git-rebase.md].
Feature Branch Workflow¶
When using a dedicated feature branch, the typical workflow involves rebasing the feature branch onto the updated main branch (e.g., master) before merging. This ensures the feature branch contains the latest upstream changes^[600-developer-tools-git-git-rebase.md].
The process is as follows:
- Switch to target:
git checkout master^[600-developer-tools-git-git-rebase.md] - Update target:
git pull^[600-developer-tools-git-git-rebase.md] - Return to feature:
git checkout issue^[600-developer-tools-git-git-rebase.md] - Start rebase:
git rebase master^[600-developer-tools-git-git-rebase.md] - Resolve conflicts: If conflicts occur, resolve them and run
git rebase --continue^[600-developer-tools-git-git-rebase.md]. - Stage changes:
git add .^[600-developer-tools-git-git-rebase.md] - Commit:
git commit -m "[issue]"^[600-developer-tools-git-git-rebase.md] - Merge back: Switch to master and merge the feature branch:
git checkout masterfollowed bygit merge issue^[600-developer-tools-git-git-rebase.md]. - Cleanup: Delete the feature branch:
git branch -d issue^[600-developer-tools-git-git-rebase.md]. - Push: Push the merged changes:
git push master^[600-developer-tools-git-git-rebase.md].
To modify the most recent commit during this process, git commit --amend can be used^[600-developer-tools-git-git-rebase.md].
Single Branch Workflow¶
When multiple developers are working on the same branch, git rebase is used to move local commits to the tip of the remote branch, effectively replaying local work on top of incoming changes^[600-developer-tools-git-git-rebase.md].
Manual Rebase¶
- Fetch latest changes:
git fetch^[600-developer-tools-git-git-rebase.md] - Rebase local commits:
git rebase^[600-developer-tools-git-git-rebase.md] - Resolve conflicts (if any) and continue:
git rebase --continue^[600-developer-tools-git-git-rebase.md] - Push updates:
git push^[600-developer-tools-git-git-rebase.md]
Pull with rebase¶
A streamlined approach combines pulling and rebasing into a single step^[600-developer-tools-git-git-rebase.md].
- Commit local changes^[600-developer-tools-git-git-rebase.md]
- Pull and rebase:
git pull --rebase^[600-developer-tools-git-git-rebase.md] - Push updates:
git push^[600-developer-tools-git-git-rebase.md]
To avoid adding --rebase manually every time, Git can be configured to use rebase by default for git pull^[600-developer-tools-git-git-rebase.md].
git config --global pull.rebase true
git config --global branch.autoSetupRebase always
Sources¶
^[600-developer-tools-git-git-rebase.md]
Related¶
- [[Git]]
- [[Version Control]]
- [[Continuous Integration]]