Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase onto a tag when master and a branch is ahead of the current commits

Tags:

git

rebase

I am having difficulties with Git. I am following a remote repo on my local machine. The master branch is being updated from the remote and I always sync with it to get the updates. And then my local branch ("feature" branch) is rebased onto the master branch to apply my local changes. The issue is I forgot to rebase my local branch onto master branch's version tag. instead I rebased onto the latest commit of master branch. Now I want to rebase my local branch to master's version tag branch without losing my local changes. Any ideas how to do it?

                          o -- o
                         /     ^ feature branch
o --- o --- o --- o --- o --- o --- o 
      ^ V2.0                        ^ master (origin/master)

I want to rebase my "feauture" branch like this:

        o -- o
       /     ^ feature branch
o --- o --- o --- o --- o --- o --- o 
      ^ V2.0                        ^ master (origin/master)
like image 296
deepnote Avatar asked Sep 29 '16 10:09

deepnote


People also ask

How do I rebase my current branch with a master?

To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type git rebase master (where master is the branch you want to rebase on).

Do I need to commit before git rebase -- continue?

For each change you make, you'll need to perform a new commit, and you can do that by entering the git commit --amend command. When you're finished making all your changes, you can run git rebase --continue . As before, Git is showing the commit message for you to edit.

What does it mean to rebase onto?

git rebase --onto is more precises. It allows us to choose specific commit where we want to start and also where we want to finish. Like here: git rebase --onto F D. and the result is: Before After A---B---C---F---G (branch) A---B---C---F---G (branch) \ \ D---E---H---I (HEAD my-branch) E'---H'---I' (HEAD my-branch)


1 Answers

Checkout the git rebase manpage https://git-scm.com/docs/git-rebase.

The remote branch origin/master (assuming your remote is called origin) is the upstream master. Rebase works by selecting a common ancestor between two branches and then cut off one part:

                          o -- o
                         /     ^ feature branch
o --- o --- o --- o --- C --- o --- o 
      ^ V2.0                        ^ master (origin/master)

Note that C is the common ancestor between the feature-branch and origin/master This is what git rebase later will use when we use git rebase origin/master feature-branch.

Let's cut off the following:

                        [ X -- X ]
                         /     ^ feature branch
o --- o --- o --- o --- C --- o --- o 
      ^ V2.0                        ^ master (origin/master)

and attach it to v2.0. So let's do it:

git stash          #  move local uncomitted changes away
git checkout feature-branch
git rebase --onto v2.0 origin/master
git stash apply    # reapply uncommitted changes

Rebase

The rebase command will calculate the common ancestor between origin/master and your current branch. It will then cut off all the commits that are unique to your branch. (think about it as asking rebase for everything on feature-branch that is not also on origin/master). Now that we know what we cut off, we use the the --onto to specify the target. In our case a tag.

A good explanation about rebase can be found here

like image 170
somnium Avatar answered Oct 21 '22 00:10

somnium