140

In my git repo, I have a Master branch. One of the remote devs created a branch Branch1 and had a bunch of commits on it. I branched from Branch1, creating a new branch called Branch2 (git checkout -b Branch2 Branch1) such that Branch2 head was on the last commit added to Branch1:(Looks like this)

Master---
         \
          Branch1--commit1--commit2
                                   \
                                    Branch2 (my local branch) 

Branch1 has had a number of changes. The other dev squashed his commits and then added a few more commits. Meanwhile, ive had a bunch of changes in my branch but havent committed anything yet. Current structure looks like this:

  Master---
             \
             Branch1--squashed commit1,2--commit3--commit4
                                       \
                                        Branch2 (my local branch)

Now I want have to rebase my changes on top of Branch1. I am supremely confused on how to go about this. I know the 1st step will be to commit my changes using git add . and git commit -m "message". But do i then push? using git push origin Branch2 ? or git push origin Branch2 Branch1 ? Help is much needed and GREATLY appreciated, also if I can some how create a backup of my branch, it will be great in case I screw something up

2

4 Answers 4

175

First backup your current Branch2:

# from Branch2
git checkout -b Branch2_backup

Then rebase Branch2 on Branch1:

# from Branch2
git fetch origin           # update all tracking branches, including Branch1
git rebase origin/Branch1  # rebase on latest Branch1

After the rebase your branch structure should look like this:

master --
         \
          1 -- 2 -- 3 -- 4 -- Branch2'

In the diagram above, the apostrophe on Branch2 indicates that every commit in the rebased Branch2 after commit 4 is actually a rewrite.

Keep in mind that you have now rewritten the history of Branch2 and if the branch is already published you will have to force push it to the remote via

git push --force origin Branch2

Force pushing can cause problems for anyone else using Branch2 so you should be careful when doing this.

12
  • What if there are any merge conflicts ? Will they show up when I do a git rebase Branch1? Im assuming any conflicts should prevent the merge and then I can fix them before I merge the branches
    – Beginner
    Commented Oct 12, 2016 at 23:26
  • @newkid101 You can of course have merge conflicts as each commit from Branch2 is being reapplied. Simply resolve them all, and type git rebase --continue when you have done so. Commented Oct 12, 2016 at 23:30
  • Where Do I commit my changes done in Branch2 ? Is it during the rebase as well ?
    – Beginner
    Commented Oct 12, 2016 at 23:35
  • 1
    So here are your options: if you want the changes, then do git add on each file and then commit. If you don't want any of the changes, you can do git reset --hard to throw them away. If you are unsure, you can do git stash and worry about it later. Only use the reset option if you are certain that you don't really want the changes. Commented Oct 12, 2016 at 23:56
  • 1
    Of course I want the changes. Ill do a git commit -m "commit message" before I do a rebase
    – Beginner
    Commented Oct 12, 2016 at 23:58
58

git rebase branch1 branch2 will rebase branch branch2 onto branch1. Operationally, this means any commits which are contained only in branch2 (and not in branch1) will be replayed on top of branch1, moving the branch2 pointer with them. See git rebase --help for more information, including diagrams of this operation.

The operation might produce some conflicts which then you'll have to resolve manually. Edit the affected files, merging content and removing any failed hunks. Afterwards, mark the files as merged using git add <file> and then continue the rebase using git rebase --continue. Repeat until it is done.

Once done, you have nothing else to do. You don't have to push. However if you wish to mirror your new changes to some other repository (for instance, to share it with others or to have those changes in another repository of yours), do a final git push.

3
  • 4
    Warning: git rebase -i branch1 branch2 does an automatic checkout of branch2, and then modifies the commits in branch2. branch1 remains untouched. Commented Nov 19, 2018 at 15:24
  • 1
    Just for future reades: note that if branch1 reside only on remote, you have to checkout into 'branch1` that the "smart" git will create on the fly and track automatically that in remote(if this does not happens, then you have to do this process manually), then execute has the answer suggested Commented Mar 29, 2022 at 13:58
  • I would also like to add that check you are subsequent rebase from the origin( origin/branch1) that may have some after commits after the first rebase otherwise you could be always updated but missing those Commented Mar 31, 2022 at 14:01
7

I want to rebase my changes (from local branch2) on top of branch1.

git checkout branch2   # Go to your local branch. Use -f to force the checkout.
git reset HEAD --hard  # Drop all non-committed changes.
git rebase branch1     # Rebase on top of branch1. Use -i for an interactive list.

Note: If a branch is on the remote such as origin, prefix the branch name with origin/.

Troubleshooting

  • If you got stuck in the middle of rebase and you want to start over, run:

    rm -fr .git/rebase-merge # Abort a rebase-merge mode.
    git reset HEAD --hard    # Reset everything to the current HEAD.
    
  • If you're on the detached branch (run: git branch and look for the star symbol), run:

    git checkout branch2 -f # and start again.
    
  • If you get conflicts, you need to fix them, use different rebasing point.

  • If you'd like to do rebase manually step by step, use cherry-picking. E.g.

    git reflog              # Note hashes of for your commits.
    git checkout master     # Go to your base branch.
    git cherry-pick C0MM1T1 # Cherry pick first commit based on its hash.
    # Go to the next one or solve the conflicts.
    git cherry-pick C0MM1T2 # Cherry pick another commit and so on.
    
  • If your rebase showing too many commits on the interactive list after running git rebase branch1 -i, you can start your rebase given the specific commit just before your changes, e.g. git rebase pr3v1ios.

4

First of all, you have to make sure your reference to Branch1 is up to date (specialy since it's history has been modified).

If you like to work with local copys, you cand do something like this:

git push origin Branch2 # this ensures you have at least one copy in your remote
git fetch origin
git checkout Branch1
git reset --hard origin/Branch1
git checkout Branch2
git rebase Branch1 # solve conflicts ... and check that everything is ok
git push -f origin Branch2

Your Answer

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

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