17

I have, what I assume, is a typical workflow.

Our project works with pull requests.

To develop new feature I create a dev. branch. By the time I am finished with the feature some changes were made in master so I want to get those changes into my branch so I make pull request.

From what I've read on the internet there are two options for that:

  1. merge
  2. rebase

However, I tried both of them but when I make pull request it shows that all files were changed in this pr.

Here is what I did:

on the branch

-- git commit -a -m "changes i made on my branch" 
-- git checkout master
-- git fetch upstream
-- git merge upstream/master
-- git checkout mybranch
-- git merge master (or rebase)
-- git push origin mybranch

result -- merge commit in the history shows files changes: 90

What is the correct way to get updates from master into my branch?

Similar situation happens when somebody reviews my pr and I need to update my pr. Once again, I end up needing the changes from master.

Thanks for the help.

Prad
  • 515
  • 7
  • 15
rigby
  • 1,280
  • 3
  • 13
  • 21
  • Possible duplicate of [When do you use git rebase instead of git merge?](http://stackoverflow.com/questions/804115/when-do-you-use-git-rebase-instead-of-git-merge) – Joe Oct 26 '16 at 09:44
  • 1
    @Joe it's not when to use this or that. in both cases i get the same result – rigby Oct 26 '16 at 09:46
  • 1
    If you get tons of files changed when in fact you only edited a few files, check the diffs. If it automatically changed tabs to spaces then you might want to look into your line ending settings. – miva2 Oct 26 '16 at 10:06
  • 1
    @miva2 nope, it actually shows some changes that were made by other people as mine – rigby Oct 26 '16 at 10:15
  • 1
    This is problem for me too. I'm beginner in Git and still grokking all git commands. Sadly, I haven't found a simple solution just to push "my" changes only and not 100 other files committed by others in master. – Harish Feb 20 '19 at 18:14

2 Answers2

33

You can pull changes from master to your branch with:

git checkout my_branch    # move on your branch (make sure it exists)
git fetch origin          # fetch all changes
git pull origin master    # pull changes from the origin remote, master branch and merge them into my_branch
git push origin my_branch # push my_branch

Please note, based on new changes to branch default name on some git repository providers you can have a master branch named main

Matteo Gaggiano
  • 1,254
  • 15
  • 28
3

-- git checkout mybranch

-- git merge master (or rebase)

Till this it is correct

After this you are directly pushing to your branch, before this just add and commit like this.

-- git add .

-- git commit -m "msg after merging"

-- git push origin mybranch

This will merge Master Branch Code with your branch (i.e. mybranch) & will push the code to origin

Community
  • 1
  • 1
Rohit shah
  • 833
  • 4
  • 15
  • 1
    i commit all my changes for my feature before switching to master to check for changes there. but i tried what you suggested and still get the same problem: Showing 90 changed files what i did: ... -- git checkout mybranch -- git merge master (no conflicts) -- git add . -- git commit -m "msg after merging" (git says nothing to commit) -- git push origin mybranch result: 90 changed files – rigby Oct 26 '16 at 09:57
  • what is the final result you want ?@rigby – Rohit shah Oct 26 '16 at 10:00
  • 1
    i want git to show that only files that i actually changed as "changed". for now it shows that ALL (90) files were changed. – rigby Oct 26 '16 at 10:09
  • This post might help you. http://stackoverflow.com/questions/4873976/how-to-commit-only-modified-and-not-new-or-deleted-files – Rohit shah Oct 26 '16 at 10:18