Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get changes from master branch to local branch?

Tags:

git

git-merge

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.

like image 398
rigby Avatar asked Oct 26 '16 09:10

rigby


2 Answers

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

like image 158
Matteo Gaggiano Avatar answered Oct 15 '22 02:10

Matteo Gaggiano


-- 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

like image 36
Rohit shah Avatar answered Oct 15 '22 02:10

Rohit shah