Branching and Rebasing explained in easy steps
This is a summary or a quick look at Git branching and rebasing. To learn Git Branching, I concluded the following things.
What is a branch?
To keep things more modular and isolated we use branching with Git. For example, a feature can be developed in isolation in a branch and then it can be merged to the master branch later.
This diagram depicts a master branch and another iss53 branch.
How to create a new branch?
git branch mynewbranch
How to switch between branches?
git checkout mynewbranch
How to see all the local branches?
How to see all the local and remote branches?
git branch -a
How to merge any branch to the master?
1. Switch to the master branch
git checkout master
2. Merge the feature branch to the master branch
git merge mynewbranch
How to create a remote branch?
You cannot directly create a remote branch. You need to create a local branch and then you can push your local branch to the remote.
How to push your local branch to remote?
1. Switch to the branch
git checkout your_branch
2. Push the branch to remote
git push -u origin your_branch
How to switch to a remote branch?
Consider A created a new branch and pushed it to remote. And now B wants to work on the same branch. B follows these steps on some other computer.
1. Fetch the remote branches
2. Switch to the remote branch
git checkout origin/your_branch
You must read this StackOverflow answer to collaborate in a branch
What problems can arise when merging any branch to the master branch?
When you create a new branch and make commits to it, simultaneously, other developers might have made commits to the master branch and while merging, you may get conflicts. These conflicts will arise in the master branch which is a bad practice. Here comes branch rebase!
What is branch rebase?
Branch rebase is similar to merge but instead of merging to the main branch, rebase can pull the new commits to your feature branch from the master branch.
How rebase can help you with the problems?
Rebase can keep the master branch clean. With rebase, you can resolve the conflicts in the feature branch itself. Here is a more real world example of how the rebase can be performed with merge.
Once you watch this video, to summarize we can say, to rebase, we follow these steps
1. Switch to your master branch
$ git checkout master
2. On the master branch, bring the new changes from remote to your copy of master
$ git pull --rebase origin master
3. Switch to your feature branch
$ git checkout mynewbranch
4. Then bring the changes from master branch (rebase) to your feature branch
$ git rebase master
Important Rebase does not merge the feature branch to the master branch. Once you are ready with the feature branch. You perform a rebase and then merge the feature branch to the master branch as explained above in the step “how-to-merge”
What are the best practices of branching and deployment?
- Never spoil your master branch, rather rebase from master to your feature branch
- Merge the branch and if the feature development is completed, you can delete the branch
- To fix the production for some hotfixes, create a new hotfix branch, resolve the problem and merge to the production.
- You should have the following branches
- Feature branches
- Development branch
- Release branch
- Hotfixes branches
- Master branch