Dan
Dan

Reputation: 57961

How can I clear all previous commits from git history?

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.

Upvotes: 1

Views: 180

Answers (4)

Sven Marnach
Sven Marnach

Reputation: 602525

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.

Upvotes: 0

René Link
René Link

Reputation: 51463

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

Upvotes: 1

clash
clash

Reputation: 532

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

Upvotes: 0

Jepessen
Jepessen

Reputation: 12435

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

Upvotes: 0

Related Questions