git - 将东西添加到 git 裸存储库

标签 git git-bare

我知道如果我在裸存储库中有文件,我可以使用 git show HEAD:path/to/file 访问它们。

但是我可以在不克隆和修改工作树的情况下向裸存储库添加新内容吗?

最佳答案

if I add 1 file, only the 1 file is in that commit, aka we added something new and it's now set in stone

有几种方便的方法可以将单个文件提交添加到裸仓库中主分支的尖端。

So it appears i need to create a blob object, attach it to a tree, then attach that tree object to a commit.

提交任何内容的所有方法都归结为这样做,这只是一个方便命令在多大程度上适合您的目的的问题。 git add 创建一个 blob 并在索引中为它创建一个条目; git commit 执行一个 git write-tree 为索引中的内容添加任何新树,以及一个添加提交的 git commit-tree顶层结果树,以及一个 git update-ref 来保持 HEAD 是最新的。 Bare repos 确实有一个 HEAD 提交,通常附加到(也称为符号引用)像 master 这样的分支。 . .

所以 git 的便利命令已经几乎完全按照您的要求进行了。尤其是只有一个文件,这将非常容易。

比如说你的文件出现在 ~server/data/logs/ 中,你用于分发的裸仓库位于 ~server/repo.git,您希望提交的文件位于 repo 协议(protocol)中的 data/logs 中,并且您始终希望提交最新的日志文件:

#!/bin/sh
cd ~server

# supply locations git ordinarily does on its own in working i.e. non-bare repos:

export GIT_DIR=$PWD/repo.git                  # bare repos don't have defaults for these
export GIT_WORK_TREE=$PWD                     # so supply some to suit our purpose
export GIT_INDEX_FILE=$GIT_DIR/scratch-index  # ...

# payload:  commit (only) the latest file in data/logs:

git read-tree --empty                       # make the index all pretty, and 
git add data/logs/`ls -1t data/logs|sed q`  # everything's ordinary from here - add and 
git commit -m'new logfile'                  # commit

git read-tree 从已提交的树中加载索引条目。它是 checkout 、 merge 和重置的基础,可能还有一些我忘记了 atm。在这里,我们只想要一个空索引开始,因此 --empty

use push/pull/remote to synchronize data while using a tool already available on every machine

你说随着时间的推移有“数百万”个文件,如果你不想分发完整的历史记录,据我所知你已经怀疑 rsync 可能是更好的选择。但是——一次一个,每分钟一个新文件,要花两年时间才能积累到一百万。那么,?

无论如何,上述过程可以非常有效地扩展到每次提交的任何少量文件。对于批量工作,有更好的方法。

关于git - 将东西添加到 git 裸存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389897/

相关文章:

git - 可以在 github 中启用 word-diff 选项以查看对一行的更精细更改吗?

git - 我可以通过 http git clone 非裸存储库吗?

git - 使用裸存储库 Hook 将服务器恢复到过去的提交

Git 存储库同步

git - 如何创建仅供查看的 git 克隆

git - 如何撤消主分支的 merge ?

android - 具有相同 GIT 子模块的多个 Android 项目

ios - merge 两个 git 仓库(不小心从 github 下载的 .zip,需要重新 merge 进去)

git merge 裸仓库中的分支

git - 如果文件在特定目录下被修改,则运行预提交 Hook