Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Git to exclude a local directory from PUSH to remote, but still part of local repo

Tags:

git

Using git, I would like to be able to keep a certain folder as part of my local project (feature it in branches, etc) yet I don't want this directory getting pushed up to the public git server.

The folder in question has some CLI Java stuff that the project does not need, but I would like to keep in in sync with the branch work that will go live.

Is this possible?

like image 646
Chris Legge Avatar asked Jun 15 '12 21:06

Chris Legge


2 Answers

A very short answer is no. Here is why: When your local git branch is pushed to a server, it's pushed as is without any exceptions. As a commit contains files, it would mess up git internally if some files would not be pushed. This kind of selective behaviour is not supported and I guess it'll never be, as most of the time you are using git incorrectly when you need such stuff.

There are, however, many ways:

  1. The first one is simply adding the stuff to the repo. You are obviously needing it and you want it to be version controlled, so from my point of view that's all that is needed to add files to git.
  2. You could try git-submodules, but this would also add files to your repo. Your java cli would be another repository and your original repo would track the versions.
  3. You could tell git to ignore the subfolder, than create your java-cli repository there and just the two without any interaction.

I would definitly go with 1 because when I need something for the project, then it belongs to the repo, even if it's java cli.

like image 180
Sgoettschkes Avatar answered Sep 30 '22 12:09

Sgoettschkes


If you don't need to track those files at all...

... you can make git ignore them.

If you want to track them locally...

... but not have them in the remote repo I can only give you some hints/ideas to thing I never tried, but you will probably end up having different repositories and things might get a little nasty.

I would have a look at (from maybe ok to probably quite messy and a bad idea):

  • Nested repositories
    • Subtrees, Submodules, git-repo, git-subrepo or similar
    • Nest repositories manually: Make git ignore the subfolder, set up an own repository, make sure commits somehow align
  • Remove on push: (Drawback: You probably won't be able to properly diff as histories only match for the temporary repository. You could of course somehow also hack your diffing, making the setup even messier.)
    • Steps:
      • Before pushing clone the repository to a temporary folder
      • Remove your subfolder from the entire history inside the newly created clone (getting another repository with different commit hashes)
      • Push the newly created repository
    • Options: Trigger:
      • pre-push Hook
      • Manually do the above instead of using git push
    • Options: Removal:
      • Tool like BFG-Repo-Cleaner
      • git-filter-branch by hand

Again: I didn't use either of them and don't know their drawbacks, pitfalls etc. but to me it seems subtrees are by far the best solution from the above.

Most of the rest does seem quite messy to me but at the end of the day the only thing that matters is what works for you.

Ship lousy stuff, but ship.

...be it in Wasabi

like image 26
sopupe Avatar answered Sep 30 '22 12:09

sopupe