【发布时间】:2012-04-09 00:02:12
【问题描述】:
假设我有一个 git repo,其工作树和/或索引是“脏的”(即我有尚未提交或隐藏的本地修改)并且我很想在没有先提交的情况下执行“git pull”或藏匿。 (或者,假设我正在向 git 介绍一个新的团队成员,他们当他们的本地仓库“脏”时很想运行“git pull”。)我'我想知道在这种情况下执行“git pull”有多安全。如果不安全,可能发生的最糟糕的事情是什么?我是从受信任的来源还是从不受信任的来源中提取内容有关系吗?
到目前为止,我的调查提出了一系列令人困惑的想法,我认为这是一个相当简单的问题。
首先,git-stash 手册页让 git pull 听起来非常安全,如果您处于可能出现问题的情况下,它会中止:
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...
到目前为止,在我的简单测试中,这似乎也发生了。
也可能暗示“git pull”非常安全,Visual Studio 的Git Extensions 工具栏有一个显眼的拉动按钮,它在向“git pull”发射之前不会检查是否脏污。 (如果我正在设计一个 Visual Studio 工具栏,我会尽量避免让自己特别容易被人踩到脚。)
git-pull 手册页不会让我的“git pull”听起来很危险,尽管它表明这不是最佳实践:
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).
但是你也可以找到建议,拉入肮脏是非常糟糕的,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
对于哪种观点最好有一个简单的答案,或者这是否是一个个案?
跟进:使用“git pull --rebase”而不仅仅是“git pull”会改变答案吗?在某些情况下,rebase 可能有它的 own pitfalls,但到目前为止,我的猜测是,拥有一个肮脏的工作树/索引不会使 rebase 比其他情况更成问题。
【问题讨论】: