在Github上查看.docx文件,并在.docx文件格式上使用git diff

r3b*_*00t 7 git github

我有两个问题:

  1. 有没有办法查看.docx文件Github?我们已将所有作业上传到Github,但我们无法在浏览器中查看它.如果我们可以.docx在不下载文件的情况下在浏览器中查看这些文件,那就太好了.

  2. 如何使用git diff的.docx文件格式?我尝试使用catdoc但它对我不起作用.我想我以前曾git diff在Windows上用过这种.doc格式,但它在Mac上并不适用于我.

非常感谢.

小智 7

回答你的第二个问题——

通常当你尝试

git diff filename.docx
Run Code Online (Sandbox Code Playgroud)

你会得到表格的输出 -

二进制文件 a/filename.docx 和 b/filename.docx 不同

不是很有帮助。一个完美的方法是使用Pandoc。

  • 在您的系统上从上面的链接安装 Pandoc。
  • 创建或编辑文件 ~/.gitconfig (linux, Mac) 或 "c:\Documents and Settings\user.gitconfig" (Windows) 以添加(或使用git config --global --edit)

    [diff "pandoc"]
         textconv=pandoc --to=markdown
         prompt = false
    [alias]
         wdiff = diff --word-diff=color --unified=1`
    
    Run Code Online (Sandbox Code Playgroud)
  • 在包含 .docx 文件的 git 控制目录中,创建或编辑文件 .gitattributes(linux、Windows 和 Mac)以添加

    *.docx diff=pandoc
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以提交.gitattributes以便它可以在其他计算机上使用,但是您需要在每台要使用的新计算机中编辑 ~/.gitconfig 。

  • 现在您可以看到一个漂亮的彩色差异,其中包含自上次提交以来对 .docx 文件所做的更改

     git wdiff file.docx
    
    Run Code Online (Sandbox Code Playgroud)

可以在此处找到更多详细信息。


zpa*_*win 7

接受的解决方案(使用 strings / unzip )在 Linux Mint 19.3 上对我来说效果不佳。对于大多数 doc/docx/rtf/xls 文件以及它们的 LibreOffice 对应文件,以下内容似乎工作得很好。其中一些可能通过 cygwin/git bash 在 Windows 上工作,但我没有测试过;如果我提到的包在 cygwin/git bash 中不可用,那么我会寻找执行相同转换的 python/perl 脚本并用它们替换。

  1. 安装先决条件:sudo apt install git pandoc catdoc odt2txt.
  2. 请注意,catdoc 和 odt2txt 包含多个用于处理 doc/xls/ppt/odt/ods/odp 格式的工具,而不仅仅是包名称中的格式。同样,pandoc 处理所有较新的压缩“x”格式。
  3. 我希望我的属性应用为全局(例如用户范围),而不是像其他答案中那样按项目应用。要创建用户范围的 git 属性文件,请使用mkdir ~/.config/git/ && touch ~/.config/git/attributes(在 Windows 上这应该是mkdir "%USERPROFILE%\.config\git" && echo "" > "%USERPROFILE%\.config\git\attributes")
  4. 设置 git 属性文件(上一步中提到的用户范围文件或项目范围的文件${projectDir}/.git/info/attributes):
    # handle windows *.reg files (utf-16 which git doesn't normally like)
    *.reg diff=utf16

    # handle misc common document formats
    *.pdf diff=pdf
    *.rtf diff=catdoc

    # handle libre/open document formats
    *.ods diff=ods2txt
    *.odp diff=odp2txt
    *.odt diff=odt2txt

    # handle older common ms document formats
    # note: ppt did not work for me
    *.doc diff=catdoc
    *.ppt diff=catppt
    *.xls diff=xls2csv

    # handle newer zipped ms document formats
    # note: pptx and xlsx did not work for me
    *.docx diff=pandoc
    *.pptx diff=pandoc
    *.xlsx diff=pandoc
Run Code Online (Sandbox Code Playgroud)
  1. 创建 .gitconfig 定义(在用户范围内~/.gitconfig或在项目范围内${projectDir}/.git/config)。其中大部分内容基于本文,但根据我自己的测试进行了更改。
[core]
        autocrlf = false
    [diff]
        guitool = kdiff3
    [diff "odp2txt"]
        textconv = odp2txt
        binary = true
    [diff "odt2txt"]
        textconv = odt2txt
        binary = true
    [diff "ods2txt"]
        textconv = ods2txt
        binary = true
    [diff "catdoc"]
        textconv = catdoc
        binary = true
    # note catppt did not work for me
    [diff "catppt"]
        textconv = catppt
        binary = true
    [diff "xls2csv"]
        textconv = xls2csv
        binary = true
    [diff "xlsx2csv"]
        textconv = xlsx2csv
        binary = true
    [diff "pandoc"]
        textconv=pandoc --to=markdown
        prompt = false
    [diff "pdf2txt"]
        textconv=pdf2txt
        binary = true
    [diff "utf16"]
        textconv = iconv -c -f UTF-16LE -t ASCII
Run Code Online (Sandbox Code Playgroud)

即使从他们的 github 页面下载了最新版本的 pandoc,我也永远无法成功地获得适用于 xlsx、ppt 或 pptx 的差异。即使使用 Mint/Ubuntu/Debian 存储库中的超旧版本(2016 年的 v1.19.2.4),docx 转换也能正常工作。对于我使用的 xlsx/pptx 样本,我总是收到“无效的 UTF-8 流致命”(旧版本)或“UTF-8 解码错误”(新版本)。

这可能是由于我使用的示例文件(一些来自网络的示例和我通过转换 LibreOffice 文档创建的一些示例)、我的系统设置、我使用的版本或其他原因。

为了完整起见,在安装较新的 pandoc 后,我使用了:

$ uname -vipor
5.3.0-40-generic #32~18.04.1-Ubuntu SMP Mon Feb 3 14:05:59 UTC 2020 x86_64 x86_64 GNU/Linux

$ dpkg -l catdoc odt2txt pandoc git xlsx2csv|grep '^ii'
ii  catdoc         1:0.95-4.1          amd64        text extractor for MS-Office files
ii  git            1:2.17.1-1ubuntu0.5 amd64        fast, scalable, distributed revision control system
ii  odt2txt        0.5-1build2         amd64        simple converter from OpenDocument Text to plain text
ii  pandoc         2.9.2-1             amd64        general markup converter
ii  xlsx2csv       0.20+20161027+git5785081-1 all          convert xslx files to csv format
Run Code Online (Sandbox Code Playgroud)

编辑:还尝试使用该包xlsx2csv进行 xlsx 转换而不是 pandoc,我也遇到了问题。可能与我的样本有关,但由于我并没有真正做任何特别的事情来创建它们,因此我会认为 xlsx2csv/pandoc 的覆盖差距/限制是这样的。


Axe*_*Axe 6

  1. 回答第二部分问题.已经是一个老帖子,但在前10名中没有答案就出现了.通过以下设置,您可以在docx文件上找到一个穷人的差异.

在.gitattributes中使用:

*.docx diff=zip
Run Code Online (Sandbox Code Playgroud)

在.git/config中使用:

[diff "zip"]
      textconv = unzip -c -a
Run Code Online (Sandbox Code Playgroud)

作为奖励我的旧单词/ excel和新单词/ excel的设置:

在.gitattributes中使用:

*.doc diff=word
*.xsl diff=excel
*.xlsx diff=zip
*.docx diff=zip
Run Code Online (Sandbox Code Playgroud)

在.git/config中使用:

[diff "word"] 
    textconv = strings
[diff "excel"]
    textconv = strings
[diff "zip"]
    textconv = unzip -c -a
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 5

有没有办法在 Github 上查看 .docx 文件?

还没有(2016 年第四季度),除非 Word 文档是纯文本。

如何在 .docx 文件格式上使用 git diff?

由于适用于 Windows 1.9.5 的 git 和适用于 Windows 2.5.3的Git(2015 年 9 月,问题 355),您无需进行任何自定义设置:

git diff -- myWord.docx
Run Code Online (Sandbox Code Playgroud)

那可行。(它也适用于.doc和.pdf)

而且由于GIT中的Windows 2.10.1,你可以diff的.docm和.dotm太(见PR 128)。


Aka*_*wal 1

这是有问题的,据我所知,在 github 或任何其他 git 主机上这是不可能的。虽然 git 可用于对任何内容进行版本控制,但像 git diff 这样的东西将以纯文本形式返回两个版本的差异。难以辨认。

不过我觉得这也不是没有原因的。世界上有无限的文件格式,其中许多是专有的。因此,git 不支持像 VLC 这样的每一种格式,而是使用文本文件来处理所有内容。

此外,即使 git 确实以某种方式支持 docx,它也无法在终端内显示格式更改,更不用说 cmd 了。如果只是文本,最好将其存储为文本文件。或者手动签出以前的版本以比较更改。