3

I've made a few commits and now I want them (say from some commit in the branch) to move them into a new branch, i.e.:

 master - O1-O2-O3-X-C1-C2-C3 

to become

master - O1-O2-O3-X  
                   \ new_branch - C1-C2-C3

After that I need to make patches for the mentioned commits (C1, C2, C3) so that a friend of mine would be able to apply them on their tree.

As to the first part I suppose I should do something like:

  1. Create patches for x -> C3
  2. Reset to X
  3. Create branch
  4. Apply patches

Although here they suggest that I could use git branch new_branch; git reset --hard X; git checkout new_branch. Wouldn't reset --hard delete my commits?

I'm not too good with git to do it all without guidance. Thanks!

Community
  • 1
  • 1
Albus Dumbledore
  • 11,500
  • 22
  • 60
  • 102
  • 1
    The proposed sequence works fine because new_branch now points at the commits and prevents them from being garbage collected. More generally, though, if you're not sure about something in git, create a test repository, fire up gitk and try it out. Worst case scenario - you hose your test repository (big deal). Another thing worth mentioning is that you can mirror your current repository with git clone --mirror. This gives you a backup in case you screw anything up. Just run git remote update now and again from your mirror and all will be well :) – Stuart Golodetz Sep 29 '11 at 11:28

1 Answers1

2

No reset --hard wouldn't delete your commits, as they would still be referenced by the 'new_branch' branch.
(meaning that, by a checkout of 'new_branch', you would recover and see those commits again)

But it would delete any untracked changed in your working tree though, so make sure you don't have any work in progress before typing that command.

You can see a more detailed illustration of that sequence of commands in "What will git checkout master + git reset --hard do?".


To complete Stuart Golodetz's comment, should a git reset had removed or erased any commit, you would still have been able to get hold of those commit with a git reflog.
See:

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283