1

A parent question here: Move the most recent commit(s) to a new branch with Git

I have taken over a project that has a working tree full of files that still need organizing - some refer to work tasks in progress; others are completed. I have a recent commit of completed work, but this is not destined for production. How can I discreetly move it into some new branch without screwing up my current tree?

I'd like to change

Working tree - A - B - C - D - E

to

Working tree - B - C - D - E
              /
             A
Community
  • 1
  • 1
sscirrus
  • 55,407
  • 41
  • 135
  • 228

3 Answers3

0

With checkout -b "name of branch" you will create a new local branch.

Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30
0
git branch tmp
git reset --soft B

Will move the HEAD (and only HEAD) to B (after marking A with a 'tmp' branch)

See "Practical uses of git reset --soft?" for more on reset --soft.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

First write A's SHA1 somewhere, you'll need it later.

git checkout <E's SHA1>
git rebase -i HEAD~6 (6 is commit distance from E to A plus one)

Delete A's line in the editor and save.

Now you have this:

 Working tree - B - C - D - E

Now we want to create a branch to A, starting from B:

git checkout <B's SHA1>
git branch BAbranch (or whatever you want to call it)
git checkout BAbranch
git cherry-pick <A's SHA1>

And now you have this:

 Working tree - B - C - D - E

                  \

                     A  
bcoder
  • 39
  • 4