klijakub
klijakub

Reputation: 845

Checkout to another existing branch with changes from develop

I have a simple scenario.

So, I have 3 branches

I have changed something on /feature_1 committed and merge it to develop

And now I want to change something in the feature_2 branch, but it's not up to date, develop is a few commits ahead. I want that feature_2 branch is a copy from the develop branch.

I want to do something like this

git checkout -b new_branch

but to exsiting branch, I don't want to create a new branch.

So what should I do, because I've read a lot about it and should I checkout to feature_2 and pull from develop?

rebase?

merge?

please give me the best solution

Upvotes: 1

Views: 1052

Answers (2)

Marina Liu
Marina Liu

Reputation: 38116

Execute the command git pull origin develop --rebase on feature_2 branch will get what you needed.

Assume the commit history for now as below:

      E-----F-----G     feature_1
     /             \
…---A---…---B---C---D   develop
             \
              H---I     feature_2

After executing git pull origin develop --rebase on feature_2 branch, the commit history will be:

      E-----F-----G     feature_1
     /             \
…---A---…---B---C---D   develop
                     \
                      H'---I'    feature_2

As you find, feature_2 branch will on the latest commit D from develop branch.


Besides, if you haven’t committed changes on feature_2 branch (no commit H and I on feature_2 branch) as below graph:

      E-----F-----G     feature_1
     /             \
…---A---…---B---C---D   develop
            |
        feature_2

Except the way git pull origin develop --rebase, you can use below commands to reset feature_2 branch on the top of develop branch:

git checkout develop
git checkout -B feature_2

Note: -B option for git checkout command will create a new branch if the branch name not exist. While if the branch name exist, it will reset the existing branch.

Upvotes: 1

smerlung
smerlung

Reputation: 1519

In the case where feature_2 is unaffected by the changes made on develop then I would just finish feature_2 and then merge with develop.

In the case where feature_2 needs the changes made on develop, then if feature_2 has not been published (your the only person working on it) then I would rebase it on develop, because it creates a cleaner history. If you're not the only one working on feature_2, then I would merge develop into feature_2, such that the other people that work feature_2 don't get their history changed.

Upvotes: 1

Related Questions