Git rebase one branch on top of another branch

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)

                                    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:

  • How to keep git from changing file ownership
  • Are code freezes irrelevant with Git?
  • Can't find branch name in git config file
  • How to make an existing branch track a remote branch?
  • How to rename a remote git branch name
  • Git SourceTree:shows symbolic link files as unstaged file
  •   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

  • git checkout previous commit without mouse
  • No branch matches prefix
  • Partial subtree upstream push in Git
  • How can I insert the current git commit into an org-mode buffer to be evaluated on export?
  • What should I store in Git?
  • Changing subfolder name in git project without losing file history?
  • 3 Solutions collect form web for “Git rebase one branch on top of another branch”

    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.

    git rebase branch1 branch2 will rebase the changes exclusively under branch2 onto branch1.

    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.

    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
    Git Baby is a git and github fan, let's start git clone.