git push --force,在幕后 [英] git push --force, behind the scenes

查看:340
本文介绍了git push --force,在幕后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你使用--force推git时会发生什么?

我一直在网上搜索大约一个小时,而且我可以找到最相关的答案是:

 与[git push]相同,但强制推送,即使它导致非快进合并。除非你确定你知道你在做什么,否则不要使用--force标志。 

-source from https://www.atlassian.com/git/tutorial/remote-repositories#!push



我认为这很好,但是对我来说,这就像是说 git push --force 会强制推送。尽管我进行了搜索,但我找不到任何具体的概述了推动力所涉及的过程。



假设我有一个名为git @ heroku的主要回购协议.com:my-app.git,以及在Heroku上设置的用于登台的回购镜像,名为git@heroku.com:my-app-staging.git。



我创建了一个名为'new_changes'的本地分支,完成了工作,并从此分支中推送到分段。



我不喜欢结果,所以我放弃了项目,建立一个名为'more_new_changes'的新分支,做了一些工作,在本地进行了测试,与主人合并,并试图推动分级,以确保分级是最新的。



我的推送告诉我...

 推送至git@heroku.com:my-app -staging.git 
获取存储库,完成。
至git@heroku.com:my-app-staging.git
! [被拒绝] master - > master(非快速转发)
错误:未能将某些文件推送到'git@heroku.com:my-app-staging.git'
为防止您失去历史记录,正向更新被拒绝
在再次推送之前合并远程更改(例如'git pull')。有关详细信息,请参阅git push --help的快速转发部分的
'注意事项。

我不想将分段中的代码合并到本地主分支中 - 事实上,我希望暂存代码消失。 (显然,这些分支的标题是哈希值,但是为了争辩),我认为如果我的分段看起来像

push079

code> - > push080 - > new_changes(HEAD)



和我的本地外观如

push079 - > push080 - > more_new_changes(HEAD)



我决定使用--force ,那么结果将是一个分段分支,看起来像



push079 - > push080 - > more_new_changes(HEAD)



或者,它看起来更像



push079 - > push080 - > new_changes


               ;                    \

      &NB属;                                  > more_new_changes(HEAD)

但是如果我错了,而且只是把两者压在一起,我就会得到类似于
$ b

push079 - > push080 - > new_changes - > more_new_changes(HEAD)

然后我失去了一个镜像反映我的生产回购的连续性。



那么,当你使用--force推送时幕后实际发生了什么?

解决方案

  push079-> push080-> more_new_changes(HEAD)
\
- > new_changes(仅限reflog或git fsck)

一次提交wi将会取代另一个:在推送新的之前,您不需要摆脱先前的提交。



这个答案说明了 git fsck 将允许您取回要替换为 git push --force 的历史记录。


what exactly happens when you git push with --force?

I have been searching the web for about an hour now, and the most relevant answer I can find is:

Same as [git push], but force the push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.

-source from https://www.atlassian.com/git/tutorial/remote-repositories#!push

I understand this well enough, I think, but to me this is like saying git push --force will force a push. Despite my searching, I can't find anything really concrete that outlines the process involved in a force push.

Hypothetically, I have a main repo set up called git@heroku.com:my-app.git, and a mirror of that repo set up on Heroku for staging called git@heroku.com:my-app-staging.git.

I have created a local branch called 'new_changes', done work, and pushed to staging from this branch.

I didn't like the results, so I abandoned the project, set up a new branch called 'more_new_changes', did some work, tested it locally, merged with master, and tried to push to staging, just to make sure staging is up-to-date.

My push tells me...

Pushing to git@heroku.com:my-app-staging.git
Fetching repository, done.
To git@heroku.com:my-app-staging.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@heroku.com:my-app-staging.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

I don't want to merge the code that's on staging into my local master branch- in fact, I want that staging code to be gone. (Obviously, the branches would be titled with hashes, but for the sake of argument) I figure that if my staging looks like

push079->push080->new_changes(HEAD)

and my local looks like

push079->push080->more_new_changes(HEAD)

and I decide to push to staging using --force, then the result would be a staging branch that looks like

push079->push080->more_new_changes(HEAD)

Alternatively it could look more like

push079->push080->new_changes
                                     \
                                       >more_new_changes(HEAD)

but if I'm wrong, and it just squashes the two together, and I wind up with something like

push079->push080->new_changes->more_new_changes(HEAD)

then I lose the continuity of having a staging repo that mirror-reflects my production repo.

So, what actually happens behind the scenes when you push using --force?

解决方案

The result of a git push --force will actually be:

push079->push080->more_new_changes(HEAD)
             \
              ->new_changes (reflog only or git fsck)

One commit will replace the other: you don't need to get rid of the previous commit before pushing the new one.

This answer illustrates how git fsck would allow you to get back the history you would replace with a git push --force.

这篇关于git push --force,在幕后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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