如何在运行 “git pull” 时避免 Git 创建合并?

huangapple go评论57阅读模式
英文:

How to avoid git from creating a merge when running "git pull"?

问题

在Git中,我刚刚执行了以下操作:

git pull

以获取其他人可能在我所在的远程分支上进行的任何可能的更改,并且git pull创建了一个自动合并,我不得不使用以下命令进行还原:

git reset --hard <commit-just-before>

在执行git pull时,有没有一种方法可以避免这种自动合并?

可能我在做事情方面完全错误,因为我是Git的初学者,对于这些上下文中使用的术语并不熟悉。

英文:

In git I just did a

git pull

to get any possible changes that someone else might have been made on the remote branch I am on, and that git pull created an automated merge I had to revert with

git reset --hard &lt;commit-just-before&gt;

Is there a way to avoid this automatic merge when doing git pull?

There is the possibility I am doing things completely wrong, as I am a beginner with git and I am not familiar with the nomenclature used in these context.

答案1

得分: 1

你可以检查你的远程分支是否包含新的提交:

git fetch
git log ..origin

添加 -p 以将代码更改包括在提交日志中:

git fetch
git log ..origin -p

如果你想查看没有提交的更改:

git fetch
git diff ...origin

但如果你无论如何想要合并而不提交:

git fetch
git merge --no-ff --no-commit
自动合并成功;按照要求在提交之前停止

在此之后清除索引:

git reset --hard
英文:

You could check whether your remote branch contains new commits:

git fetch
git log ..origin

Add -p to include code changes into the commit log:

git fetch
git log ..origin -p

If you want to see changes without commits:

git fetch
git diff ...origin

But if you want to merge without a commit anyway:

git fetch
git merge --no-ff --no-commit
Automatic merge went well; stopped before committing as requested

To clean the index after this:

git reset --hard

答案2

得分: 1

在当前版本的 Git 中,你可以像这样调整 Git 的配置:

git config --global pull.ff only

然后,当你运行 git pull 时,它会中止尝试合并,并显示以下消息:

fatal: Not possible to fast-forward, aborting.

如果你想要覆盖这个设置,仍然执行拉取和合并操作,你可以使用以下命令:

git pull --no-ff

参考文档:https://git-scm.com/docs/git-config#Documentation/git-config.txt-pullff

版本要求:我认为是 2.1+,你可以使用 git --version 来检查版本。

英文:

In current versions¹ of git you can adjust git's config like this:

git config --global pull.ff only

Then git pull will abort instead of attempting a merge, with the following message:

fatal: Not possible to fast-forward, aborting.

If you ever want to override this and pull+merge anyway, you can do that with

git pull --no-ff

See also: https://git-scm.com/docs/git-config#Documentation/git-config.txt-pullff

¹ Version 2.1+ I think, check git --version

huangapple
  • 本文由 发表于 2023年7月20日 19:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定