Home > Blockchain >  I want to update my branch with the new files/code a team member added to their branch; do I use Mer
I want to update my branch with the new files/code a team member added to their branch; do I use Mer

Time:10-07

(Using git through IntelliJ, not command line)

I'm newish to git and I've been reading up on it and learning as much as I can, but I cannot seem to find a clear cut answer to this question.

I'm working in a team where each member has their own branch under origin (I'll call them FredBranch, JoeBranch, and MindyBranch - I'll call my branch MyBranch for simplicity). Fred did a bunch of work and added all kinds of files to the project under his branch. Now I'm going to do some work but I want those files in my branch so I'm working with the newest version of the project. Fred said he pushed the changes onto his branch.

If I right-click FredBranch under my Remote branches section, it gives me the following options:

  • Rebase 'MyBranch' onto origin/FredBranch
  • Merge origin/FredBranch into 'MyBranch'
  • Pull into 'MyBranch' Using Rebase
  • Pull into 'MyBranch' Using Merge

I can't seem to wrap my head around the differences, or at least how they apply to this specific situation haha. What would be the correct thing to do here?

CodePudding user response:

Rebasing makes your branch contain all the commits from the other branch while also putting your commits specific in your own branch on top of the branch you're rebasing onto. The git history becomes one as if you started your work from the rebased branch, that's why the word "rebase", basing your work again.

Merging achieves the same thing as rebasing but it creates a merge commit while keeping both branches' history.

Pulling is fetch merge. So, you refresh your origin, and then merge a remote branch onto your local branch.


Now, let's take an example. Let's assume:

MyBranch contains:

My Commit A
develop(common branch)'s lastest commit

FredBranch contains:

Fred Commit A
develop(common branch)'s lastest commit

If you rebase your branch onto FredBranch, the new git tree will be:

My Commit A
Fred Commit A
develop(common branch)'s lastest commit

If you merge FredBranch into your branch:

Merge FredBranch into HEAD (some auto-generated commit message)
|                   \
|                   |
My Commit A     Fred Commit A
develop(common branch)'s lastest commit

Pull is basically the same thing as merge, so creates the same git tree.


Re: 4 options,

Rebase 'MyBranch' onto origin/FredBranch: this simply means you're rebasing your MyBranch branch onto the remote FredBranch.

Merge origin/FredBranch into 'MyBranch': merging the remote FredBranch into your local MyBranch branch.

Pull into 'MyBranch' Using Rebase: this is basically a rebase (same as #1). The only difference is it first fetches the remote (i.e. refreshes it).

Pull into 'MyBranch' Using Merge: This is pull that we discussed in the first section of this answer. Same as #2 but as you may know by now, this also fetches the remote.


Now, back to your question. So, which is the best option?

This is a bit opinion-based so I won't tell you what I think is the best option. I'll just share what are the options and most common practices.

Usually, rebasing is done when you're working with a common branch (develop, main, etc), and also it involves trickier conflict resolution process, so it's not common to do it between two working branches. But if you want, you can do it with this option: Rebase 'MyBranch' onto origin/FredBranch.

The most common practice for merging two WIP feature branches is merging. You have two options of merging:

  • Using Merge origin/FredBranch into 'MyBranch' option, i.e. merging the remote branch directly into your local working branch.
  • Create a local copy of FredBranch and then merging the local FredBranch into your working branch.

Pull is not a new concept. It confuses people and I don't love the term. You can always think of pull as a merge. It's basically the same thing. Pull just fetches the remote, and then merge a certain branch onto your local branch. So, my advice is always fetch whenever possible, don't use pull but instead, use merge or reset HEAD.

  •  Tags:  
  • git
  • Related