Jacob Buller
Jacob Buller

Reputation: 137

How to push only files inside of a folder using gitignore

I was wondering how do I can get git to ignore the actual folder but track and push the files within the folder itself to github?

I have a github repository I'd like to use as a test site, but when I push my code it's always in the www folder, which screws it up with I use Github Pages. I'm using Vapid CMS and it puts all the compiled html files in a www subfolder. How can I just push the content within that folder and ignore the parent folder itself using gitignore? I'm using GitKraken to track and push my files.

Here's my repository -https://github.com/jcbbuller/bigsheep.design/tree/master

Thanks!

Upvotes: 0

Views: 1485

Answers (3)

jthill
jthill

Reputation: 60423

Commits are small objects referring to trees in the repo, so all you need is commits directly referring to the trees at www in your main history.

There are lots and lots of ways to do this, perhaps the simplest is to just refresh the whole thing every time.

git branch -f pages master
git filter-branch --subdirectory-filter www pages -- www

which, while it regenerates already-existing (previously-generated) commits every time, doesn't actually add anything redundant, and it's so simple that it's likely to perform well enough and stay easiest for quite a while, until you're into commit histories in the thousands. A fully incremental version that runs in a few milliseconds will get easy to build and understand, something like

#!/bin/sh
# post-commit and post-merge hook
[[ `git rev-parse -q --verify pages` != `git rev-parse -q --verify master:www ]] && {
        pages=`git rev-parse pages`
        git update-ref refs/heads/pages $(
                git commit-tree ${pages:+-p pages} -m "built pages" master:www
        )
}

but this is just a more-efficient way of producing the same effect.

Upvotes: 0

torek
torek

Reputation: 489093

Git does not push files. Git pushes commits.

Now, it's true that commits contain files, so you can, if you like, think of this as "pushing files". The problem is that when you do think of it like this, you end up in your situation, not knowing what to do. :-) So it's better to think of Git as pushing commits. Your question now becomes:

When I run git push, I keep pushing commits with files named www/file1, www/file2, and so on. I'd like to push commits that have files named file1, file2, and so on.

Now the answer is obvious: make commits that contain files named file1, file2, and so on. To do that, take some existing commit that has the files inside the www folder, and tell Git to rename them all so that they don't use the www name any more:

$ cd www
$ git mv * ..

for instance.

The obvious drawback is now you're interspersing commits that have all the files named as www/file1, www/file2, and so on, with other commits that have no www at all and have all the files named file1, file2, and so on. There are two obvious approaches for handling this:

  • put the commits that have the files renamed into a different branch, so that the commits with the two different naming scheme aren't interleaved, or
  • as Nitsan Avni suggested, put the commits that have the files renamed like this into a separate repository.

There are more ways—you can invent your own, if you like—but they all boil down to the fact that if some software insists on seeing commits with files named A B C and so on, and your existing commits have the files named folderX/A folderX/B folderX/C, you need to make new and different commits that have the files with the names the software demands. Which commits, if any, are "better" or "more master-ful" ... well, that part is up to you. As far as Git is concerned, they are just different commits. If all of the software you're using doesn't mind putting all these different commits into a single repository, you can use a single repository to hold them. If some of the software does mind, you'll need multiple repositories to hold them—or to fix that software.

Upvotes: 0

Nitsan Avni
Nitsan Avni

Reputation: 841

instead of using .gitignore I propose to create a new repo inside the www subfolder, using:

cd www
git init
git remote add origin [email protected]:jcbbuller/bigsheep.design.git
git add .
git commit -m 'initial commit'
git push -u -f origin HEAD

Upvotes: 2

Related Questions