4

The master branch of a new project's git repo contains only prototype/spike code. I'm about to re-start the coding from scratch, now that I have the basic ideas understood.

I want the master branch to be clear of all the prototype code. Ideally the prototype code needs to be moved into a branch so I can still get to it later.

Is it possible to reset master back to before the first commit? What are the git commands to do this?

Andrew Davey
  • 5,441
  • 3
  • 43
  • 57

3 Answers3

5

If you're really going to start from scratch, just create a completely new repository. It's possible to have two branches without any common ancestor, but it's not really that useful.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
4
  1. From the current master branch, make a new branch with your prototype code. git checkout -b prototype.
  2. Get back to the master branch with git checkout master.
  3. Remove all the files from the master branch with git rm -rf *.
  4. Commit your changes on the master branch with git commit -m "Removed prototype code".

This way, the history will be kept and you can always track in the log when the switch took place.

rid
  • 61,078
  • 31
  • 152
  • 193
  • Git doesn't let me revert back to the first commit. – Andrew Davey Jun 06 '11 at 08:38
  • @Andrew Davey, if you want to get to a version where there are no files at all in the project, then just `git rm -rf *` instead of `git revert`. – rid Jun 06 '11 at 08:42
  • Sorry, but `git revert` will not remove any of the history but rather *add* another commit which just has the reverse change of the specified commit. I suspect, you really wanted to say `git reset --hard ` which will move `master` to the state *after* the named commit. This is however no straight-forward route to going back to *before* the initial commit. Also, `reset` will as well cause the new `master` to be non-fast-forward w.r.t. the old `master` and the `push` related comment on my answer holds here as well. – Tilman Vogel Jun 06 '11 at 14:48
  • @Tilman Vogel, indeed, `revert` will not remove the history. I said this is a way to *keep* the history. – rid Jun 06 '11 at 15:26
  • @Radu, still `git revert ` does *not* put the tree state into the state of `^`. Instead it will put the tree into the state of HEAD with the inverse patch of the changes introduced between `^` and `` applied on top. It will only undo the changes introduced in a single commit! – Tilman Vogel Jun 06 '11 at 18:18
  • @Tilman Vogel, you're right, `revert` won't work. I changed the answer to use `rm` instead. – rid Jun 06 '11 at 18:31
  • @Radu, ok, that of course works. Somehow, I think two parallel branches next to each other are a little little bit more useful (because the original prototype branch is not formally merged in already and because the tabula rasa commit is a bit awkward history). – Tilman Vogel Jun 06 '11 at 18:51
4

This is how you create a new branch not sharing any history with your other branches: How to create a new empty branch for a new project.

Before following these instructions, just rename your existing master to prototype using git branch -m master prototype. Then, when following the linked instructions, use master instead of newbranch.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
  • master has already been pushed to GitHub. Will that be a problem when I try to push the new master later? – Andrew Davey Jun 06 '11 at 08:49
  • Yes and no: The new `master` will not be fast-forward of the current `master` (then `prototype`). Thus, you will need to force the push using `git push +master`. If people are still working on that branch, they will be disturbed by this forced update. You should then also push the old `master` as `prototype` to github such that people may do the transition to the new `prototype` branch. – Tilman Vogel Jun 06 '11 at 09:18