Pavel Shorokhov
Pavel Shorokhov

Reputation: 4974

How to clean git history?

Have a situation:

  1. [add many files] Developer has added accidentally the external dependencies to the repository (thousands files).
  2. [make commit] Next added over this commit several commits.
  3. [remove many files] And then he saw the external dependencies in the repository, and remove them.
  4. [make commit] And finnally make some commits and push it to master.

And now I see in Github stats, that developer added 1kk lines, and remove 1kk lines.

How can I clean git history? How change those commits and remove those files from history?

Thank you

Upvotes: 1

Views: 132

Answers (3)

dan
dan

Reputation: 13262

You can use the github recommended steps using either:

  1. Use filter-branch and on step 3 instead of Rackfile add your directory or files that were added by accident. Also follow the step 8, that is very important, since you are modifying the history.
  2. Use BFG, and also notify the other developers to do a rebase, since BFG will also alter the history.

Upvotes: 2

mcfedr
mcfedr

Reputation: 7975

You can use git rebase -i <last good commit> then in the editor choose to squash the commits the add and remove the files into one

This does have the disadvantage of meaning that you only have one commit where you might want the two change sets separate.

Also you will have rewritten history and need to be careful that your developers reset to your new history.

Upvotes: 1

Petr Skocik
Petr Skocik

Reputation: 60056

Use git-filter-branch with --tree-filter or --index-filter.

More info in the link.

Upvotes: 0

Related Questions