schoetbi
schoetbi

Reputation: 12906

Flatten old history in Git

I have a git project that has run for a while and now I want to throw away the old history, say from start to two years back from now. With throw away I mean replace the many commits within this time with one single commit doing the same.

I checked git rebase -i but this does not remove the other (full) history containing all commits from git.

Here a graphical representation (d being the changesets):

(base) -> d1 -> d2 -> d3 -> (HEAD)

What I want is:

(base) -> d1,d2 -> d3 -> (HEAD)

How could this be done?

Upvotes: 25

Views: 21823

Answers (5)

mkrieger1
mkrieger1

Reputation: 23314

I got it working with

git rebase -i cd1e8c9

with cd1e8c9 being the start revision (base) to squash. Then I used fixup to meld the revisions together.

This was originally added as Revision 3 of the question.

Upvotes: 0

willnode
willnode

Reputation: 1527

This may seems unconventional, but if you don't care about the history, and the repository have only single master branch and have not been published to Github.com or other sites, you can:

  1. Delete hidden .git folder in the root of repo
  2. git init
  3. Commit

Remember the first step will clear all git data including branch and commit, so please be careful.

Upvotes: 4

murraybo
murraybo

Reputation: 973

If you do not really care about the whole history, another simple way to do this would be to take the current branch and create an orphan branch based on this. Then add all files to this branch and make one initial commit (which would lose all history). This can then be pushed to the remote repository.

Assuming you are in the branch that you want to flatten. First check if it is clean:

git status -s

The above command should not output anything.

Now create a orphan branch:

git checkout --orphan flattened

Add all files

git add .

Create single commit

git commit -m "Initial flattened commit"

Check if everything is as wanted and push to remote (ex):

git status -s

# (original_branch being the branch with the full history)
git diff original_branch..flattened  

# (assuming your remote is origin and the branch to overide is master) 
# Think twice before doing this!
git push origin +flattened:master 

Upvotes: 44

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74292

squash the respective commits into one using git rebase --interactive.

Upvotes: 4

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72855

I'm not very comfortable with doing rebasing so try this on a separate clone to see if it works before doing it on your real work space.

Here are my commits

noufal@sanitarium% git log --pretty=oneline
967122e7d4e687c0707d25c62cb0d3b6a45d337f Added h
3b82cae737d5fb3317bc7a686a2fdf0fdd9d1c7e Added g
94d89e0455b12e1e4843e64a8f62f3ad6cdf42f3 Added f
a30321b7a367d9b7da6369783650acadbb773cfb Added e
04453f1c90ffde0574c9c8a76f154d741d7d83f4 Added d
ec723a3266e56cc39967bf117154465575905e31 Added c
f415d1f58e2f7bd4beea80ab9acd8309bf5b64e7 Added b
7f1f8d1f903168aa929818a0eb81e0ec7743fb85 Added a
21790602bd6c0a009899ea33e64fec63559c0a76 Added it

I'm rebasing 04453f1c90ffde0574c9c8a76f154d741d7d83f4 (Added d) onto 21790602bd6c0a009899ea33e64fec63559c0a76 (the first commit) and squashing them all and I do it with this command

git rebase 04453f1c90ffde0574c9c8a76f154d741d7d83f4 --onto 21790602bd6c0a009899ea33e64fec63559c0a76

After I finish this, the logs look like this

noufal@sanitarium% git log --pretty=oneline
c76290666c8b868d36290d8f5276b879bb78d05d Added h
7001ce74f0837b35c0b82abbb82ad8f40801449c Added g
051062042e56759d83258c5e90a9876aa6f52764 Added f
fb1a62b0c0faefa0110ef7b8eee01a13f2f62034 Added e
21790602bd6c0a009899ea33e64fec63559c0a76 Added it

Is this what you're looking for?

Upvotes: 3

Related Questions