Git 选择性地推送已添加和提交的文件
以下是我所做的:Git 选择性地推送已添加和提交的文件,git,Git,以下是我所做的: 克隆了一个git存储库 对文件做了一些更改 创建了一个新分支,并签出了该分支 添加文件,提交更改,然后推送更改 现在,在推的时候,我意识到有太多的文件,因为有些是我不想推的视频文件。所以我按Ctrl+C取消推送 我意识到没有.gitignore文件,所以我被困在这里 $ git status # On branch new-branch nothing to commit, working directory clean 所以,现在我想推送除.mp4视频文件(在许多子文件夹中
$ git status
# On branch new-branch
nothing to commit, working directory clean
所以,现在我想推送除.mp4视频文件(在许多子文件夹中)之外的所有文件,这些文件也已经在git中添加和提交
我该怎么办?您应该首先从索引中删除视频:
git rm --cached path/to/videos/*.mp4
这样就不用处理文件了,它只会将它们从git中删除,就好像它们从未被添加到索引中一样
现在,您应该添加一个.gitignore
文件,将路径添加到视频中,然后将其添加到git:
git add .gitignore
现在您可以commit--amend
修改最近的提交:
git commit --amend
最后推动:
git push origin branchname
顺便说一句,如果您已经按下遥控器,您可以使用几乎相同的命令,只是需要将
-f
(-force
)选项添加到git push
。但你需要确保没有其他人已经从遥控器中拔出,否则你会把事情搞砸。这是你的情况。您错误地添加、提交和推送了一堆mp4
文件。我重新创建了一个类似的场景:
> git add -A
> git status
new file: a.mp4
new file: b.mp4
new file: c.mp4
new file: d.mp4
new file: e.mp4
new file: good-file.txt
new file: good-file1.txt
new file: good-file2.txt
new file: subdir/f.mp4
new file: subdir/subdir/g.mp4
> git commit -m "Add new files."
> git push
mp4
文件现在正在污染远程存储库
请注意,您还推送了一些要保存在遥控器中的txt
文件。要删除mp4
文件,请首先通过添加以下行来更新.gitignore(这假定您要忽略所有mp4文件-否则您必须添加更具体的路径)
然后运行以下git命令。首先,递归地从索引中删除所有mp4
文件。然后,修正你最后的承诺;由于mp4
文件不再在索引中,因此它们将不在修改后的提交中。最后,强制将新的、重写的提交推送到远程上游
> git rm --cached -r *.mp4
rm 'a.mp4'
rm 'a1.mp4'
rm 'a2.mp4'
rm 'bar.mp4'
rm 'foo.mp4'
rm 'subdir/foobar.mp4'
rm 'subdir/subdir/foofoo.mp4'
> git commit --amend -m "Add new files without mp4s"
> git push -f
结果:遥控器不再有您的
mp4
文件警告:如果其他开发人员已经取消了您的第一次提交,他们将不得不从您重写的提交中重新设置基址。使用git update index
可以最容易地执行批量索引操作,如下所示:
git ls-files \*.mp4 | git update-index --force-remove --stdin
修改您的承诺:
git commit --no-edit --amend
推它
git push --force origin # the server might not allow this
你很好。git最终会清理垃圾,忘了它,让它去做它的工作。视频文件是否被推到远程?不,它们没有被推送,因为我在它达到100%之前取消了它。
git push --force origin # the server might not allow this