0

i have been developing committing on the master branch, and would like to move all commits beyond the "initial commit" to a separate development branch, and keep the master for release versions.

right now, my tree looks like this:

master: A - B - C - D - E - F

i would like it to look like this:

development:   B - C - D - E - F
              /
master:      A -----------------

that way i would be able to merge a release like so:

development:   B - C - D - E - F --- X
              /                       \
master:      A ----------------------- Y

can someone suggest the best way to do this? i've seen other answers with similar but not exact cases, but i don't want to take the chance of screwing something up.

aynber
  • 22,380
  • 8
  • 50
  • 63
Daniel Byon
  • 198
  • 6
  • With git, you don't have to worry about screwing anything up once it's committed. Unless you actually delete the repository from your disk, any commits you make will always be there and be recoverable for a minimum of two weeks (by default) before they're eligible for garbage collection. – Ryan Stewart Jul 17 '11 at 21:12

2 Answers2

6

To be simple:

(on master)
git branch development
git reset --hard A
manojlds
  • 290,304
  • 63
  • 469
  • 417
0

first create a new branch for commit F (last commit on branch master):

git checkout -b dev master # or directly using the commit hash:
git checkout -b dev SHA1_OF_F # if master is currently checked out:
git checkout -b dev

then, move back your master branch (without destroying your working tree)

git branch -f master SHA1_OF_A

checkout master again if you want to continue working on it:

git checkout master

if you don't care about your working tree (it is clean, i.e. there are no uncommitted nor unstaged changes, you can use git reset --hard as suggested by @manojlds in his answer.

this alters (i.e. prunes) history of the master branch, so be sure you understand the consequences. if your changes have already been pushed to a public repo other users will have the development commits already contained in master and would have to reset their branch too.

Community
  • 1
  • 1
knittl
  • 246,190
  • 53
  • 318
  • 364
  • I'm getting a "! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@github.com:danielbyon/GetBig.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details." when i try to push to github – Daniel Byon Jul 17 '11 at 08:23
  • 1
    @daniel: yes, if you have pushed master before, you won't be able to do so again without forcing git push and overwrite because it would remove history from the public repository. users who have cloned your repository before have the master branch point to a commit which came after your last master commit. if you're absolutely sure you want to push (also applies if you are the sole developer using that repo), use `git push -f origin master` or `git push origin +master` – knittl Jul 17 '11 at 08:28