Git – Refresh Feature Branch from Master – Rebase Vs. Merge

1. Context

It’s a good practice to work with branches instead of directly working with the master or main. For every feature and bug you work on, create a distinct branch to isolate your work. Feature branches are supposed to be short-lived. Once your code is merged with the master, delete it. However, while working on a feature branch, often you need to get the latest changes from master (contributed by other developers) to your feature branch to test your code with changes made by other developers.

There are two ways to refresh your feature branch – merge and rebase. In this article, we’ll explore both options.

2. Working with Feature Branch

Create a feature branch, make some changes, and push to the branch.

# Create a feature branch
git checkout -b <feature-branch>

# Make some changes and stage
git add <files>

# Commit your changes
git commit -m "Some message"

# Submit your changes to the feature branch
git push origin <feature-branch>

Now, the master and the feature branch diverged.



Master and Feature branches diverged. Courtesy: www.atlassian.com

3. Rebase

After Rebase operation. Courtesy: www.atlassian.com

Rebase moves all diverging commits of the feature branch to the top by creating new hashes for the commits. The commit history is re-written, which makes it linear and clean. Because of this, subsequent code push to the feature branch has to be forced using --force option.

In interactive mode (rebase -i) however, we can re-organize the commit history as desired.

Steps for rebasing:



# Switch to master and get latest files
git checkout master
git pull

# Switch to your branch and rebase to get changes
git checkout <feature-branch>
git rebase master

# Resolve merge conflicts and push to your branch
# Use force flag if you have already pushed changes to the branch before
git push origin <feature-branch> --force

4. Merge

After Merge operation. Courtesy: www.atlassian.com

Merge applies all commits on top of the feature branch and creates a new merge commit in the feature branch, keeping the feature branch’s commit history intact. It means the feature branch will have an extra merge commit every time we perform the merge operation.

Steps for merging:

# Switch to master and get latest files
git checkout master
git pull

# Switch to your branch and merge to get changes
git checkout <feature-branch>
git merge master

# Resolve merge conflicts and push to your branch
git push origin <feature-branch>

5. Recommendation

Since rebase re-writes the history, it is hard to understand when changes from the master were incorporated into the feature branch. For public repositories, rebase is not recommended at all. On the other hand, merge creates an extra commit. The merge commit helps to track when the changes were incorporated and it keeps the history intact. Therefore, in order to refresh your feature branch from the master merge is recommended.

Note that the same recommendation applies to refresh any branch from any other branch, not only while refreshing the feature branch from the master.

Git – Refresh Feature Branch from Master – Rebase Vs. Merge
Be The First

Join our list to get instant access to new articles and weekly newsletter.

Tagged on:
0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
()
x