Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting each commit by file?

Tags:

git

I've accumulated a lot of git commits for my project. As the first step in cleaning the repo up, I'd like to split every commit into two commits: one which only touches a particular file /some/directory/file, and another which touches everything else.

Since the git history is fairly long, I'd like to avoid doing this manually.

Some approaches that I've thought about (but haven't actually tried out) are:

  • using git rebase --exec with a script that does the splitting.
  • using filter-branch. (don't have much familiarity with this tool)

However, since this seems to me like a fairly common problem, I thought I would check here before attempting to reinvent the wheel. Is there any automated way to do this?

like image 846
Hrushikesh Avatar asked Jan 01 '16 13:01

Hrushikesh


1 Answers

You are on the way, what you have tried is the way to do it.
As you found out you have 2 main options:


git rebase -i aka: squash (manual procedure)

// Manipulate any commit by editing it and commitign again
git rebase -i HEAD~X

Since the git history is fairly long, I'd like to avoid doing this manually.

git filter-branch

  • Read this article which address your problem How to tear apart a repository: the Git way

  • git filter-branch will run on every commit specified in the condition and then you can do what ever you wish with it (via script) and not manually.

  • Here is another very useful article about filter-branch

  • Splitting a subfolder out into a new repository


sample

// Remove any given file from commits 
// Now you can commit it again into a new commit

git filter-branch --tree-filter 'rm -f <file name> HEAD
like image 131
CodeWizard Avatar answered Oct 17 '22 07:10

CodeWizard