Let's say I have the following 4 files on my master
branch
$ ls
README.txt documentation.html main.py docs/main.html
$ git branch -a
* master
remotes/origin/gh-pages
remotes/origin/master
Q. What are the set of commands I should run to commit ONLY the html files documentation.html
and docs/main.html
to a separate branch gh-pages
in a way such that it destroys the existing branch and creates a single commit containing those files? I would also like to push them to the remote machine. Is it possible to do this without running git checkout gh-pages
?
My current solution for this is to use the following commands
git branch -D gh-pages || echo "Delete local branch failed. It is possible that local branch exists"
git checkout --orphan gh-pages
git rm --cached $(git ls-files) # unstage existing files
# Currently using python add_files.py to do this instead
# subprocess.call('git add {}'.format(html_file)')
find . -name '*.html' | xargs git add
git commit -m "Commit all html files. This commit message does not matter"
git reset $(git commit-tree HEAD^{tree} -m "Generate Sphinx Documentation") # Creates a single commit from current branch
git push --set-upstream origin gh-pages --force
git clean -fxd # Since I unstaged existing files, they will cause conflicts on running the next command if not removed
git checkout master
git pull # Just to make sure master and origin/master are in sync
This is not elegant in my opinion, but it works. I was wondering if there were a better way to do this? The purpose of this "script" is to generate documentation on a ContinuousIntegration server and push the documentation to a gh-pages
branch on the remote repository to take advantage of GitHub pages or GitLab pages. I'm not concerned about the history of the gh-pages branch.
Thoughts?