Git Rebase
Updated at Dec 7, 2021
A rebase takes all of your commits from one branch and moves them on top of another branch commits. What you get with this approach is a nice clean straight line graph with all of your commits laid out nicely in a row.
It makes it really easy to trace even if you have many people working on the same project at the same time.
Step 1: (main) git pull
Step 2: git checkout -b feature_branch
Step 3: git checkout main
Step 4: git pull
Step 5: git checkout feature_branch
Step 6: git rebase main
Step 1
When you first start development, make sure you always sync the remote main. And make sure you have all the changes that your teammates have been doing on your local machine. So you want to run the git pull and make sure you get up-to-date.
Step 2
When you start developing a feature, you always want to check out a new branch so that all your commits can live on this branch without disrupting your main branch. Do a git checkout -b feature_branch.
Step 3
As you’re making commits on your feature on your local machine, another developer is shipping features to your remote main branch. This means your development environment is out of sync with the remote main branch.
Check out your local main. Run a git checkout main.
Step 4
Do a git pull. This will make sure your local main is up-to-date with the remote main and any of the changes that your co-workers may have pushed in the meantime.
Step 5
Now that you have all of your co-worker's changes on your local main, you want to ensure that your feature branch will drive with those changes. Check out the feature branch. Run a git checkout feature_branch.
Step6
Do a git rebase main. What this will do is it will re-anchor your feature branch against the latest changes. At this point, git will let you know if you have any conflicts. If you do, you can take care of them. Then, commit all your code to the feature branch