0

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?

kdheepak
  • 1,274
  • 11
  • 22
  • `git push -f origin gh-pages` after you create the local branch. Or you can just `git push origin :gh-pages` to delete the remote branch first if you don't want the `-f` flag. – Mad Physicist Jul 11 '16 at 17:00
  • You can squash all existing commits into one on a branch with `git rebase -i` – Mad Physicist Jul 11 '16 at 17:02
  • `git rebase -i` will not work if I'm using Travis-CI or Jenkins. The equivalent `git rebase --root` will only merge all commits except the root commit into one, leaving two commits on the branch. – kdheepak Jul 12 '16 at 19:23
  • Acutally it will if you do something like `EDITOR=touch git rebase -i`. Use `GIT_SEQUENCE_EDITOR` instead of `EDITOR` for newer versions. – Mad Physicist Jul 12 '16 at 19:27
  • Wouldn't that still require interaction? You still need to tell git which commits need to be squashed onto the previous commit, right? – kdheepak Jul 12 '16 at 19:30
  • Nope. See here: http://stackoverflow.com/q/25938711/2988730 – Mad Physicist Jul 12 '16 at 19:31
  • Oh. I see. But then how do you expect to do this on Jenkins to begin with? – Mad Physicist Jul 12 '16 at 19:32
  • The best you can do is to write a script that can figure out which commits to mark for squashing. – Mad Physicist Jul 12 '16 at 19:32
  • The way I've described it currently in the question works on Jenkins. I was just curious if there was a more terse way of doing it. For example, [ghp_import](https://github.com/davisp/ghp-import/blob/master/ghp_import.py) seems to be doing something similar but it works on an entire folder. – kdheepak Jul 15 '16 at 15:12

1 Answers1

-1

For adding only html files, you can simply run git add *.html.

in a way such that it destroys the existing branch and creates a single commit containing those files?

By this I guess you are just concerned with the current state of your files, but I won't suggest doing that. Maintaining a history is always helpful. However, you can always do git push -f origin gh-pages after amending gh-pages branch locally. In this way your branch will always be containing a single commit with your most recent changes.

Is it possible to do this without running git checkout gh-pages?

Well, you are currently doing it but its quite verbose. Give this answer a look. Although checking out to the branch will also save you from doing git reset and git clean as you are doing in your current approach.

Community
  • 1
  • 1
yellowflash
  • 65
  • 2
  • 9
  • Thanks for that answer. There is a tool that seemingly does the same thing - [ghp_import](https://github.com/davisp/ghp-import/blob/master/ghp_import.py). However it does not seem to take a list of files as an input and appears to work on an entire folder instead. – kdheepak Jul 12 '16 at 19:28