I have a website, when I started to develop it, I've cloned Laravel github repo with all it commits. Now I want to remove all original commits, leave only mine. Is there any way to do this? I tried git rebase, but it requires to choose all 6k commits manually and removes commits from the latest. (I need from the oldest).
3 Answers
I think you can do this by checking out a specific commit, creating an orphan branch at that commit, then rebasing onto the branch.
- Checkout the commit before your first commit
git checkout <commit hash before your first commit>
- Create an orphan branch. The index will contain the files at that state, but there will be no commit history.
git checkout --orphan foo
- Commit the squashed changes
git commit -m 'Squashed!'
- Checkout your main branch
git checkout <original branch name>
- Rebase to the new squashed branch.
git rebase foo
I tested this with a repository with 5 commits on master
:
* 40f82e6 - 5
* 78deb2c - 4
* aff34f8 - 3
* 15cd469 - 2
* 6e420cb - 1
Let's squash the first three (output is abbreviated):
$ git checkout aff34f8
Note: checking out 'aff34f8'.
HEAD is now at aff34f8 3
$ git checkout --orphan foo
Switched to a new branch 'foo'
$ git commit -m 'Squashed!'
[foo (root-commit) 933b62d] Squashed!
$ git checkout master
Switched to branch 'master'
$ git rebase foo
First, rewinding head to replay your work on top of it...
Applying: 1
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: 2
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: 3
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: 4
Applying: 5
After this change, master now looks like this:
* 5d908c9 - 5
* 477ee53 - 4
* 933b62d - Squashed!

- 20,844
- 5
- 51
- 74
-
I see that working, but a have a lot rebase conflicts – EXayer Mar 06 '20 at 14:21
You could just create a new orphaned branch if you're willing to lose your commits.
In this example the branch will be called "fresh":
git checkout --orphan fresh
Read more about git checkout --orphan
in this question.

- 24,894
- 8
- 75
- 90
You could use git rebase
, but with 6k or so commits to squash, that could be quite cumbersome.
Another option could be to do the following:
From your final commit, create a new branch:
git checkout -b my-branch
"Reset" your staged files to be the difference between the very first commit and your current state:
git reset --soft [hash of initial commit]
Now, your staged files are all the differences between the initial commit and the last commit. Do a
git commit
to commit these changes.
Now, on this newly created branch, you have a single commit after initial, which contains all the changes over those original 6k commits.

- 4,984
- 30
- 34