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?

2
  • The command is rm -rf .git; git init. Commented Jun 6, 2011 at 8:22
  • @larsmans, this is obviously not what the OP asked for. Commented Jun 6, 2011 at 8:40

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

master has already been pushed to GitHub. Will that be a problem when I try to push the new master later?
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 <github remote here> +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.
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.

7 Comments

Git doesn't let me revert back to the first commit.
@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.
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 <hash> 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, indeed, revert will not remove the history. I said this is a way to keep the history.
@Radu, still git revert <commit> does not put the tree state into the state of <commit>^. Instead it will put the tree into the state of HEAD with the inverse patch of the changes introduced between <commit>^ and <commit> applied on top. It will only undo the changes introduced in a single commit!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.