简体   繁体   English

为什么“git pull origin master”命令会删除最近的合并提交?

[英]Why does “git pull origin master” command removes the most recent merge commit?

I found that when I try "git pull origin master" command, sometimes the most recent commit is removed.我发现当我尝试“git pull origin master”命令时,有时会删除最近的提交。 It happens when I merge a local branch to the master, then pull from the remote origin.当我将本地分支合并到主分支,然后从远程源拉取时,就会发生这种情况。

Firstly I created a git repository with first commit.首先,我创建了一个首次提交的 git 存储库。 Currently local HEAD and origin/master points to the first commit (up to date).当前本地 HEAD 和 origin/master 指向第一次提交(最新)。

$ git log --oneline --all --graph --decorate
* d42ad4e (HEAD -> master, origin/master) First commit

Then I created a branch (branch name "test"), made a simple change and commit, and merged to the master.然后我创建了一个分支(分支名称“test”),做了一个简单的更改和提交,然后合并到 master。 After then, commit history looks following.之后,提交历史看起来如下。

$ git log --oneline --all --graph --decorate
*   ea13eb8 (HEAD -> master) Merge branch 'test'
|\
| * 9d3969f (test) test
|/
* d42ad4e (origin/master) First commit

As you can see, 9d3969f is the commit I made on test branch, and ea13eb8 is created via "git merge test" command.如您所见,9d3969f 是我在 test 分支上所做的提交,而 ea13eb8 是通过“git merge test”命令创建的。

Then if I use "git pull origin master" command,然后如果我使用“git pull origin master”命令,

$ git pull origin master
From https://github.com/????/????
 * branch            master     -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.
$ git log --oneline --all --graph --decorate
* 9d3969f (HEAD -> master, test) test
* d42ad4e (origin/master) First commit

The change applied as I wanted, but commit ea13eb8 is deleted now.根据我的需要应用了更改,但现在删除了提交 ea13eb8。 I'm just curious what happens to the most recent commit when I pull from the remote origin.我只是好奇当我从远程源拉取时最近的提交会发生什么。

The feedback you are getting from Git might partially reveal what is happening here:你从 Git 得到的反馈可能会部分揭示这里发生的事情:

Successfully rebased and updated refs/heads/master.成功地重新定位并更新了 refs/heads/master。

It appears that your pull strategy is using rebase, rather than a simple merge.看来您的拉取策略正在使用变基,而不是简单的合并。 As a result, when you git pull , a rebase is happening, which can rewrite history and also move around commits.结果,当您git pull ,会发生变基,它可以重写历史记录并移动提交。 Check you .gitconfig file for an entry looking something like this:检查.gitconfig文件中的条目,如下所示:

[branch]
  autosetuprebase = always

You may also run the following to check your Git configuration:您还可以运行以下命令来检查您的 Git 配置:

git config --list

If you don't want the current behavior, then maybe it is time to change your settings.如果您想要当前的行为,那么也许是时候更改您的设置了。 Or, you could explicitly pull via the merge strategy using:或者,您可以使用以下方法通过合并策略显式拉取:

# from master
git fetch origin
git merge origin/master

It seems like you use git pull using rebase .似乎您使用git pull使用rebase

Please refer git rebase has empty commit behaviour请参考git rebase 有空提交行为

By default git has git merge behaviour which adds merge commit.默认情况下,git 具有git merge行为,它添加了合并提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM