“git pull”安全吗?当我的工作树和/或索引很脏? [英] Is it safe to "git pull" when my working tree and/or index is dirty?

查看:184
本文介绍了“git pull”安全吗?当我的工作树和/或索引很脏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个git repo,其工作树和/或索引是脏的(即,我有本地修改还没有被提交或隐藏),我很想在没有第一次提交的情况下做一个git pull或存储。 (或者,假设我正在引入一个新的团队成员来git,当他们的本地回购是脏的时,他们试图运行git pull)。我想知道在这种情况下做一个git pull是多么安全。如果不安全,可能发生的最糟糕的事情是什么?如果我从一个可信赖的来源或不可信任的来源获取信息,那它有什么关系吗?



到目前为止,我的调查显示出一个令人困惑的想法,直接的问题。



首先,git-stash联机帮助文件使得它听起来像git pull是非常安全的,如果您处于某种情况出错:

 拉入脏树
当你处于某种东西的中间时,你会发现有
可能与您正在做的事情相关的上游更改。
当您的本地更改与
上游的更改没有冲突时,一个简单的git pull会让您前进。

但是,在某些情况下,您的本地更改与上游更改冲突
,并且git pull拒绝覆盖您的
更改。在这种情况下,您可以隐藏您的更改,执行
拉,然后取消摆放,就像这样...

在我的简单测试中,这似乎也是发生了什么。

也许暗示git pull非常安全,Visual Studio的 Git Extensions 工具栏有一个突出的拉按钮,不会执行检查掏出之前的肮脏,以git拉。 (如果我设计的是Visual Studio工具栏,我会尽量避免让自己在脚下拍摄变得特别容易。)



git-pull手册页没有尽管它表明它不是最佳实践:

 如果任何远程更改与本地未提交的更改,
合并将自动取消并且工作树不受影响。
通常最好在
拉动或用git-stash(1)将它们存储起来之前在工作顺序中进行任何本地更改。

但是,你也可以找到一些建议,说明肮脏是非常糟糕的, eg

 避免git-pull! 
git-pull应该永远不会被调用,如果你有肮脏的文件躺在周围,或者如果你的分支领先于主。
这总是会导致提交历史中的一些脏文物

有没有简单的答案哪个视角最好,或者这是某种情况下的个案?



后续:会使用git pull --rebase 而不仅仅是混帐改变答案呢?重新激活可能会有 在某些情况下>陷阱,但到目前为止,我的猜测是有一个肮脏的工作树/ index>不会使rebase成为比其他情况更麻烦的问题。

解决方案

维护Apache DeltaSpike的人没有冒犯项目,但我相信Git手册页上关于Git关于Delta Spike wiki的内容的看法。

另外请注意,在引用的文本中(强调我的) :如果你有肮脏的文件躺在附近,应该永远不会调用
$ b


git-pull 或者您的分支领先于主。这将永远导致提交历史中的一些脏文物。


提交历史记录中的脏文物是Git合并提交。这发生在你合并分支的任何时候,而不是重新分配另一分支的内容。无论这些合并提交是否为肮脏取决于您的项目,您的组织以及它的意见和政策。



无论如何, git-拉从不是一个危险的操作。据记载,它不会破坏您的任何历史记录或正在进行的工作。


Say I have a git repo whose working tree and/or index is "dirty" (i.e. I have local modifications that have not yet been committed or stashed) and I'm tempted to do a "git pull" without first committing or stashing. (Alternatively, say I'm introducing a new team member to git and they are tempted to run "git pull" when their local repo is "dirty".) I'm wondering how safe it is to do a "git pull" in this case. If it's unsafe, what's the worst thing that can happen? Does it matter if I'm pulling from a trusted vs untrusted source?

My investigation so far suggests a confusing range of ideas on what I'd assumed would be a fairly straightforward question.

To start with, the git-stash manpage makes it sound like git pull is pretty safe, and will abort if you're in a situation where something might go wrong:

   Pulling into a dirty tree
       When you are in the middle of something, you learn that there are
       upstream changes that are possibly relevant to what you are doing.
       When your local changes do not conflict with the changes in the
       upstream, a simple git pull will let you move forward.

       However, there are cases in which your local changes do conflict
       with the upstream changes, and git pull refuses to overwrite your
       changes. In such a case, you can stash your changes away, perform a
       pull, and then unstash, like this...

This actually seems to be what happens too, in my simple tests so far.

Also perhaps implying that "git pull" is pretty safe, the Git Extensions toolbar for Visual Studio has a prominent pull button that does not do a check for dirtiness before shelling out to "git pull". (If I were designing a Visual Studio toolbar, I would try to avoid making it particularly easy to shoot yourself in the foot.)

The git-pull manpage doesn't make my "git pull" sound dangerous, though it suggests it's not a best practice:

   If any of the remote changes overlap with local uncommitted changes,
   the merge will be automatically cancelled and the work tree untouched.
   It is generally best to get any local changes in working order before
   pulling or stash them away with git-stash(1).

But then you can also find advice that pulling into a dirty is very bad, e.g.:

Avoid git-pull!
git-pull should never get invoked if you have dirty files lying around or if your branch is ahead of master.
This will always lead to some dirty artifacts in the commit history

Is there an easy answer to which perspective is best, or is this somehow a case-by-case thing?

Followup: Would using "git pull --rebase" rather than just "git pull" change the answer at all? Rebasing may have its own pitfalls in some cases, but so far my guess is that having a dirty working tree/index wouldn't make a rebase more problematic than it otherwise would be.

解决方案

No offense to the people maintaining the Apache DeltaSpike project, but I'd trust what the Git man pages say about Git over the contents of the Delta Spike wiki.

Also note that in the quoted text (emphasis mine):

git-pull should never get invoked if you have dirty files lying around or if your branch is ahead of master. This will always lead to some dirty artifacts in the commit history.

The "dirty artifacts in the commit history" are Git merge commits. This happens any time you merge branches instead of rebasing the contents of one branch on the other. Whether or not these merge commits are "dirty" depends on your project, your organization and its opinion and policies.

In any case, git-pull is never a dangerous operation. As documented, it will not destroy any of your history or work in progress.

这篇关于“git pull”安全吗?当我的工作树和/或索引很脏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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