138

I made a new branch called feature1 from the main develop branch a month ago.

⇒  git branch 
  develop
* feature1

I've been working on feature1 for a month now and a lot of changes have been pushed to develop.

How can I update my current branch feature1 with the latest commits from develop?

I DO NOT want to checkout master and merge my feature1 branch into it. Neither do I want to use git cherry-pick to manually move commits from develop to feature1.

How would I go about doing this?

10 Answers 10

184

You just merge develop to feature1:

git checkout feature1
git merge develop

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

10
  • 2
    But this will place all commits of develop into the branch
    – htafoya
    Feb 7, 2020 at 21:13
  • 4
    Of course it will. The question essentially was "how can I place all commits of develop into the branch".
    – musiKk
    Feb 11, 2020 at 7:20
  • 9
    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
    – htafoya
    Feb 12, 2020 at 18:32
  • 2
    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"
    – htafoya
    Feb 13, 2020 at 23:25
  • 3
    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.
    – Michael
    Feb 14, 2020 at 0:28
108

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
2
  • 3
    I think this is a better solution than the designated answer.
    – csg
    Nov 10, 2020 at 14:11
  • 1
    Rebase means deleting the commit history from feature and instead have the history from develop
    – Timo
    Jul 13, 2021 at 8:37
7

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.

2
  • git rebase is like merge, isn't it?. but same thing happens, Apr 17, 2020 at 4:51
  • 2
    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.
    – EliuX
    Apr 17, 2020 at 5:51
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.

0

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
0

To avoid having the commits from develop by using a simple merge, i've found that the easier (less techier) way to do it is specially if you already pushed:

  1. Change to develop and be sure you pulled latest changes
  2. Create another branch from develop , feature1_b
  3. Merge feature1 to feature1_b
  4. Delete if you wish original feature1

So when you do your PR of feature1_b into develop, it will only have your new changes and not the whole history of commits.

If you haven't pushed then @stackdave's answer is a good answer.

0

I have found the solution. Checkout the previous branch and pull the new branch https://stackoverflow.com/a/71306254/17779236

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – aaossa
    Mar 6, 2022 at 3:25
0

In IntelliJ IDEA just follow these steps:

  1. open the Git Tool Window (View->Tool Windows->Git) enter image description here

  2. select "Log"

    enter image description here

  3. right click on "develop"

  4. click on either

    -Merge 'develop' onto 'feature1' (keeps all commits history from your branch)

    or

    -Rebase 'develop' into 'feature1' (delets the commit history from your branch and instead have the history from develop)

    enter image description here

  5. Finally Git push

-1
$ git checkout <another-branch> <path-to-file> [<one-more-file> ...]
$ git status
$ git commit -m "Merged file from another branch"
0
-3
  1. git checkout develop
  2. git pull 3.git checkout localbranch

Then run merge or rebase based on your requirements

  1. git merge develop
  2. git rebase develop
1
  • 1
    How is your answer adding any new insights that were not already presented by the numerous other answers? Feb 15, 2021 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.