2

So, in our git repository, some commits were made (not the most recent commits now) into our develop branch, that should have been in a feature branch. The branch they should be is was made further down the line. The commits ended up being for functionality that still isn't completed, so now we really need to get them off of the develop branch so that we can go back to having a functioning develop branch.

Is there a way that I can move specific commits from the develop branch to the start of the feature branch? Not 100% familiar with git, so if any additional info is needed, please ask.

SvenskNavi
  • 117
  • 1
  • 12
  • Possible duplicate of [Move multiple non-consecutive commits to different branch](https://stackoverflow.com/questions/36493916/move-multiple-non-consecutive-commits-to-different-branch) – phd Jun 07 '18 at 15:57
  • 1
    You can't really move the commit, but you can create a new commit that applies the same patch, and you can create a new branch that has the same tree at its head as the current branch without the changes applied by the commits you're trying to remove. You'll want to read the documentation for `git rebase` and `git cherry-pick` (and probably some others!) – William Pursell Jun 07 '18 at 16:02
  • So maybe that's a bit of info that I'm not sure if I was clear enough on. The commits were made on develop and then the feature branch was created after, so the commits are already on both branches. Would that then be sort of a matter of rebasing the feature branch to an earlier commit, then cherry pick the commits on develop? – SvenskNavi Jun 07 '18 at 16:16

2 Answers2

2

Try cherry pick. With cherry pick you can get the selected commits of another branch to the current branch.

https://git-scm.com/docs/git-cherry-pick

pramesh
  • 1,914
  • 1
  • 19
  • 30
2

First, the usual disclaimer: what you want to do (i.e. removing commits from a branch) means rewriting history, so proceed with caution.

Assuming your history looks something like this:

A--B--C--D--E develop
         \
          F--G feature

And you want to "move"1 commits B and C from develop to feature, you can do that with the rebase --onto command:

git checkout develop
git rebase --onto feature A C
# This will apply commits B and C on top of the commit referenced by "feature"

Now you history will look like this:

A--B--C--D--E develop
         \
          F--G--B'--C' feature

Notice that B' and C' (read "B prime" and "C prime") are new commits that contain the same changes as B and C but have different commit IDs because they have different parents.

At this point you can remove B and C from the develop branch by saying:

git rebase --onto A C
# This means rebase the current branch on top of commit A instead of C

which will result in commits B and C no longer being reachable in develop:

A--D'--E' develop
A--B--C--D
         \
          F--G--B'--C' feature

However, you still have the feature based off of the old commit D, which has now be rewritten as D'. To solve that you need, once again, to rebase feature on top of the new D':

git checkout feature
git rebase --onto D' D

Finally resulting in:

A--D'--E' develop
    \
     F--G--B'--C' feature

which is probably what you wanted.


1) See @WilliamPursell's comment about what it means to "move" commits in Git.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154