1

After the holiday I would like to continue working on my open ticket. I already started working on a feature ("feature/my-new-feature") locally before the holiday. After one week my colleagues have already merged their features into the master. Since I want to avoid later merge conflicts, I want to update my feature branch. However, I am unsure which update strategy to use. Two approaches are going through my mind.

1.

git checkout master
git pull
git checkout feature/my-new-feature
git merge master

2.

git pull origin/master

Probably both strategies work. Are there advantages and disadvantages to both ways that should be considered?

phd
  • 82,685
  • 13
  • 120
  • 165
MartinTTS
  • 111
  • 8

2 Answers2

3

I usually rebase on master, i.e. replay my commits on top of the master:

  1. add and commit everything in the feature branch,
    git add ...
    git commit -m "..."
    
  2. update the local master,
    git checkout master
    git pull
    
  3. rebase the feature branch on the master
    git checkout feature/my-new-feature
    git rebase master
    

What you are proposing in 1. is updating your master and merging it to your branch. It is not wrong at all but it can create messy history with merge commits. You can read about the difference e.g. here: https://blog.git-init.com/differences-between-git-merge-and-rebase-and-why-you-should-care/

Your 2. is updating your remote tracking branch and merging that into your branch (pull does fetch and merge). It does exactly what you asked for: get the state on remote and integrate it to your branch. But it is not what you usually want: update master and origin/master too. There is nice explanation here and here:

https://stackoverflow.com/a/18137512/12118546

https://stackoverflow.com/a/8690791/12118546

Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28
1

Here is what I do to avoid conflicts;

  • Checkout master and pull the latest of master.
  • Checkout your feature/my-new-feature branch, and pull the latest of your feature branch if you need to.
  • Create a new feature branch off your feature/my-new-feature branch, name it something like feature/merge-from-master-into-my-new-feature-branch.
  • Using your IDE or Git tool, merge master into your feature/merge-from-master-into-my-new-feature-branch, and resolve the conflicts locally.
  • Once all is ready, then commit and push your feature/merge-from-master-into-my-new-feature-branch changes.
  • Create a PR from your feature/merge-from-master-into-my-new-feature-branch into your feature/my-new-feature.
  • Review the PR, approve it and delete feature/merge-from-master-into-my-new-feature-branch as you don't need it any longer.
  • Checkout feature/my-new-feature and pull the latest changes.
  • At this point, you have all your latest master changes in your feature/my-new-feature branch and you are good to continue working on your changes.
Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20