- Two Main ways to use git rebase command:
- as an alternative to merging
- as a cleanup tool
Alternative to Merging:
- When master branch is one commit ahead of feature branch and you want to get the latest work from master into feature you will do a merge commit. If this is a big project your repo will be cluttered by many merge commits.
- When you do a rebase all your commits will be added to the tip of master branch eliminating all the merge commits.
- git rebase master : when you specify master you are rebasing onto master.
- Never rebase commits that have been shared with others. If you have already pushed commits upto Github.. Do not rebase them unless you are positive on one on the team is using those commits.
- When you run into conflicts —> open the file and resolve the conflict and use stage the file and use git rebase —continue . You can abort using git rebase —abort to abort the process.
As a Cleanup Tool:
- Running git rebase with ~i option will enter the interactive mode which allows us to edit , commit , drop commits ans also add files etc.
- git rebase -i HEAD~4
- When you run the command it shows some commands that are used mostly:
- pick - use the commit
- reword - use the commit, but edit the commit message.
- edit - use commit, but stop for amending
- drop - remove commit.
- fixup - use commit contents but meld it into previous commit and discard the commit message.