1

Use-Case:

I am creating a clone of my repository and I wish to maintain the version history of every other file but one, lets call it "black-sheep.json"


Following is the only approach I've discovered as yet:

  1. Create a temporary copy of black-sheep.json, say tmp.json
  2. Then remove all history for black-sheep.json using this command
git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch black-sheep.json' \
  --prune-empty --tag-name-filter cat -- --all

.3. Lastly rename the tmp.json to black-sheep.json

I don't like this solution! Is there a better way ?


I just realized another approach, I can squash all the commits for this particular file into a single commit.

However in this approach I have the following doubts

  • How to do this in a non-interactive mode ?
  • How to do this across multiple non-sequential commits ?

NOTE:

  • All commits include only a single file.
  • I need to implement this in code.
shantanusinghal
  • 714
  • 1
  • 8
  • 17

2 Answers2

1

1/ How to do this in a non-interactive mode ?

The only solution I know of would use a git rebase --interactive --autosquash, but it is based on identical commit messages: if you don't have those, you would need to:

  • first, rebase --interactive in order to edit those messages
  • second, use the autosquash

But that would ruin the "non-interactive" part of the process.

2/ How to do this across multiple non-sequential commits ?

That is what autosquash does, but in my opinion, the git filter-branch remains the omre precise tool to do what you want in one (non-interactive) step.
Another tool could be BFG Repo-Cleaner, if you don't like the syntax of the git filter-branch command.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

JGit does provide an API for doing a interactive rebase.

But I decided to keep it simple and rename the file to append it with a new version number each time I clone the repository.

So back-sheep_revA.json in the new revision becomes black-sheep_revB.json

And delete black-sheep_revA.json

It's not a very refined approach but it keeps the complexity low and get the job done (remove the history for black-sheep.json)

shantanusinghal
  • 714
  • 1
  • 8
  • 17