Matthew Colley
Matthew Colley

Reputation: 11436

How to "git pull" from master into the development branch

I have a branch called "dmgr2" in development, and I want to pull from the master branch (live site) and incorporate all the changes into my development branch. Is there a better way to do this?

Here is what I had planned on doing, after committing changes:

git checkout dmgr2
git pull origin master

This should pull the live changes into my development branch, or do I have this wrong?

Upvotes: 913

Views: 2020585

Answers (10)

Manish Singh Chouhan
Manish Singh Chouhan

Reputation: 381

The best way to doing this is

first go to master/origin

git checkout master/origin

First run " git branch temp"
then "git checkout temp"
& then "git merge development"

if any conflicts come, please resolve & commit 
and then 
1. git checkout development
2. git merge temp

After doing this all run to check

git branch 

if the result is development

if not then run

git checkout development

now you are on development branch

and you can run

git merge temp

Now you can get your master branch code into development.

Upvotes: 0

DR-MATTH
DR-MATTH

Reputation: 251

If dev is clean and just want to update with main or master changes

  • git checkout dev
  • git pull origin <main or master>
  • git push origin dev

Upvotes: 17

Dr-Bracket
Dr-Bracket

Reputation: 5484

If you're on feature-1 branch and you want to pull master -- (maybe to get the latest merged updates/reduce the chance of a merge conflicts), do:

git pull
git merge origin/master

Pulls master into your branch - Does not affect master!

This will pull anything that has gone into master into your branch since the two of you diverged.

It is fine to do this if your branch has already been made public, as it does not rewrite history.

Upvotes: 37

greenridinghood
greenridinghood

Reputation: 679

Working in my local branch, I love to keep-up updates in the development branch named dev.

Usually, I prefer to use:

git fetch
git rebase origin/dev

Upvotes: 50

Pratik Gaurav
Pratik Gaurav

Reputation: 907

This worked for to get the latest code from master to my branch:

git rebase origin/master

Upvotes: 38

GM1
GM1

Reputation: 491

I have master updating and my branch updating, I want my branch to keep track of master with rebasing, to keep all history tracked properly. Let's call my branch Mybranch:

git checkout master
git pull --rebase
git checkout Mybranch
git rebase master
git push -f origin Mybranch

I need to resolve all conflicts with git mergetool, git rebase --continue, git rebase --skip and git add -u, according to the situation and git hints, until everything is solved.

Note: correction to last stage, in courtesy of Tzachi Cohen, using "-f" forces git to "update history" at server.

Now, the branch should be aligned with master and rebased, also with the remote updated, so at git log there are no "behind" or "ahead", and I just need to remove all local conflict *.orig files to keep the folder "clean".

Upvotes: 15

Mbs Yaswanth
Mbs Yaswanth

Reputation: 150

You might want to use this if your histories doesn't match and want to merge it anyway:

git pull origin master --allow-unrelated-histories

See "The “fatal: refusing to merge unrelated histories” Git error" for more information.

Upvotes: 6

keivan kashani
keivan kashani

Reputation: 1349

This is my solution:

git checkout mybranch
git rebase master mybranch
git add .
git rebase --continue
git commit -a -m "test"
git pull
git push

Use git add . to stage your files.

Here is another solution for getting your master changes to your branch:

git checkout mybranch
git fetch origin
git merge origin/master

Your history of git is clear when you use rebase, but it is easier to use merge origin/master.

Upvotes: 1

gajanan_done
gajanan_done

Reputation: 11

  1. First, fetch the code using:

    git fetch
    
  2. Then use:

    git rebase origin/dev
    

Upvotes: 1

torek
torek

Reputation: 487725

The steps you listed will work, but there's a longer way that gives you more options:

git checkout dmgr2      # gets you "on branch dmgr2"
git fetch origin        # gets you up to date with origin
git merge origin/master

The fetch command can be done at any point before the merge, i.e., you can swap the order of the fetch and the checkout, because fetch just goes over to the named remote (origin) and says to it: "gimme everything you have that I don't", i.e., all commits on all branches. They get copied to your repository, but named origin/branch for any branch named branch on the remote.

At this point you can use any viewer (git log, gitk, etc) to see "what they have" that you don't, and vice versa. Sometimes this is only useful for Warm Fuzzy Feelings ("ah, yes, that is in fact what I want") and sometimes it is useful for changing strategies entirely ("whoa, I don't want THAT stuff yet").

Finally, the merge command takes the given commit, which you can name as origin/master, and does whatever it takes to bring in that commit and its ancestors, to whatever branch you are on when you run the merge. You can insert --no-ff or --ff-only to prevent a fast-forward, or merge only if the result is a fast-forward, if you like.

When you use the sequence:

git checkout dmgr2
git pull origin master

the pull command instructs git to run git fetch, and then the moral equivalent of git merge origin/master. So this is almost the same as doing the two steps by hand, but there are some subtle differences that probably are not too concerning to you. (In particular the fetch step run by pull brings over only origin/master, and it does not update the ref in your repo:1 any new commits winds up referred-to only by the special FETCH_HEAD reference.)

If you use the more-explicit git fetch origin (then optionally look around) and then git merge origin/master sequence, you can also bring your own local master up to date with the remote, with only one fetch run across the network:

git fetch origin
git checkout master
git merge --ff-only origin/master
git checkout dmgr2
git merge --no-ff origin/master

for instance.


1This second part has been changed—I say "fixed"—in git 1.8.4, which now updates "remote branch" references opportunistically. (It was, as the release notes say, a deliberate design decision to skip the update, but it turns out that more people prefer that git update it. If you want the old remote-branch SHA-1, it defaults to being saved in, and thus recoverable from, the reflog. This also enables a new git 1.9/2.0 feature for finding upstream rebases.)

Upvotes: 1275

Related Questions