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.

  • Pre-commit script creates mysqldump file, but “nothing to commit (working directory clean)”?
  • Git clone with username password authentication in one go
  • How can I know what was the latest changes made to a branch?
  • Force git to manual merge even in “obvious” situations
  • how to bundle git with an electron app
  • Has no default translation and R is not being generated
  • 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

  • Convert git repo into a submodule AND move .git directory to superepo
  • One-to-one mapping of git commits to TFS changesets using git-tfs rcheckin
  • How do criss-cross merges arise in Git?
  • Display all first-level descendant branches using Git
  • Difference between `git rm --cached` and `git update-index --assume-unchanged`?
  • What are the “loose objects” that the Git GUI refers to?
  • One Solution collect form web for “How can I commit specific directory to a different than a working branch on git?”

    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.

    Git Baby is a git and github fan, let's start git clone.