15

I am trying to rebase my remote branch onto master, but I want to keep the remote branch pointing to it's commits, just based at a different point in master.

Here is my structure:

A - B - C - D  (origin/master)
 \
  R - S - T (origin/develop)

I would like:

A  - B - C - D (origin/master) - R - S - T (origin/develop)

Is such a rebase possible without some sort of merge?

Andrew
  • 1,355
  • 2
  • 13
  • 28
  • why don't you want to just use 'merge' in that case? – Artyom Pranovich Oct 07 '16 at 15:44
  • I also want to squash the commits on origin/develop into 1 commit onto origin/master – Andrew Oct 07 '16 at 15:48
  • In Git, you do all your work on *local* branches. Remote-tracking branches like `origin/develop` just remember for you what your Git saw on `origin`'s Git the last time your Git had a conversation with the Git on `origin`. To make a remote branch change in some way, you must convince that other Git to change it (typically, by making the change locally first, on *your* branches, and then pushing that change). – torek Oct 07 '16 at 22:19

2 Answers2

25

To see more about rebase you can read the manual or write git rebase --help at your terminal

To solve your problem there is a easy way, following these steps:

git branch -D develop          # remove your local develop repository
git fetch //update references 
git checkout develop          # change to develop branch, but because you deleted,
                              # this command will download from origin/develop
git rebase -r origin/master  # -r is --rebase-merges

After this step you might have some conflicts, so resolve them and git add FILES THAT HAD CONFLICTS and git rebase --continue

Now check if everything still working after rebase; if yes

git push -f origin develop

Note: Prior to v2.35, there was an option -p (--preserve-merges), which had some slight differences to -r but was similar in spirit and function.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • Does this imply that before rebasing, our local repo gets lost and the upstream repo is cloned into local? – Pe Dro Feb 29 '20 at 16:42
4

In your context, you will do

git rebase origin/master    
git rebase origin/master origin/develop

Official reference: At the beginning

      A---B---C topic
     /
D---E---F---G master

after do

git rebase master
git rebase master topic

we have

              A'--B'--C' topic
             /
D---E---F---G master

(Source: https://git-scm.com/docs/git-rebase)

Vy Do
  • 46,709
  • 59
  • 215
  • 313