Git 将某些文件提交到新分支

Git 将某些文件提交到新分支,git,Git,假设我的master分支上有以下4个文件 $ ls README.txt documentation.html main.py docs/main.html $ git branch -a * master remotes/origin/gh-pages remotes/origin/master 问:我应该运行什么命令集来仅将html文件documentation.html和docs/main.html提交到一个单独的分支gh pages,从而销毁现有分支并创建一个包含这些文件的提

假设我的
master
分支上有以下4个文件

$ ls
README.txt documentation.html main.py docs/main.html

$ git branch -a
* master
  remotes/origin/gh-pages
  remotes/origin/master
问:我应该运行什么命令集来仅将html文件
documentation.html
docs/main.html
提交到一个单独的分支
gh pages
,从而销毁现有分支并创建一个包含这些文件的提交?我还想把它们推到远程机器上。是否可以在不运行
git checkout gh页面的情况下执行此操作

我目前的解决方案是使用以下命令

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
在我看来,这并不优雅,但确实有效。我想知道有没有更好的办法?此“脚本”的目的是在ContinuousIntegration服务器上生成文档,并将文档推送到远程存储库上的
gh pages
分支,以利用GitHub页面或GitLab页面。我不关心gh pages分部的历史


想法

要仅添加html文件,只需运行
git add*.html

是否会破坏现有分支并创建一个包含这些文件的提交


我猜您只是关心文件的当前状态,但我不建议这样做。保持历史记录总是有帮助的。但是,在修改gh页面本地分支后,您始终可以执行
git push-f origin gh页面
。这样,您的分支将始终包含一个包含最新更改的提交

是否可以在不运行
git checkout gh页面的情况下执行此操作


嗯,你现在正在做,但它相当冗长。看一看。尽管签出到分支机构也可以避免您在创建本地分支机构后执行
git reset
git clean

git push-f origin gh pages
。或者,如果您不想使用
-f
标志,您可以通过
git push origin:gh pages
首先删除远程分支。您可以使用
git rebase-i
git rebase-i
将分支上的所有现有提交压缩为一个。如果我使用Travis CI或Jenkins,则
git rebase-i
将不起作用。等效的
git-rebase-root
只会将除根提交之外的所有提交合并为一个,在分支上留下两个提交。如果您执行类似于
EDITOR=touch git-rebase-i
的操作,实际上会这样做。对于较新的版本,使用
GIT\u SEQUENCE\u EDITOR
而不是
EDITOR
。这还需要交互吗?您仍然需要告诉git哪些提交需要压缩到上一个提交上,对吗?谢谢您的回答。有一种工具似乎做同样的事情-。但是,它似乎没有将文件列表作为输入,而是处理整个文件夹。