如果我有一个包含源代码和其他类型文件的git存储库,这样的幻灯片,二进制等等。我希望git只是版本控制文本文件(例如源代码)。对于其他文件类型(例如二进制文件),我只想在git存储库中存储一个副本,即每当我更新它时我都希望git覆盖该文件。我不想为其他类型的文件存储多个版本,因为它们可能非常大。有没有办法为此目的配置git?我运行自己的git主机。
提前致谢!
答案 0 :(得分:3)
add
和commit
您要在单独的提交中将所有文件存储在repo 中。
第一次提交应该是你想要跟踪的文件(即文本文件)。
每次更改要跟踪的文件时,都会像往常一样进行新的提交
每次更改要存储一次的文件时,都会进行新的提交,并rebase
/ fixup
提交添加二进制文件的提交。
这是实践中的想法:
$ ls
banana.pdf bar.bin foo.txt mew.ppt
$ git status -s
?? banana.pdf
?? bar.bin
?? foo.txt
?? mew.ppt
添加文件 - 首先是跟踪所有二进制文件的文件
$ git add foo.txt
$ git commit -m "foo text file"
$ git tag "root" HEAD # easy access to the first commit
$ git add banana.pdf bar.bin mew.ppt
$ git commit -m "binaries"
进行更改和提交
$ echo "ohai, I'm a change :D" >> foo.txt
$ git add foo.txt
$ git commit -m "foo changed"
$ echo "ohai, I'm a banana" | hexdump >> banana.pdf
$ git add banana.pdf
$ git commit -m "fixup! binaries"
让我们看看我们有什么
$ git log --oneline
42c09bd fixup! binaries # this will be merge with
a9b1853 foo changed
8899046 binaries # this - same commit message!
7c8ae05 foo text file # this is also the 'root' tag
现在修改提交以修复二进制文件的提交
$ git rebase --autosquash -i root # everything will be ready for us
pick 8899046 binaries
fixup 42c09bd fixup! binaries # notice this :)
pick a9b1853 foo changed
:wq # vim save and quit
$ git log --oneline # lets look at what we end up with
41e1f09 foo changed
50adb90 binaries
7c8ae05 foo text file
将“非跟踪”文件的新提交标记为非常非常重要
git commit -m "fixup! <same message as the commit to merge with>"
在我们的例子中,添加'banana.pdf'的提交的原始消息是binaries
因此,更改任何二进制文件的提交的提交消息应为fixup! binaries
(如示例中所示)
如果您没有以这种方式命名提交,则--autosquash
无法帮助您,并且您必须在要与其合并的提交下手动移动要更改的提交行,并替换例如,'选择'与'fixup'(从我们离开的地方继续):
$ echo "ohai, more changes" >> bar.bin
$ git add bar.bin
$ git commit -m "changed bar"
$ git log --oneline
bd36eb9 changed bar
41e1f09 foo changed
50adb90 binaries
7c8ae05 foo text file
$ git rebase -i root
pick 50adb90 binaries # you want this to merge with
pick 41e1f09 foo changed
pick bd36eb9 changed bar # this
所以将其改为
pick 50adb90 binaries
fixup bd36eb9 changed bar # under the line to be merged with and with 'fixup' instead of 'pick'
pick 41e1f09 foo changed
:wq # save and quit
$ git log --oneline # what we end up with
9f94cbe foo changed
886eebd binaries
7c8ae05 foo text file
完成:))
如果你想要更进一步,可以使用 git hook 自动执行此操作。见
$ git help githooks
一个pre-commit
钩子会为你做一些技巧
答案 1 :(得分:1)
Git被设计为源代码的最佳选择,并不是二进制文件的最佳选择,但其他VCS系统也不是最佳选择。
一种解决方案是忽略git repo的二进制文件,但为git commit
创建一个别名,它将检测二进制文件更改并替换单独存档中的副本,假设它很重要。
请记住,如果您的二进制文件不经常更改,那么git首先是高效的,不是存储重复文件,而是第二次压缩这些文件。您还可以将文件标记为--assume-unchanged
(我没有使用过,请查看手册),以便忽略短期更改。