Why does the -cached option on the filter branch delete files from the working directory?

I needed to delete some Xcode files from an old repo, which was supposed to be ignored. So I ran the following command

git filter-branch --index-filter 'git rm -f --cached --ignore-unmatch *mode1v3 *pbxuser' HEAD 

My understanding was that adding - caching would not affect the current working directory, but git deleted the corresponding files. Fortunately, I had a backup (!), But I'm curious why she does it, or I don’t understand what --cached does?

+6
source share
2 answers

The culprit is not a git rm command. The --cached option works as you say. You can easily try this in a small git repository.

Although the man page does not mention this, git filter-branch does not seem to save your workspace. In fact, the team refuses to run if your workspace is not cleared, which is already an indicator.

But even if the files disappeared from the workspace, they did not disappear from the repo. They are no longer in any commit available in your current branch. But branch-store repositories refer to your branch before overwriting the refs / original / namespace in the namespace.

Use the git show-ref command to see it.

You can check the old version for accessing deleted files. You can use the git cat-file blob refs/original/refs/heads/master:foo command to get the contents of the file without checking (use the link shown by show-ref, foo is the name of the file you need). There are many possibilities.

You can use gitk --all to navigate your rewritten and current branches, and you will see that nothing remains.

+4
source

The behavior of git-filter-branch may be unexpected, as you have discovered, and it will not protect you from unforeseen consequences when it starts.

Instead, I recommend using BFG Repo-Cleaner , a simpler, faster option specifically designed to remove files from Git history. One way to make your life easier is to not delete or modify in any way the files in your last commit .

You should follow the instructions, but the main bit is just that: download the BFG jar (requires Java 6 or higher) and run the following command:

 $ java -jar bfg.jar --delete-files *{mode1v3,pbxuser} my-repo.git 

Any file matching this expression in the history of your repository that is also not in your last commit will be deleted. Then you can use git gc to delete dead data:

 $ git gc --prune=now --aggressive 

BFG, as a rule, is much easier to use than git-filter-branch - the parameters are configured around these two common use cases:

  • Delete Crazy Big Files
  • Removing Passwords, Credentials, and Other Personal Data

Full disclosure: I am the author of BFG Repo-Cleaner.

+1
source

Source: https://habr.com/ru/post/894476/


All Articles