git:推送单个提交,使用rebase重新排序,重复提交 [英] git: Pushing Single Commits, Reordering with rebase, Duplicate Commits

查看:1323
本文介绍了git:推送单个提交,使用rebase重新排序,重复提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想推几个单一的提交到git远程回购。我遵循Geoff在这里找到的答案:

I want to push several single commits to a git remote repo. I followed Geoff's answer found here to do so:

如何推送特定的提交到远程,而不是以前的提交?

我想推的提交不在头,所以我必须首先使用rebase重新排序提交,并且我使用这些指令:

The commits I want to push are not at the head, so I have to reorder the commits using rebase first and I used these instructions to do so:

http://gitready.com/advanced/2009/03/20/reorder-commits-with-rebase.html

本质上我做了:

git clone
git commit
git commit
...
git pull
git rebase -i HEAD~3
git push origin <SHA>:master

我做错了这个错误。所以我开始深入研究这个问题。我发现在我的日志中有重复的提交,如果我在重新制作后再次执行第二个git pull,例如:

I got errors doing this. So I started looking deeper into the problem. I found that there are duplicate commits in my log if I do a second git pull after rebasing, for example:

git clone
git commit
git commit
...
git pull
git log --pretty=format:"%h - %an : %s" // log before rebasing
git rebase -i HEAD~3
git pull
git log --pretty=format:"%h - %an : %s" // log after rebasing
git pull 
git log --pretty=format:"%h - %an : %s" // log after rebasing after pulling

所以我发布了这个问题:

So I posted this question:

git:本地Rebase之后的重复提交后接拉

Roger的回应导致我对于这个问题:为什么在重新引导和拉取后会看到重复的提交?

Roger's response there led me to this question: Why do I see duplicate commits after rebasing and pulling?

从上面来看,重新生成之前的日志如下所示:

From above, the log before rebasing looks like:

84e4015 - Me : Local Commit 3
0dbe86a - Me : Local Commit 2
d57ba2a - Me : Merge branch 'master' of remote repository
a86ea35 - Me : Local Commit 1 before reordering
2fc4fe7 - Remote User 2 : Remote Commit 2
b7a8656 - Remote User 1 : Remote Commit 1
8ce80fc - Me : Merge branch 'master' of remote repository

并且重新生成之后的日志如下所示:

And the log after rebasing looks like:

cf1ff7b - Me : Local Commit 3
cd14463 - Me : Local Commit 2
b9d44fb - Me : Local Commit 1 after reordering
9777c56 - Remote User 2 : Remote Commit 2
a2d7d8b - Remote User 1 : Remote Commit 1
8ce80fc - Me : Merge branch 'master' of remote repository

9777c56和a2d7d8b。我相信这是问题的开始。

Notice that the original 2 commits 2fc4fe7 and b7a8656 have new SHAs; 9777c56 and a2d7d8b. I believe this is the start of the problem.

现在,我做了另一个git拉,日志看起来像这样:

Now after I do another git pull the log looks like this:

e8e1a85 - Me : Merge branch 'master' of remote repository
cf1ff7b - Me : Local Commit 3
cd14463 - Me : Local Commit 2
b9d44fb - Me : Local Commit 1 after reordering
9777c56 - Remote User 2 : Remote Commit 2
a2d7d8b - Remote User 1 : Remote Commit 1
2fc4fe7 - Remote User 2 : Remote Commit 2 // duplicate 2
b7a8656 - Remote User 1 : Remote Commit 1 // duplicate 1
8ce80fc - Me : Merge branch 'master' of remote repository

请注意,远程提交现在已被重复,远程提交的原始SHA已经返回.2 / c4fe7和b7a8656已返回。

Notice that the remote commits are now duplicated, and the original SHAs of the remote commits, 2fc4fe7 and b7a8656, have returned.

在Roger的回应中,他说它看起来像其他人的错误,推动了git,并且他们正在重新推行已经推出的承诺。但我相信这是我的错误,在本地重新推出一个承诺。

In Roger's response he said that it looked like the fault of other people pushing to git and that they were rebasing their already pushed commits. But I believe it is my fault for locally rebasing a pushed commit.

这是因为我重新提交已经被推送到远程的提交?如果是这样,我应该怎么做才能避免这种情况呢?我需要修改我的提交,以便我可以单个提交。我应该使用分支系统来做到吗?如果是这样,我如何使用分支来解决这个问题?

Is this because I rebased a commit that had already been pushed to the remote? If so, what should I have done to avoid this? I need to rebase my commits so that I can push a single commit. Should I have used a branching system to do this? If so how would I use branches to solve this problem?

推荐答案

简短的答案是rebase不会更改提交, 1 ,而是复制它们。 Git通常会隐藏原件,但如果您的原件包括其他用户共享的原件,则您(当然也可以使用),仍然会看到这些原件。

The short answer is that rebase does not change commits,1 but rather copies them. Git normally then hides away the originals, but if your originals include those shared by other users, you (and of course they) and still see those originals.

规则,你应该只修改自己的私人,未发布的提交。由于没有一个其他可以按照定义复制这些内容,所以您自己制作副本,然后(通过rebase)隐藏原件的事实不是问题:您现在可以看到副本而不是您的副本原件,也没有人看到,如果需要,您可以继续退换货。一旦你发布(通过 push 或类似的)提交,尽管如此,你不能再改变它,因为其他人现在有一个你的原件的副本,包括它的SHA -1 ID,他们还会继续。

As a general rule, you should only rebase your own private, unpublished commits. Since no one else has a copy of these by definition, the fact that you make your own copies and then (via rebase) hide away your originals is not a problem: you now see your copies instead of your originals, and no one else sees either, and you can continue to rebase if needed. As soon as you publish (via push or similar) a commit, though, you can no longer change it, because someone else now has a copy of your original, including its SHA-1 ID, and they will still have it later.

在这种情况下你所做的是重新(即复制)他们的承诺以及您自己的。问题的一部分源于使用 git pull ,这意味着fetch then merge,当你想要的是fetch then rebase。您可以分别执行步骤:

What you've done in this case is to rebase (i.e., copy) their commits as well as your own. Part of the problem stems from using git pull, which means "fetch then merge", when what you wanted was "fetch then rebase". You can either do the steps separately:

git fetch
git rebase

或使用 git pull --rebase

git pull --rebase

pull 脚本,在执行提取后,它应该做一个rebase而不是一个合并。您也可以自动配置git,而不使用 - rebase 参数。 2

which tells the pull script that after doing the fetch, it should do a rebase instead of a merge. You can also configure git to do this automatically, without the --rebase argument.2

现在的主要问题是你有一个你可能不想要的合并。如果是这样,您将需要撤消合并(使用 git reset ;请参阅其他stackoverflow发布)。

The main problem right now is that you have a merge you probably didn't want. If so, you will need to "undo" that merge (with git reset; see other stackoverflow postings).

1 它不能:包含提交的git对象由其对象ID命名,该对象ID是其内容的Crypographic校验和。提交包括其父ID,树的ID,提交的作者和提交者(名称,电子邮件和时间戳)和提交消息。如果您更改任何这些,您将得到一个新的,不同的提交与不同的ID。

1It can't: a git object, including a commit, is named by its object ID, which is a crypographic checksum of its contents. A commit is comprised of its parent ID(s), the ID of the tree, the commit's author and committer (name, email, and timestamp), and the commit message. If you change any of these, you get a new, different commit with a different ID.

2 您甚至可以配置它使用 git pull --rebase = preserve 。但是,保留跨rebase操作的合并是一个单独的主题(我在stackoverflow发布之前已经介绍过)。

2You can even configure it to use git pull --rebase=preserve. However, preserving merges across rebase operations is a separate topic (which I've covered somewhat before in stackoverflow postings).

这篇关于git:推送单个提交,使用rebase重新排序,重复提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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