1

My project has a single branch master. Let's say I've performed four commits A, B, C and D. Now I just realized that D is something that is too experimental, so it should not be on master (yet), but rather on a separate branch of its own, say experimental.

How do I:

  • Put D on a branch of its own?
  • Go back to C as master and perform new commits on this branch?

(Note: I don't want to just undo the commit)

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51
  • I'm not sure if you can move a commit, but you can just [undo the commit](http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit), switch branches, and commit again. – GolezTrol Oct 12 '15 at 13:22

2 Answers2

3

First, you need to branch the experimental branch from master:

mureinik@computer ~/src/git/myproject [master] $ git branch experimental

Then, you can reset the master branch to one commit behind:

mureinik@computer ~/src/git/myproject [master] $ git reset HEAD~ --hard
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

First, create the experimental branch on the D commit. At this point it will be the same as master.

Do a git checkout master.

Do a git reset --hard HEAD~1. (This gets you on commit C)
Now do a push -f (from the master branch. this will push C as last commit on master).

This should solve it.

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45