Hello Developers!
Ever found yourself in a situation where your feature branch is behind the master branch, and your team leader says, "Raised a PR without creating a merge commit!"?
Don't worry we all have been there. Instead of creating messy merge commits, you can use rebase to catch up your branch with the master branch.
Rebasing helps keep your commit history clean and linear, making it easier for everyone to follow. Plus, it’s a handy way to resolve conflicts.
Steps to follow:
1. Before starting the rebase, make sure you pull the latest changes from the target branch:
git pull origin <branch_name>
2. After taking pull, navigate to the branch you want to rebase using the below command:
git checkout <branch_name>
3. Start rebasing your branch onto the target branch by running below command:
git rebase <target_branch>
After running this command, you’ll see a message successfully rebased and updated 'refs/heads/branch_name'.
4. If there are any merge conflicts during the rebase, Git will stop and notify you. Check the source control panel to identify which files have merge conflicts.
5. Open the conflicted files, resolve the issues, and stage them using below command:
git add <conflicted_file_name>
Then, continue the rebase using below command:
git rebase --continue
If the conflicts are unrelated, you can skip them using below command:
git rebase --skip
Repeat the process until the rebase is finished.
6. Once the rebase is complete, verify the commit history by running below command:
git log --oneline
7. Push the rebased branch to a remote repository using below command:
git push origin <branch_name> --force-with-lease
That’s it!
Your branch is now successfully rebased to the target branch. By rebasing you can keep your branches and commit history linear🚀.
Rebasing not only helps you to maintain clean and linear commit history also helps you to resolve conflicts, and ensure that code stays in sync with master branch without unnecessary merge commits.
Happy coding! 🚀
Git rebase process helps us by moving branch commits to the top of another branch, eliminating merge commits and creating a cleaner, more readable project history.
Key benefits:
'--force-with-lease' works as a safety check when force pushing, preventing accidental overwrites of remote changes.
It also ensures you don't unintentionally discard work added by other team members, protecting collaborative efforts and maintaining version control.
When conflicts occur during a Git rebase, follow the below steps:
Open the conflicted files, in the source control panel to identify which files have merge conflicts.
Resolve the conflicts and stage the file:
git add file-name
Continue the rebase process:
git rebase --continue
If more conflicts arise, repeat the process. To abort the rebase and revert to the previous state:
git rebase --abort
If the conflicts are unrelated, you can skip them using below command:
git rebase --skip
By this process you can resolve merge conflicts using rebase process.
Still have questions?
Send an email to archana@leadwalnut.com, OR
Book a FREE consultation with an expert developer here.