2

Normally, while working on a development project, we make a feature branch from the master and commit our changes to our feature branch and raise a pull request to the master branch.

If the development work is supposed to span for longer time, how to prevent the feature branch from going out of date from the master branch?

How to prevent conflicts while taking pull from the master in such a case?

Rachit
  • 403
  • 10
  • 32

2 Answers2

3

Let's say you are working on your feature branch which keeps track of the remote master branch. If your team member adds a new commit to the remote master branch and you want to add that commit to your feature branch then you need to follow the following steps in order to have your feature branch up to date.

Step 1: Check if there is a new commit to the master.

git fetch

or

git fetch -p

The fetch command will give you information about the changes(like new branch has added and many more) in the remotes repository. -p will give you the same thing except it will give the information about the remote branches that have been deleted by the team member for any good reason. Always remember that fetch will not do any changes in the local repository. It only displays the information about the changes made in the remote repository.

Step 2: Check your feature branch status (not necessarily)

git status

It will show you how many commits you are behind/after the master.

Step 3: If your feature branch is behind one or more commits then Rebase the remote master branch into your feature branch

git rebase origin/master

You can also do it like this

git rebase

As you are keeping track of remote master branch so it will directly rebase the origin/master. But I'll recommend you to use the first approach by explicity saying that I need to rebase the origin/master to my current local repository.

In my opinion, this is the best way to make your feature branch up to date.

Never do merge origin/master to your feature branch git merge in your local repository because it will add an extra merge commit in your local repository then your feature branch will have two commits in local one is your feature branch commit and another one is merge commit. So, when you push that same branch to the remote, it will add two commits rather than one commit. Generally, this will not give a clean history of the commits.

Subrato Pattanaik
  • 5,331
  • 6
  • 21
  • 52
2

If you have commits on the feature branch and your team happened to commit some stuff on the master branch as well, then you have no choice but to pull the latest changes to master and then from the feature branch you can either:

git merge master

or

git rebase master -i

If you have no experience with rebase, then I would recommend to to the first and merge the master into your feature branch. That will integrate all the changes from master. It will also create an extra merge commit on your feature branch.