当你使用 --force 进行 git push 时到底会发生什么?
我已经在网上搜索了大约一个小时,我能找到的最相关的答案是:
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.
- 来源自https://www.atlassian.com/git/tutorial/remote-repositories#!push
我想我对此非常理解,但对我来说这就像说git push --force
将强制推动。尽管我进行了搜索,但我找不到任何真正具体的内容来概述强制推送所涉及的过程。
假设,我设置了一个名为[电子邮件受保护]:my-app.git,以及在 Heroku 上设置的用于暂存的该存储库的镜像,名为[电子邮件受保护]:my-app-staging.git。
我创建了一个名为“new_changes”的本地分支,完成了工作,并从该分支推送到暂存。
我不喜欢这个结果,所以我放弃了这个项目,建立了一个名为“more_new_changes”的新分支,做了一些工作,在本地测试了它,与master合并,并尝试推送到staging,只是为了确保staging已经启动-迄今为止。
我的推动告诉我...
Pushing to [email protected]:my-app-staging.git
Fetching repository, done.
To [email protected]:my-app-staging.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]: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.
我不想将暂存的代码合并到我的本地主分支中 - 事实上,我希望暂存代码消失。 (显然,分支将以散列为标题,但为了论证)我认为如果我的分期看起来像
push079
->push080
->new_changes(HEAD)
我的本地看起来像
push079
->push080
->more_new_changes(HEAD)
我决定使用 --force 推送到暂存,然后结果将是一个看起来像的暂存分支
push079
->push080
->more_new_changes(HEAD)
或者它可能看起来更像
push079
->push080
->new_changes
\
>more_new_changes(HEAD)
但如果我错了,它只会把两者压在一起,我最终会得到类似的东西
push079
->push080
->new_changes
->more_new_changes(HEAD)
然后我就失去了镜像反映我的生产仓库的暂存仓库的连续性。
那么,当您使用 --force 进行推送时,幕后实际发生了什么?