How to Rebase Git Branch
There are several git rebase flows. In this snippet, we are going to show you how to rebase your branch. Here are the steps to follow:
1. Run git fetch¶
You should receive the latest changes from a remote git server. Thus the first step is running git fetch:
2. Run git rebase¶
The second step is running git rebase. Rebase is a Git command which is used to integrate changes from one branch into another. The following command rebase the current branch from master (or choose any other branch like develop):
git rebase origin/master
In case of git rebase conflict resolve them, add your changes by running git add, and then continue like this:
git rebase --continue
Run git rebase --abort to cancel the rebase.
3. Run git push¶
The final step is git push (forced). This command uploads local repository content to a remote repository. See the code below to run it:
git push origin HEAD -f
Rebasing vs Merging¶
Choosing between git rebase and git merge remains one of the most discussed topics in the community. Some may say that you should always use merging, some may say that rebasing is a more correct way to do things. There is no right or wrong way of using these two commands. It mainly depends on the user and the workflow.
Rebasing and merging are both used to integrate changes from one branch into another differently. They are both used in different scenarios. If you need to update a feature branch, always choose to rebase for maintaining the branch history clean. It keeps the commit history out of the branch, contrary to the git merge command, which is its alternative. While, for individuals, rebasing is not recommended in the case of feature branch because when you share it with other developers, the process may create conflicting repositories. Once you need to put the branch changes into master, use merging. If you use merging too liberally, it will mess up git log and make difficult to understand the history. Merging preserves history whereas rebasing rewrites it. If you need to see the history absolutely the same as it happened, then use merge.
Force push to update pull request¶
Running git push after a rebase will fail as the direct path from the commit on the server to the commit on your branch is missing. You should use the -f or --force option to instruct Git to force push. It is recommended to set the push.default config to simple, which is the default in Git 2.0 version.
Run the following to make sure the config is correct:
git config --global push.default simple
If it is correct, run:
git push -f
After doing all this your pull request will be updated.
The git fetch command downloads commits, files, and refs from a remote repository into the local repository. It updates your remote-tracking branches.
The git fetch command allows you to see the progress of the central history, not forcing you to merge the changes into your repository. It does not affect your local work process.
Do not confuse fetching with the pulling as here it will merge the changes with your branch.
The git pull command is used to fetch and download content from a remote repository and update the local repository to match that content.
Git Force Push¶
The git push command is the opposite of git fetch, as fetching imports commits to local branches, pushing exports them to remote branches. Use -f to force the push.
The difference between git force push and usual push is that you only force a push when you need to replace the remote history by your local history.