The other way is using the rebase that I’m going to show you now. Step 1: Fork and clone the desired repo Step 2: Set upstream Step 3: Create a branch from the dev branch of the upstream Step 4: Rebase your branch with the. There are a few ways to update the current branch, one of them is using git merge and merging the most updated branch, in this case main, into the branch you want to update in this case task-2. Knowing that main is more up-to-date than your branch task-2 and following the good practice of always working with the most up-to-date project, you decide it’s time to update your working branch, which has the current history looking like this: I am making an analogue of Git Pull -rebase using the Libgit2 library, the code is made without errors, but only I have Head in front of Master. Now the git tree looks like this: One clean stream of. Firstly let’s checkout on to the feature branch, then right click on the master branch and select rebase current changes onto master. What git pull -rebase does, in this case, is: git fetch origin git rebase -onto origin/foo e foo. Even a git fetch git rebase origin/foo would not cut it, because commits b and c on one side, and commit b+c on the other, would conflict. So far the git tree looks like this: Let’s now rebase the feature branch on to master. A git pull at this point would result in chaos. Similarly, the history graph should look like the image below: Now let’s checkout the master branch and commit also a change.
Suppose you have a project and you were working on a task on the branch task-2, meanwhile someone else was working on the branch task-1 that was merged into main and thus making the branch main more up-to-date as shown in the drawing below:
In this pro tip I will show you how to use git rebase to update a branch. The goal of the rebase is to have a cleaner history if I have local changes or commits on the repo. Warning: There is a git rebase onto switch which sometimes developers incorrectly believe they need to include with the rebase command. Each time I want to update, from my local master branch: git fetch upstream git rebase upstream/master. To rebase develop to master the command is as follows: git rebase master develop. The command git rebase can be used to make various history adjustments, from rewriting the commit tree (and by doing that rewriting the history) to even pushing commits forward as if the branch were created in the future. Simply append to the end of the command the name of the source branch and then the name of the branch to rebase.