Anthony
Anthony

Reputation: 35928

How to get changes from another branch

I am currently working on featurex branch. Our master branch is named our-team. Since I started working on featurex, more changes have been made to branch our-team.

I have done this locally to get all the latest changes from our-team:

git checkout our-team
git pull

Before I push featurex for merging, I would locally like to get all changes from our-team branch into featurex so that I can ensure everything works as expected.

How can I do that?

Upvotes: 275

Views: 520907

Answers (6)

heisenberg
heisenberg

Reputation: 912

git fetch origin our-team

or

git pull origin our-team

But first you should make sure that you are already on the branch you want to update (featurex).

Upvotes: 73

CyberFoxar
CyberFoxar

Reputation: 569

There are 2 options, either merging or rebasing your branch. Both work differently, but have similar outcomes.

The accepted answer is a rebase. This will take all the commits done to our-team and then apply the commits done to featurex, prompting you to merge them as needed.

One bit caveat of rebasing is that you lose/rewrite your branch history, essentially telling git that your branch did not begin at commit 123abc but at commit 456cde. This will cause problems for other people working on the branch, and some remote tools will complain about it. If you are sure about what you are doing though, that's what the --force flag is for.

What other posters are suggesting is a merge. This will take the featurex branch, with whatever state it has and try to merge it with the current state of our-team, prompting you to do one, big, merge commit and fix all the merge errors before pushing to our-team. The difference is that you are applying your featurex commits before the our-team new commits and then fixing the differences. You also do not rewrite history, instead adding one commit to it instead of rewriting those that came before.

Both options are valid and can work in tandem. What is usually (by that I mean, if you are using widespread tools and methodology such as git-flow) done for a feature branch is to merge it into the main branch, often going through a merge-request, and solve all the conflicts that arise into one (or multiple) merge commits.

Rebasing is an interesting option, that may help you fix your branch before eventually going through a merge, and ease the pain of having to do one big merge commit.

Upvotes: 44

adrianbx
adrianbx

Reputation: 1

incase someone stumbles upon this asking themselves how to pull in changes without using git pull - git pull will first run git fetch then git merge as it makes up the combination of two sequences i.e. fetch + merge. For those that need to just pull changes from another branch without doing so:

git checkout <branch with changes wanted> 
git checkout -b <new branch> 

Upvotes: -4

JulCh
JulCh

Reputation: 2878

You can use rebase, for instance, git rebase our-team when you are on your branch featurex.

It will move the start point of the branch at the end of your our-team branch, merging all changes in your featurex branch.

Upvotes: 133

Jad Chahine
Jad Chahine

Reputation: 7149

Before following these instructions keep in mind that featurex is the branch where changes are being merged and pushed

  1. go to your branch featurex

    git checkout featurex
    
  2. merge the changes of our-team branch into featurex branch

    git merge our-team
    

    or

    git cherry-pick {commit-hash}
    

    if you want to merge specific commits.

Note: probably you will have to fix conflicts after merging our-team branch into featurex branch before pushing.

Upvotes: 265

Pierre Fouilloux
Pierre Fouilloux

Reputation: 239

You are almost there :)

All that is left is to

git checkout featurex
git merge our-team

This will merge our-team into featurex.

The above assumes you have already committed/stashed your changes in featurex, if that is not the case you will need to do this first.

Upvotes: 20

Related Questions