1

I have started a new project recently, and I've made a lot of experiments at it's beginning. Now some new people are joining, and I have an idea of wiping the first commits away from git history, because they seem to make no sense and everyone browsing the repo can get confused.

A possible solution is setting up a new repo from scratch, but I would prefer rewriting the history.

What I need is just setting the HEAD commit on master to be the first and only commit in the repo.

Dan
  • 55,715
  • 40
  • 116
  • 154
  • 3
    Create a new git repository and throw away the old one? That sounds like the best way to get what you like to do. – Nitram Jan 19 '15 at 14:59
  • i think you should revisit your preference for blowing away history. you as the original author may want to reference something down the road. i'd highly recommend just creating a new repo and keeping your other one to yourself. – NG. Jan 19 '15 at 14:59
  • @NG, that would require changing configs on several staging servers – Dan Jan 19 '15 at 15:05
  • if they all point to the same remote, you can still move the current repo, git init a bare repo that replaces the current one and then push a single commit to it. do you not own the remote? – NG. Jan 19 '15 at 15:10
  • @NG I own it. I just do not want to change the repo url everywhere – Dan Jan 19 '15 at 15:20
  • The answers given are correct. I just wanted to add that you *could* create a tag which points to the old history and even push that one too, just in case you want to keep a reference onto the old history. – Sascha Wolf Jan 19 '15 at 15:39
  • *Why* would you prefer rewriting the history? Deleting and recreating the repo is the simplest and most reliable way to do what you want (consider grabbing a copy of the repo first). `rm -rf .git; git init; git add .; git commit -m "..."` – Keith Thompson Jan 19 '15 at 15:55

4 Answers4

1

If you want to use git rebase you might be interessted in Git - squash entire branch - one line squash command.

If you have a history with a lot of commits, git rebease might take a long time. In this case you can make a shallow clone

git clone --depth 1 <REPO_URL>

than do a

git commit --amend

in order to create an independent commit that contains the latest changes and than do a forced push

git push -f
Community
  • 1
  • 1
René Link
  • 48,224
  • 13
  • 108
  • 140
0

Squash all unwanted commit in your local repository, and then push them into remote repository with the --force option.

Jepessen
  • 11,744
  • 14
  • 82
  • 149
0

If you are really sure about what you are doing you can do an interactive rebase onto your first commit:

git rebase --interactive --root

and fixup every single commit. But at least I would recommend to checkout another branch before:

git checkout -b rebase
clash
  • 532
  • 4
  • 17
0

You can create a new root branch without any parent commit using

git checkout --orphan new-master

and commit the repository state to that new branch using

git add .
git commit

and push it to the server using

git push --force origin new-master:master

This will make the old history unaccessible on the server, so use it with care.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841