Specify Which Branch You Want to Rebase Agains
Rebase is a powerful Git utility that facilitates software engineers, developers, and programmers to simplify the merge process. Whether it is your first time using the rebase Git control, or have issues completing a rebase, it can be challenging at the beginning if you are not used to rebasing.
In this article nosotros are going to explain what rebase does in Git. Then, we will show yous the steps to follow when starting a rebase in Git. Finally, we will explain the reasons why you lot should consider using the rebase command more often.
How Git rebase Works
Git rebase is the process of updating a serial of commits of an existing branch to a new base commit. Unfortunately, explaining Git concepts doesn't brand too much sense or is hard to sympathise without a proper visualization of a git tree diagram. Hopefully, the following diagram volition assistance you empathize what git rebase does.
During software development, it is typical to accept a master or a main branch and multiple characteristic branches. Whenever we create a feature branch, the feature branch is created off of a specific branch, typically the latest commit of the chief co-operative.
As there are more people working on a project, other feature branches are created. Let's say, you created feature branch A, and someone else created feature branch B. In this scenario, we are going to assume someone in the squad has finished their development in feature B branch. Hence, it is normal for that person to merge the feature B branch to the chief branch to update the principal codebase typically used for production deployments.
Meanwhile, you have been working in feature A branch. Notwithstanding, since there are new changes in main branch after merging characteristic B co-operative, characteristic A branch no longer has the latest changes from main branch. Hence, in case you decide to merge feature A branch to master, there is a possibility of running into conflicts.
If you lot use the rebase command onto principal branch, you lot will update feature A branch base commit. In other words, you lot would get all of the latest changes from principal branch in feature A branch as if those commits happened first prior to you lot making commits to feature A branch, making sure feature A co-operative is up to date with main co-operative.
The rebase commands have different configurations that can be used prior to starting rebasing. Still, to start a rebase the only thing you lot should know is the name of the branch y'all are rebasing onto.
git rebase <branch rebasing onto> Steps to rebase in Git
At present that you sympathise what the rebase does in Git, nosotros volition show yous the steps to follow when starting a rebase. For the sake of making it simpler to follow, we are going to have as a reference the following diagram.
*Notice in the git tree structure we have a login-page co-operative and a primary branch.
These are the steps to rebase a branch in Git.
1. Checkout feature branch
Make sure to exist in the feature branch that you want to rebase.
git checkout login-page
two. Pull feature branch latest commits
We are going to pull the latest commits of the feature branch from the remote repository, even if you think you lot already have the latest commits in the local repository.
git pull 3. Remove whatever unstaged commits from feature branch (optional)
There might be changes in the local repository that take not been committed yet. If that is your case you have a couple of options. Either reset the branch and delete those changes or stash them.
I typically stash the changes in example I need to those changes after rebasing the branch unless I have added some logs to facilitate debugging.
git stash 4. Checkout co-operative you are planning to rebasing onto
Switch the electric current branch to the co-operative you are planning to rebase onto. It is common to rebase onto principal branch. However, you can rebase to any other branch too. For this tutorial, nosotros will checkout master co-operative.
git checkout master
5. Pull latest commits of branch you are planning to rebase onto
Pull the latest commits of the branch you lot are planning to rebase onto from the remote repository. This footstep is very important as it will utilise the terminal commit from that branch to fix a new base of operations commit of the feature branch.
git pull
6. Checkout feature branch
Afterwards getting the latest commits of the branch you are rebasing onto, you are going to switch dorsum to the feature branch
vii. Start rebase
Offset to rebase in the feature co-operative past using the proper name of the branch you are rebasing onto.
git rebase principal eight. Fix conflicts during rebase or skip to proceed rebase, or abort the rebase (optional)
At that place tin can exist conflicts that need to be resolved during a rebase. If you lot run into conflicts, the last volition display a message starting with the give-and-take Conflict Merge conflict in followed by the path of the file with conflicts.
Open the file and resolve conflicts. Once conflicts are resolved, brand certain to stage the file.
git add ./path/of/file/with/conflicts Verify the conflicted files are staged using the status command.
git condition Later on that, brand certain to to non commit those changes. Instead, you want to continue the rebase as the rebase hasn't finished yet.
git rebase --proceed In the case you don't desire to bypass the commit that caused the disharmonize, you lot tin can skip it.
git rebase --skip Finally, yous can always opt to
ten. Once you lot finish rebasing, Exercise Not pull, but push button immediately to remote
Y'all volition meet the rebase is completed because the terminal will throw the following message:
Successfully rebased and updated
At that bespeak, the feature branch in your local repository is rebased. Hence, the base commit of the feature branch is updated. Notwithstanding, the feature branch in the remote repository still doesn't have the base commit updated.
If you decide to pull the latest commits from the remote repository, it will cause conflicts, every bit the base commits from both repositories, are not the same. Therefore, nosotros need to force to push local repository commits to remote.
It is highly recommended to use --force-with-lease than to use --force when pushing to remote afterwards a rebase as --force overwrites the remote branch with a local branch. While in theory --forcefulness-with-charter has a similar outcome, it is a safer option as information technology won't overwrite whatsoever commits on the remote co-operative that someone else might have added while you were rebasing. This volition save headaches in example you overwrite someone else'southward code and have to find out what was overwritten.
git push --force-with-lease *In example the push --strength-with-lease fails, my recommendation is to do a hard reset of the feature branch in the local repository to lucifer the remote repository. Then, starting time the rebase all over over again.
Reasons why you lot should rebase more than often
Adopting the usage of the rebase command tin can have positive effects when done correctly. Here is a list of some of the benefits when you and your team rebase more often
- Information technology allows to maintain a linear project history
- Rebase allows for safer/simpler merges
- Forbid the possibilities programmatic test from declining afterward merging
- It reduces merge conflicts
- Rebase helps to keep up to appointment commits from on another branch as you piece of work in your local feature co-operative
The Dangers of Rebasing
Only like any other tool, there tin can be dangers of using information technology when not washed correctly, and rebasing is not the exception. Here are some problems you tin can face when using rebase.
- Rebase can rewrite the commit history equally yous can't update remote using only
push button, just instead y'all have to force push - At that place could be a lot of merge conflicts during a rebase if a branch is non rebased often
Conclusion
All in all, rebase is a powerful tool not many developers are enlightened of or not sure how to utilise it as it helps to go on commits up to date in relation to another branch, reducing and fifty-fifty removing the existence of merge conflicts. Even so, using the rebase control can have negative effects if the developer is not sure of how to employ it every bit reverse to merging, rebase doesn't preserve the commit history.
Interested in Learning more almost Git?
I wrote other articles explaining how to use other git commands, and I thought you might exist interested in reading some of them since you are reading this.
- The Complete Guide to Git Allonym: Shorcuts to Exist Efficient
- How to Revert to Last Commit Locally and Remote in Git
- What is Git HEAD? A Practical Guide Explained with Examples
- Acquire How to Employ Version Control with Git and GitHub: The Absolute Guide for Beginners
- How to Update GitHub Personal Admission Tokens?
Did you like this commodity?
Share your thoughts by replying on Twitter of Become A Ameliorate Programmer or to personal my Twitter account.
— Become A Ameliorate Programmer (@bbprogrammer) December 22, 2021Have you ever used git rebase before? Do you know what it does?
git rebase helps y'all keep your branch up to date in relation to other branches without the demand of merging.
Interested in learning more?https://t.co/jL56nYfBJo
Source: https://www.becomebetterprogrammer.com/git-rebase/
0 Response to "Specify Which Branch You Want to Rebase Agains"
Post a Comment