git会自动提取文件吗 [英] does git pull atomically write files

查看:88
本文介绍了git会自动提取文件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档中找不到任何内容. 如果我进行git pull,是否可以确保自动生成合并产生的基础文件?

I can't find anything in the documentation. If I do a git pull, am I guaranteed that the underlying file, resulting of the merge, is atomically written?

关于我要实现的目标的更多背景信息: 我有一些定期执行git pull的脚本,我需要知道是否可以依靠在pull期间文件的状态有效.

Some more context about what I am trying to achieve: I have some scripts that periodically do a git pull and I need to know if I can rely on the state of the files being valid during a pull.

我们基本上是使用git作为部署工具.我们从没有设计过合并冲突.在远程端,一个作业每隔x秒不断拉动一次,其他作业则读取文件.可能发生的是,我们在被git拉动文件的同时打开了文件,而文件的内容与我们所期望的不符. 除非git足够聪明,可以在基础操作系统(在这种情况下为RedHat)上使用原子交换

We are basically using git as a deployment tool. We never have merge conflicts by design. On the remote end, a job constantly pulls every x seconds, and other jobs read the files. What could happen is that we open a file while it's being pulled by git, and the contents of the file are not what we are expecting. This is unless git is smart enough to use some atomic swap on the underlying OS (RedHat in this case)

推荐答案

简短的回答是.

值得考虑的是,git pull根本不是关于文件的,它是关于 commits 的.文件只是一个副作用. :-) pull操作只是git fetch(获取提交),然后是第二个Git命令,通常是git merge.合并步骤合并 commits .如果操作不是快进而不是合并,那么这也会合并文件.然后在合并或快进完成后,Git会对结果提交进行git checkout

It's worth considering that git pull isn't about files at all, it's about commits. Files are just a side effect. :-) The pull operation is just git fetch (obtain commits) followed by a second Git command, usually git merge. The merge step merges commits. This has a side effect of merging files as well, if the operation is not a fast-forward instead of a merge; and then when the merge or fast-forward is complete, Git does a git checkout of the resulting commit.

所以这实际上可以归结为: git checkout在操作系统级别是原子的吗?答案是很大声的否:绝不是原子的.使用OS级别的write调用(不是原子的)一次写入一个在工作树中写入的单个文件.一次创建或删除需要创建的文件. Git 确实使用索引(该索引对工作树进行索引(即保持在其上))来最大程度地减少删除,创建或就地重写的文件数量. Git还可以锁定其他 Git 操作,并使Git级别的事务看起来像原子的-但是,在Git外部进行的任何工作(与Git的锁定系统不配合)都可以在发生更改时看到更改

So this really boils down to: Is git checkout atomic at the OS level? The answer is a very loud no: it's not atomic in any way. Individual files written in the work-tree are written one at a time, using OS-level write calls, which are not atomic. Files that need to be created or deleted are done one at a time. Git does use the index, which indexes (i.e., keeps tabs on) the work-tree, to minimize the number of files removed, created, or rewritten-in-place. Git also locks against other Git operations, and makes the Git-level transaction appear atomic—but anything working outside Git, that does not cooperate with Git's locking system, will be able to see the changes as they occur.

这篇关于git会自动提取文件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆