如何使不可推送的提交可推送?

发布于 2025-01-08 11:10:11 字数 1151 浏览 0 评论 0原文

Master 分支有这样的提交:A -> B-> C(头)HEAD 位于 C

我做了什么:

我签出了B并在其之上进行了提交。

现在树看起来像这样:

    A -> B -> C(master)(origin/master)
         |
         | -> B1 -> B2(HEAD)

项目目录上的 git status 给了我以下消息:

# Not currently on any branch.
nothing to commit (working directory clean)`

所以这意味着 B1B2 不能推。我知道发生这种情况是因为这些提交在任何分支上都不存在。现在,如果我将分支切换回 master,这些提交会丢失吗?我期待在这些未命名的分支上进行更多提交(B3, B4 ... BN),之后我希望 C 位于其之上。本质上,我希望以这种方式看到 master 分支:

    A -> B -> B1 -> B2 -> B3 -------> BN -> C(master)(origin/master)

或者至少以这种方式:

    A -> B ---------------------> C(master)(origin/master) -> D
         |                                                    ^
         |                                                    |
         | -> B1 -> B2 -> -> ...->BN--------------------------

我必须有哪些选项才能完成此任务?我希望能够将提交保存在 origin 中。

任何建议/方向表示赞赏。

Master Branch had commits like this : A -> B -> C(HEAD) . HEAD was at C.

What I did :

I checked out B and made commits on top of it.

Now the tree looks like this :

    A -> B -> C(master)(origin/master)
         |
         | -> B1 -> B2(HEAD)

git status on the project directory is giving me the following message :

# Not currently on any branch.
nothing to commit (working directory clean)`

So it means that B1 and B2 cannot be pushed. I got to know that this is happening because these commits doesn't exist on any branch. Now, if I switch my branch back to master will these commits be lost? I am expecting a few more commits(B3, B4 ... BN) on these unnamed branch after which I want C to come on top of it. In essence, I would like to see the master branch in this way :

    A -> B -> B1 -> B2 -> B3 -------> BN -> C(master)(origin/master)

or atleast this way :

    A -> B ---------------------> C(master)(origin/master) -> D
         |                                                    ^
         |                                                    |
         | -> B1 -> B2 -> -> ...->BN--------------------------

What options I have to accomplish this? I want to be able to save the commits in the origin.

Any suggestions/directions is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

¢好甜 2025-01-15 11:10:11

除非您可以在远程存储库中重写C,否则您想要的结果

A -> B -> B1 -> B2 -> B3 -------> BN -> C(master)(origin/master)

是不可能的;这样做会给其他所有基于C做过工作的人带来问题。

第二个偏好

A -> B ---------------------> C(master)(origin/master) -> D
     |                                                    ^
     |                                                    |
     | -> B1 -> B2 -> -> ...->BN--------------------------

很简单:正如 Magnus 所说,如果你给 B2 (或者序列中当前最新的提交)一个分支名称,你的生活会变得更轻松:这更容易记住并输入哈希值,并确保它不会被垃圾收集。

git checkout -b bbranch

A -> B -> C(master)(origin/master)
     |                                                    
     |                                                    
     | -> B1 -> B2 (bbranch)

现在,如果您出于某种原因想要将 B1B2 推送到原点,您可以照常合并和推送

git checkout master
git pull
git merge bbranch

A -> B ------> C -> D (master)
     |             /
     |            /
     | -> B1 -> B2 (bbranch)

git push

您可以继续处理 < strong>bbranch 并在完成后再次合并

A -> B ------> C -> D -> E -> ... -> En -> F (master)
     |             /                      /
     |            /                      /
     | -> B1 -> B2 -> B3  ->  ...  -> Bn  (bbranch)

注意:如果您出于某种原因想要将提交推送到原点,但您 >不想要它们ma​​ster,您只需推送bbranch,它就会带走您的提交。然后,您需要确保没有其他人会触及bbranch,或者使您的本地副本成为远程跟踪分支。

替换合并步骤B2 ->在本例中,D 与 git push origin bbranch 。

Your desired outcome

A -> B -> B1 -> B2 -> B3 -------> BN -> C(master)(origin/master)

is not possible unless you can rewrite C in the remote repo; doing this would cause problems for everyone else who has done work based on C.

The second preference

A -> B ---------------------> C(master)(origin/master) -> D
     |                                                    ^
     |                                                    |
     | -> B1 -> B2 -> -> ...->BN--------------------------

is easy: as Magnus says, you'll make your life much easier if you give B2 (or whatever is the current latest commit in your sequence) a branch name: this is just easier to remember and type than the hash, and will make sure it doesn't get garbage collected.

git checkout -b bbranch

A -> B -> C(master)(origin/master)
     |                                                    
     |                                                    
     | -> B1 -> B2 (bbranch)

Now, if you want to push B1 and B2 to origin for whatever reason, you can merge and push as normal

git checkout master
git pull
git merge bbranch

A -> B ------> C -> D (master)
     |             /
     |            /
     | -> B1 -> B2 (bbranch)

git push

You can keep working on bbranch and merge again when you're done with it

A -> B ------> C -> D -> E -> ... -> En -> F (master)
     |             /                      /
     |            /                      /
     | -> B1 -> B2 -> B3  ->  ...  -> Bn  (bbranch)

Note: if you want to push your commits to origin for whatever reason, but you don't want them on master, you can simply push bbranch and it'll take your commits with it. You'll then either need to be sure that no-one else will touch bbranch, or to make your local copy a remote-tracking branch.

Replace the merge step B2 -> D with git push origin bbranch in this case.

香橙ぽ 2025-01-15 11:10:11

如果我理解正确的话,您所要求的是一种使提交在外部存储库中可见的方法当它们不属于分支时

提交 B1 和 B2 可以通过其 SHA1 哈希访问,但不能通过任何命名引用访问。因此,从 git 的角度来看,它们是“垃圾”,最终将被 git gc 清理(经过您设置定义的足够时间后)。

当您执行 git Push 时,它会推送属于存储库“一部分”的更改。这包括可从命名引用访问的所有变更集。 “垃圾”提交不会消失。

因此,如果您不希望更改成为某些其他分支的一部分,但您仍然想推送它们,则应该将它们作为自己的分支。这将具有允许您推送它们、为您提供访问它们的句柄并防止它们被垃圾收集(您不希望这样)的效果。

另一方面,如果您准备好将松散提交作为 master 分支的一部分,那么您需要 git rebase (将它们放在在当前 master 之上)或 git merge (将它们与 master 集成,同时保留您当前拥有的准确历史记录)。您还可以重写 master 分支以将提交包含为您的“第一个”示例,但我不建议这样做(特别是如果您已经将 master 推送到另一个状态) 。

为了更明确地回答您的问题“如果我将 HEAD 切换回 master,这些提交会丢失吗?”,答案是“最终,是的,但立即不会.”。您可以检查提交的显式 SHA1 ID(并使用 git reflogHEAD@{1} 语法查找这些 ID)。但如果你不给它们附加一个命名的引用,它们最终就会消失。

What you are asking for, if I understand you correctly, is a way to make commits visible in an external repository when they are not part of a branch.

Commits B1 and B2 are accessible by their SHA1 hash, but not by any named ref. So, from git's perspective, they are "garbage" and will eventually be cleaned up by a git gc (after a sufficient period of time as defined by your settings has elapsed).

When you do a git push, it's pushing the changes which are "part of" your repository. This includes all changesets accessible from a named ref. The "garbage" commits don't go.

So, if you don't want the changes to be part of some other branch, but you still want to push them, you should make them a branch of their own. This will have the effect of allowing you to push them, giving you a handle to access them, and preventing them from being garbage collected (which you don't want).

If, on the other hand, you are ready to make the loose commits part of the master branch, you want git rebase (to put them atop the current master) or git merge (to integrate them with master while preserving the accurate history you currently have). You could also rewrite the master branch to include the commits as your "first" example, but I don't recommend this (especially if you already pushed master in another state).

To more explicitly answer your question "if I switch my HEAD back to master, will those commits be lost?", the answer is "eventually, yes, but immediately, no.". You can do a checkout of the explicit SHA1 IDs of the commits (and find those using git reflog, or the HEAD@{1} syntax). But in time they will go away if you don't attach a named ref to them.

青春如此纠结 2025-01-15 11:10:11

不知道为什么你不为你的提交创建一个新的分支。这就是我要做的:

git checkout B
git checkout -b fix
.. do some stuff
git commit -am 'did some stuff to B'
git checkout master
git merge fix

第一个答案:

git checkout master
git merge B2

你的提交永远不会丢失,除非你清理对它们的引用(使用 git gc 等)。只要您提交了更改,您就是安全的。

Not sure why you don't create a new branch for your commits. Here's what I would do:

git checkout B
git checkout -b fix
.. do some stuff
git commit -am 'did some stuff to B'
git checkout master
git merge fix

First answer:

git checkout master
git merge B2

Your commits are never ever lost, unless you clean up references to them (with git gc etc). As long as you have committed your changes, you are safe.

浮萍、无处依 2025-01-15 11:10:11

使用 --interactive 选项检查 git rebase。

Check git rebase with --interactive option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文