Rebasing in Git

Now you have just studies about sending a pull request and merging the pull request, if changes made in the local development appears to be a correct code.

But wait, the new code has been merged to the master branch of remote repository, but our local master branch is still off and still has only the skeleton code.
The local master branch still is not updated with the latest changes at the remote, wherein new feature has been added from branch A – the local development branch, which added a new feature of heading tag.

To update the local branch with the origin, we need to fetch all the commits from the origin, and merge it with the local master branch.

To do so, we can run

$ git fetch origin

The above command will bring up all the commits, and if there are new branch that has been created at the remote repository.
followed by

$ git merge.

The above command actually merges the changes in the remote repository to the local repository, if master has the heading tag, the local also will have it now.

But wait, there is a simple command that can carry out both the task altogether.
If you run,

$ git pull

It will fetch the latest commit from the origin, the code changes and also it will merge the change together.

As you can see, when we do a git pull, we can see the content of index.html seems to be changed, because we added a heading into it.

Hence, our local master branch is now even with the remote master branch.

Now we can checkout into a new development branch, if we want say branch “B”, to say add a paragraph text suppose, add commit those changes and send for a pull request again to master branch.

Pulling latest changes in GitHub Desktop

The command which we just tried, can be executed from GitHub Desktop via UI.

If you go to the “Repository Menu” , you will be able to see the “Pull” options in the dropdown which carries out the same task.

The “Fetch origin”command is available just below the menu bar on the right, which would perform same action, and the “merge”option is available in the “Branch”menu.

Pulling latest changes in SourceTree

In case of SourceTree, the Repository Menu has inclusion of each command out there.
The Pull command will do the same action as the command line option git pull would do, and git fetch and git merge works well with “Fetch”and “Merge” options in the menu.

Conclusion
In this very tutorial we learned how to rebase or forward-fill our local branch with the remote repository if our remote branch is ahead of our local repository. In the subsequent tutorial we will learn how to squash merge different commits into a single.

Translate »