Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

github - How can I commit specific directory to a different than a working branch on git?

I have a simple static website, based on HTML 5 boilerplate and the whole thing is in a github repository. The project structure is something like this

build
css    
img
js
publish    # this directory isn't in source control!

...

index.html & other files

Basically what I do is run a build script (from HTML 5 boilerplate) that compiles all the HTML/CSS/JS into the publish directory, which isn't in the git repository.

Now what I want to do, is make use of GitHub pages and be able to use the publish directory output as a GitHub page.

The way GitHub pages work, is that you create a clean separate branch named gh-pages, which will contain the final content. What I want to do, is to be able to on demand commit the current publish directory into the gh-pages branch, but I also want to keep it in the main .gitignore file so it won't get pushed into the source repository.

I want to do this to kinda preview the current state of the project.

in short: I need to commit one directory to a separate branch, so it's root will be the same as contents of that one directory. publish/css will become just css on the gh-pages branch

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since .gitignore is versioned as well, just don't put the publish/ directory in the gh-pages branch's .gitignore file. Then, you can just do something like this:

ORIG_HEAD="$(git name-rev --name-only HEAD)" git checkout gh-pages && mv publish/* . && git commit -am "automatic update" && git checkout "$ORIG_HEAD"

It does get tricky since you're wanting to do things in the root directory rather than a subdirectory. Another option would be to simply maintain a separate clone of the same repository in a different directory, that is always on the gh-pages branch. You could have your script just write the files to there instead of to publish/ and then just commit in that clone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...