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.

  • SVN -> Git migration with structure variations
  • How do I compress multiple merges into a single merge?
  • How to remove files from the GitHub repository?
  • Has anybody seen git create a commit that has no date & an all-zero hash?
  • How to undo a git pull?
  • How to edit the contributor's commit in a pull request in GitHub?
  • 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

  • git 'merge' only existing files
  • How to list conflicted files (files with changes in both parents) in a git merge commit?
  • Workflow Git FTP for old projects with potential external non-git users
  • A file was added to git on commit n. How do I add it instead to commit n-m?
  • git pull while not in a git directory
  • git rerere manual resolution only
  • 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.