How to merge contents of untracked directory to root of other branch
For my GitHub pages I use a static site generator. The source of the site lives in the <source> branch and it generates the html files in the _site directory. The <master> branch is used to show the actual website and should contain the generated html files at the root level.
Since I don't want the _site directory and it's contents to be part of my <source> branch I've ignored the _site directory (by putting _site in my .gitignore).
Now the question I have is how can I merge the untracked contents of the _site directory of the <source> branch to the root of the <master> branch?
Update The main problem seems to that Git needs anything you want to merge to be tracked (rightfully so ;)), but I don't want to track the generated files in my <source> branch. The only workarounds I can come up with use at least 1 temporary branch that can be deleted afterwards. Using git subtree split from a temporary branch into another temporary branch seems to be the cleanest option.
Is there a better/cleaner way to do this?
This is what I came up with:
# Checkout branch <temp1> carrying over the current working # directory including ignored/untracked files git checkout -b temp1 # Add _site to staging ignoring .gitignore and commit git add -f _site/ git commit -m "irrelevant commitmessage" # Subtree split _site to <temp2> branch (root of branch) git subtree --prefix=_site/ split -b temp2 # Checkout master and merge <temp2> git checkout master git merge --squash temp2 git commit -m "commitmessage" # Remove <temp1> and <temp2> branches and push
If there's better solutions to this problem, I'll welcome them, but given that you want to do a kind of merge, which is really not one, I can only suggest you create a post-commit script or a bash/perl/python script that does the following:
- if not on "source" branch (could be any condition) quit
- create another branch like temp001
- on that branch copy all the contents
- copy from _site to root
- checkout master merge temp001
- quit (my advice)
Now confirm your changes and commit/push. You can do this on the last step but I wouldn't do so in the first try runs.
This is the best I can suggest.