In this post I will discuss how to rebase feature branch with master branch in Git repository. You might have seen post on Git tutorial using command line tool or CLI (Command Line Interface) about how to commit and push code into the Git repository.
I will explain here how to use command line tool to rebase feature branch with master branch in Git repository. You can also use any graphical user interface, for example, GitHub, GitLab, Source Tree, Tortoise Git, etc. to do the same operation. Here I am going to explain how to rebase only using command line tool (cmd).
Rebase And Merge Git Branch
First let me tell you some features about rebase and merge commands in Git:
- In Git, the
rebasecommand integrates changes from one branch into another. It is an alternative to the
mergecommand. But there is a difference between
- Rebase is one of the two Git utilities that specializes in integrating changes from one branch to another. The other change utility is Git merge. Merge is always a forward moving change record. Alternatively, rebase has powerful history rewriting features.
- Rebase is generally performed when you need straight, linear or clean history of commits.
- Merge does not obliterate your commits. Merge preserves history. Rebase rewrites history, which is a Bad Thing after you’ve pushed it.
- Use merge instead of rebase whenever you have already pushed.
When Do You Need Merge?
The following points may give you some idea when you need to perform merge command on two different branches.
- When you have pushed the branch or others team members are working on it too.
- You do not need the full history of activities, i.e., commits.
When Do You Need Rebase?
The following points may give you idea about when you need to use the rebase command for two different branches.
- When you have not pushed the branch or no one else is working on it.
- You want the full history of activities, i.e., commits.
- You want to avoid all the auto-generated merged commit messages.
Git Rebase using CMD or CLI
Here are, in general, few steps which will tell you how to perform rebase using command line tool.
- Navigate to the root directory of your project where you want to perform rebase.
- Execute command
git fetch && git rebase origin/master. In this case, you have navigated to the feature branch using command line tool to execute the command.
- If conflict occurs, manually resolve them in each file before executing the next command.
- Execute command
git rebase --continueto continue rebase. You will also get the similar message on console window to perform the same thing.
- If conflicts occur again follow steps 3 & 4, otherwise execute
git pullfrom your feature branch if there is any difference.
- Commit the code to repository for feature branch.
That’s all about how to perform rebase using command line tool in Git repository.