3

While mirroring a git repo using the mirror commands, i'm looking for a way to exclude some folders. specifically .github folder.

I'm following these steps to mirror

1. git clone --mirror mirror repo
2. cd repo
3. git push --mirror localrepo

After step 2, can i delete the .github folder & push to local repo?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74

1 Answers1

0

You can, in general, delete files and folders, but this has no effect on your subsequent git push. Moreover, with git clone --mirror, you get no checkout, because --mirror implies --bare which means there is no working tree.

The problem here is simple: Git does not push (or fetch/clone) files. Git pushes commits. (Normally the clone operation doesn't copy branches either—only the commits—but the --mirror option changes that behavior, and the subsequent git push --mirror pushes all two-or-more-level refs.)

Commits do contain files. But the files you see and work with in a non-bare repository are not in the repository. They are copies that were copied out of the repository. Modifying or removing these files has no effect on the repository, which consists mainly of commits. All commits are read-only: no commit can ever be changed, not even by Git itself. So the existing commits, that have existing snapshots, continue to have those snapshots for as long as those commits continue to exist.

What you can do instead is take a clone—any clone will do—and copy all of its commits to new and improved, but different, commits in a new clone. The new clone's commits will contain whatever they contain, since you just made them just now and that was completely under your control. This process, of filtering every commit from some existing repository to produce a new and different (incompatible) repository, should be done exactly once, since every time you do it, it's extremely disruptive: everyone must completely abandon the old repository and clone and use the new repository instead.

There is one program included in Git distributions for doing this: git filter-branch. It is very difficult to use properly and is officially deprecated (a fancy word meaning "do not use this"). The new replacement, git filter-repo, is not yet included in Git distributions, but it's definitely easier to use correctly. There is also something called The BFG (not related to the book/movie); see How to remove/delete a large file from commit history in the Git repository?

To be as clear as possible, the process would be:

  • git clone --mirror as you're doing now, or git clone without --mirror followed by creating local branches for each remote-tracking name, if the filtering tool you have refuses to work on a bare repository;
  • use one of the filtering methods to make a completely new repository;
  • git push the new repository, perhaps also with --mirror as you're doing now, after you're satisfied that the new repository looks correct.
torek
  • 448,244
  • 59
  • 642
  • 775