How to update my working Git branch from another branch (develop) ?

150,216

Solution 1

You just merge develop to feature1:

git checkout feature1
git merge develop

There is no need to involve another branch such as master.

Solution 2

First you need to update your develop branch, then checkout your feature and merge/rebase it.

git checkout develop
git pull
git checkout feature/myfeature

Now you can decide between running:

git merge develop
git rebase develop
  • merge: keeps all commits history from your branch, and that is important if your partial commits have a lot of content that can be interesting to keep.

  • rebase: Rebase means deleting the commit history from feature and instead have the history from develop; this option is obligatory in some teams.

When you are ready you can push to your own branch (for example for a pull request)

git push origin feature/myfeature

Solution 3

This use case is very helpful to keep updated your PR branch. Firstly, I would recommend you to fetch first your remote changes, i.e. git fetch and then merge or rebase from develop, but from the remote one, e.g.

git rebase -i origin/develop

or

git merge origin/develop

This way you will update your PR branch without going back and forth between branches.

Solution 4

If you don't want that the develop head and the feature1 head will merge both into feature1, but instead you want keeping each branch head distinct while "updating" feature1 branch with the latest edit from develop, use no fast-forward:

git pull
git co feature1
git pull
git merge --no-ff develop
git push

I personally try to use --no-ff everytime I perform a merge because in my opinion it keeps the history quite clean.

Solution 5

BRANCHS:

DEV ====> develop

feature1 ====> working


STEP 1 GIT SENDING FROM THE SITE

checks the branch you're syncing

git status

add files for the commit

git add .

commits with a description

git commit -m "COMMENT"

send to the branch that you are synchronized

git push

STEP 2 SYNCHRONIZING THE UPDATED WORK BRANCH WITH DEV (development) - synchronizes the working branch with the development branch (updates the development branch)

synchronize with the remote and switch to the DEV branch

git checkout DEV

request to merge the branch that you are syncing with the feature1 branch

git merge feature1

Merge the current branch with the feature1 branch

git push

STEP 3 GIT FINDING THE REMOTE - Update the working branch from the updated development branch

connects to the reference branch

git checkout DEV

Search changes

git pull

Syncs with your work branch

git checkout feature1

request to merge the branch that you are synchronized with the DEV branch

git merge DEV

Merge the current branch with the DEV branch

git push
Share:
150,216
Eki Eqbal
Author by

Eki Eqbal

Something about myself :) ®║▌│█│║▌║││█║▌║▌║▌ OFFICIAL Stackoverflow CELEBRITY ✔ Verified Official Page! ✔ Stackoverflow Certified Sorry, im too complicated to be described in a couple of words. Im a puzzle, yes indeed. :)

Updated on July 08, 2022

Comments

  • Eki Eqbal
    Eki Eqbal almost 2 years

    I made a new branch for my code like a month ago, I created feature1 branch from develop branch.

    ⇒  git branch 
      develop
    * feature1
    

    I've been working on feature1 for a month now and a lot of the changes pushed to develop branch, How can I update my current branch feature1 with the latest commits at develop one?

    I don't want to checkout master and merge my feature1 branch. Neither I want to use git cherry-pick to manually move commits from develop to feature1.

    Any help ?

  • htafoya
    htafoya over 4 years
    But this will place all commits of develop into the branch
  • musiKk
    musiKk over 4 years
    Of course it will. The question essentially was "how can I place all commits of develop into the branch".
  • htafoya
    htafoya over 4 years
    No , it's not, it was "i am working in branch X, but i want that branch to have the latest updates from develop", when you do a PR to develop you wish only to have your branch commits and not all history of commits
  • musiKk
    musiKk over 4 years
    I don't understand. This has nothing to do with PRs. The current state of "develop" is supposed to end up in "feature1". A merge as described accomplishes that.
  • htafoya
    htafoya over 4 years
    Yes it has all to do. If the developer is alone or in a small team maybe nobody cares. But if he wants to merge this to develop in a team that has to review the branch, then they will see that all the develop commits were done instead of only showing the real changes introduced in the feature branch.
  • htafoya
    htafoya over 4 years
    I mean. the current state of "develop" will be in "feature1", but the commits were not generated there. With the merge it will appear as if all those develop commits were generated in "feature1"
  • Michael
    Michael over 4 years
    I don't think OP's question is about setting up a PR. It's about updating a personal dev branch to be based on the latest changes in the develop branch. Depending on what process you use to set up PRs, you should easily be able to specify exactly what commits you want reviewed (e.g., most recent commit only; or all commits on feature1 not in develop; etc.) Anyway, once the merge is completed, feature1 and develop are definitely going to share commit history. It's not relevant which branch the developer was working in at the moment in the past when they committed onto develop.
  • Michael
    Michael over 4 years
    Sorry, I think I did a poor job explaining. Let me try again... after the dev runs git merge develop, the tip of feature1 will be updated, including new code from develop. This lets the dev work with up-to-date code. The merge does not affect the develop branch in any way. Maybe at some point in the future, this dev will want to merge feature1 into develop (i.e. other way around) to update tip of develop and this requires review. At that point, whatever temporary merge commit(s) produced will be specified in the review so that reviewers can see the diff of how develop will change.
  • Kavindu Gayantha
    Kavindu Gayantha about 4 years
    git rebase is like merge, isn't it?. but same thing happens,
  • EliuX
    EliuX about 4 years
    In the git rebase your rewrite the history of your branch with a mix of the 2, with the merge you just apply the changes of the other branch into yours in a new merge-commit.
  • csg
    csg over 3 years
    I think this is a better solution than the designated answer.
  • EricSchaefer
    EricSchaefer about 3 years
    How is your answer adding any new insights that were not already presented by the numerous other answers?
  • Timo
    Timo almost 3 years
    Rebase means deleting the commit history from feature and instead have the history from develop
  • red_wolf
    red_wolf over 2 years
    Hi folks, thanks for the above info and I need a little help with this: I now have the latest changes from 'Develop' in my branch 'feature1' in my local machine. I want my branch 'feature1' on GITHUB to have the latest updates from 'develop' as well. How can I do this?
  • red_wolf
    red_wolf over 2 years
    Well, just figured out, github was updated(synced) too, after I merged 'develop' into my branch 'feature1' . Cheers.