Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten old history in Git

Tags:

git

git-rebase

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? Thanks.

EDIT

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. Thanks.

like image 756
schoetbi Avatar asked Dec 22 '10 06:12

schoetbi


People also ask

How do I clean up commit history?

If you have been lazily writing multiple vague commits, you can use git reset --soft <old-commit> to make your branch point to that old commit. And as we learned, Git will start by moving the branch pointer to it and stops right there. It won't modify the index or working directory.

Does git reset rewrite history?

In Git, this is actually called a reset, not a revert. Reverting has two important advantages over resetting. First, it doesn't change the project history, which makes it a “safe” operation for commits that have already been published to a shared repository.


1 Answers

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 
like image 58
murraybo Avatar answered Oct 16 '22 01:10

murraybo