是否将字体、图片等二进制文件放入Git仓库进行版本管理?

在项目开发中,是否将字体文件、图片文件等二进制文件放入 Git 仓库进行版本管理,主要取决于项目的需求和团队的工作流程。以下是一些常见的考虑因素:

1. 版本管理

文本文件(如 HTML、CSS、JavaScript 文件)通常是较小的文件,并且可以清晰地显示差异(diff)。这些文件在 Git 中的管理非常有效,特别是当多人协作时,Git 可以轻松地比较文件的不同版本、合并更改。

二进制文件(如字体文件、图片、音频、视频等)由于它们的特殊格式,Git 无法像文本文件一样处理它们的差异(diff)。Git 只能在二进制级别进行版本管理,这意味着它无法显示更改的细节,只能存储文件的完整副本。

2. 考虑因素

  • 文件大小:二进制文件(特别是图片和字体文件)的文件大小通常比较大。如果将这些文件直接放入 Git 仓库,会导致仓库的体积迅速增加,尤其是如果你有很多版本的图片或字体文件时,Git 会保存每个版本的完整文件副本,这会显著增加仓库的大小。
  • 是否频繁的修改:如果二进制文件经常修改,例如图片或字体的替换,那么它们的版本管理可能变得非常低效,因为 Git 无法存储“差异”而是存储整个文件。如果有大量的二进制文件频繁修改,Git 仓库会迅速膨胀。
  • 性能问题:随着仓库中二进制文件的增加,Git 的性能可能会受到影响,尤其是在进行 git clone、git pull 和其他操作时,文件大小的增长会增加操作的时间和存储需求。

3. 最佳实践

方法一,使用 Git LFS(Git Large File Storage)

如果必须在 Git 仓库中管理二进制文件(如图片、字体等),可以使用 Git LFS。Git LFS 允许你将大文件(例如图片、音频、视频等)存储在专门的存储服务器上,并在 Git 仓库中仅存储指向这些文件的引用。这样可以避免仓库体积过大,提高 Git 操作的性能。

示例:

git lfs install
git lfs track "*.jpg"
git lfs track "*.png"
git lfs track "*.woff"

方法二,将大文件存储在外部服务器或 CDN

对于大图片和字体文件,可以将它们存储在外部服务器、内容分发网络(CDN)或专门的文件存储服务(如 Amazon S3)中,而不是直接存储在 Git 仓库中。然后在前端项目中通过 URL 引用它们。这样,Git 仓库只需管理轻量级的文件(如配置文件或链接文件),而不需要直接存储这些大的二进制文件。

方法三,避免频繁更新二进制文件

如果你的团队在开发过程中不需要频繁更改字体和图片文件,或者这些文件的更改非常有限,放入 Git 仓库中进行版本管理是可以的。但如果这些文件经常被修改,考虑使用 Git LFS 或其他外部存储方案。

4. 总结

  • 可以放入 Git 仓库的情况:如果二进制文件的变化较少,且文件较小,放入 Git 仓库中进行版本管理是可以的,特别是对于项目中使用的字体文件和小图标等。
  • 应避免直接放入 Git 仓库的情况:如果二进制文件较大或频繁修改,最好使用 Git LFS 或将文件存储在外部存储服务中,以避免 Git 仓库的体积迅速膨胀和性能问题。

设置Git使用UTF-8编码

运行git status命令,输出如下信息:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        "m/\345\210\244\346\226\255\347\224\250\346\210\267\345\256\242\346\210\267\347\253\257\346\265\217\350\247\210\345\231\250\350\256\276\345\244\207\347\232\204\347\261\273\345\236\213/"
        "m/\350\247\206\345\217\243/"

说明git不能正常显示中文目录的名字,这个问题通常是由于Git的输出字符编码与终端的编码不匹配导致的。为了解决Git在输出中文目录名称时无法正确显示的问题,你可以通过以下几步进行配置:

1. 设置Git使用UTF-8编码

确保Git提交时使用UTF-8编码:

git config --global i18n.commitencoding utf-8

确保Git输出日志时使用UTF-8编码:

git config --global i18n.logoutputencoding utf-8

关闭路径的引用(即不使用\转义路径中的特殊字符),使路径直接显示:

git config --global core.quotepath off

2. 配置终端编码使用UTF-8编码

如果你使用的是Linux或macOS系统,确保终端的编码设置为UTF-8。你可以检查终端的当前编码,运行以下命令:

echo $LANG

如果输出不是UTF-8,你可以设置终端使用UTF-8编码。修改你的终端配置文件(例如.bashrc或.zshrc),加入以下内容:

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

然后重新加载配置文件:

source ~/.bashrc  # 或者 source ~/.zshrc

3. 确保文件系统支持中文字符

确认你所在的文件系统支持中文字符。大多数现代文件系统(如ext4)都支持UTF-8编码,如果你在不同的文件系统上工作,确保它支持中文路径。

4. 重新运行git status

在配置完成之后,重新运行git status,应该能够正确显示中文目录和文件名。

On the Granularity of Git Commits

When using Git, it’s best practice to commit your code after completing a small logical step—such as implementing one or more functions that achieve a minor feature—or upon finishing a module. This approach makes code tracking and merging much easier.

Avoid committing only after writing a large amount of code. If one small logical step contains an error, rolling back your commit could mean losing significant portions of otherwise correct code.

Similarly, if you make changes to existing code or comments, like fixing a bug, you should commit those changes immediately. Don’t wait until you’ve finished writing a new feature to bundle everything into one large commit.

关于git commit代码的粒度

应该做完一个小的逻辑步骤(例如写完了一个或几个函数实现了一个小功能)就提交一次,完成一个模块就提交一次,这样方便代码回溯,也方便代码合并。不要写了大量代码后再提交一次,万一其中的一个小逻辑步骤的代码写错了,代码回滚后,大量其他正确的代码都白写了。

修改了之前的代码或注释,例如修复一个bug,也应该及时提交一次,而不是继续写完当前小功能的代码之后一起提交。

Setting Up SSH Key Authentication and Communication with GitHub

When connecting to an existing GitHub repository, you can use SSH keys (public and private) for authentication. For instructions on how to create an SSH key and add it to GitHub, refer to the official GitHub documentation: https://docs.github.com/cn/authentication/connecting-to-github-with-ssh.

Key steps to note:

  1. You need to create a .ssh folder in your home directory and copy the SSH private key into it. You do not need to copy the public key into this folder. Instead, paste the contents of your public key into the Settings section of your GitHub account by following the steps here: Adding a new SSH key to your GitHub account.
  2. The file name of your SSH private key should be id_rsa, as this is one of the default names git looks for when locating the private key. If the file is named differently, you will encounter an error such as “[email protected]: Permission denied (publickey).” If this happens, rename your private key file to id_rsa.
  3. If you’re using a Linux-based OS like Ubuntu, ensure that no other users can read or write to the private key file. Run the following commands:
chmod o-rwx ./id_rsa
chmod g-rwx ./id_rsa

After completing the setup, you can use the SSH private key in your .ssh folder for authentication and communication with GitHub. To verify the setup, open Git Bash and run the following command:

ssh -T [email protected]

If you see a message like this:

Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.

It means you’ve successfully set up SSH key authentication with GitHub.

References:

设置通过SSH秘钥与GitHub进行认证和通信

连接GitHub已有的代码仓库时,可以使用SSH秘钥(公私钥)进行认证。如何创建SSH秘钥并将其添加至GitHub,请参考GitHub的帮助文档https://docs.github.com/cn/authentication/connecting-to-github-with-ssh。

需要注意的步骤是:

  • 你需要在家目录里创建.ssh文件夹,把SSH私钥拷贝进去,公钥不用拷贝进去。公钥的内容需要粘贴到你的GitHub账户网站的设置里,请参考https://docs.github.com/cn/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account。
  • SSH私钥的文件名应该是id_rsa,因为id_rsa是git默认查找的私钥名字之一,否则就找不到私钥,会报错“[email protected]: Permission denied (publickey).”。如果你的SSH私钥的文件名不是id_rsa,那么应该改名为id_rsa。
  • 如果你使用的是Ubuntu等Linux操作系统,私钥文件不能有被其他人读写的权限:
chmod o-rwx ./id_rsa
chmod g-rwx ./id_rsa

完成设置后,就可以用家目录里的.ssh文件夹里的SSH私钥与GitHub进行认证和通信了。打开Git Bash,执行命令:

ssh -T [email protected]

如果输出以下信息:

Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.

说明通过SSH秘钥与GitHub进行认证和通信设置成功。

参考

https://docs.github.com/cn/authentication/connecting-to-github-with-ssh https://docs.github.com/en/authentication/troubleshooting-ssh/error-permission-denied-publickey