87

git pull --dry-run弄乱我的工作树之前,是否可以查看如何合并内容?

现在我正在做:

git fetch origin && git merge --no-commit --no-ff

我在手册页中没有看到任何与它相关的“git-pull”。

澄清一下,我只需要在 Ant 脚本中部署它,以查看执行时是否存在冲突git pull,然后退出构建,失败部署并保持该目录树与之前相同git pull

4

9 回答 9

72

如果合并失败,我总是依靠 Git 的固有能力来让我回来。

要估计合并可能如何发生,您可以像这样开始:

$ git fetch origin branch  # Fetch changes, but don't merge
$ git diff HEAD..origin/branch # Diff your current head to the fetched commit

... personal judgement of potential merge conflicts ...

$ git merge origin/branch # merge with the fetched commit

如果事情没有按计划进行,请查看您的reflog并重置回您想要的状态:

$ git reflog
...
abc987  HEAD@{0}: merge activity
b58aae8 HEAD@{1}: fetch origin/branch
8f3a362 HEAD@{2}: activity before the fetch
...
$ git reset --hard HEAD{2}
于 2013-06-20T19:53:00.410 回答
40

您需要先获取以更新您的本地源/主

git fetch origin

然后你可以这样做:

git diff --name-only origin/master

将列出已更改的文件。

git diff origin/master directory_foo/file_bar.m

将逐行列出文件 directory_foo/file_bar.m 的差异。

于 2013-06-20T20:07:22.850 回答
20

您可以通过从当前分支创建一个新的一次性分支并在git pull那里执行来获得所需的效果。如果您对结果不满意,则原始分支完好无损。

于 2013-06-20T19:50:51.263 回答
15
# fetch new commits from origin
$ git fetch

# check what are the differences and judge if safe to apply
$ git diff origin/master

# actually merge the fetched commits 
$ git pull
于 2016-12-08T12:22:02.937 回答
9

从 v2.27.0 开始有一个试运行标志

于 2021-01-22T14:14:38.547 回答
4

git merge --abort由于拉动意味着合并,如果您的脚本检测到有任何冲突并且合并失败,我会继续运行。

于 2013-06-21T08:44:19.323 回答
1

在这个类似的问题中查看我的答案:

如何在不获取的情况下预览 git-pull?

这转到~/.gitconfig文件:

[alias]
        diffpull=!git fetch && git diff HEAD..@{u}
于 2018-05-14T13:00:48.953 回答
1

OliverE 很准确: git pull有一个dry-run选项,所以我建议git pull --dry-run -v实现 OP 的目的——简单直接。 pull并不总是有试运行选项,但在以前(和当前)版本的 git 中,fetch确实(并且确实)有试运行选项。因此,另一种方法是git fetch --dry-run -v在你拉之前做一个。在执行之前检查一个动作总是比花时间还原要好。

于 2022-02-04T21:33:54.080 回答
0

不拉动:

[ "$(git fetch;git diff | wc -l)" != "0" ] && (
echo there are updates
echo do your stuff here
)

或不碰任何东西:

[ "$(git pull --dry-run | wc -l)" != "0" ] && (
echo there are updates
echo do your stuff here
)
于 2021-08-12T23:09:54.587 回答