git pull 之前 git push 实践的基本问题

发布于 2025-01-14 10:45:17 字数 1437 浏览 1 评论 0原文

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

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

发布评论

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

评论(1

夢归不見 2025-01-21 10:45:17

一一解答您的疑问:

(1) 我可以在不先 git-pull 的情况下执行 git-push 吗?是否不允许
all 或 github 可以(由管理员)配置为以某种方式运行
-- 在推的过程中对婴儿床说拉动尚未完成或根本不婴儿床..

先推而不拉不一定是问题。只有当其他人将更改推送到您的目标分支,而您尚未拉取这些更改时,这才是问题。在这种情况下,推送将被拒绝。您将收到的错误消息类似于:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to XXX

如果您想在不先拉取的情况下进行推送,可以使用 git push -f 覆盖此错误(尽管服务器可能配置为仍然拒绝您的推送) 。但是,这很少是可取的,因为您将覆盖其他人所做的更改。有关详细信息(以及为什么不这样做),请参阅例如 我如何正确强制 Git 推送?

(2) 跟进 (1) 问题,假设我先做了 git pull,但是
然后我并没有真正优雅地合并任何东西,只是覆盖了
本地文件,然后尝试 git push,这会通过吗?我是说
为了同名/记录,我做了 git-pull,尽管我不尊重它......什么
阻止用户执行此操作(如果有的话)。

是的,它会通过,不,git 中没有任何东西可以阻止这一点。虽然 git 确实可以(在某种程度上)判断您在合并时是否集成或丢弃了其他人的更改,但 git 无法判断这是否正确。但是,您的更改(包括合并)将在 git 的历史记录中可见,因此您的同事稍后可以向您投诉:-)。

(3) 有没有办法配置分支以便推送到分支
仅通过拉取请求而不是直接发生(例如从命令行
ETC)。是否有类似分行所有者的概念可以
配置是否允许直推?.

在核心 git 本身中,没有这样的东西。然而,当在服务器上托管项目的中央存储库时,服务器通常不仅运行 git,还会运行支持 git 的托管服务应用程序(例如 Gitlab、Bitbucket 或 Gitea)。这些托管应用程序通常支持限制谁可以在何时何地推送。

(4) 我知道其中一些可以通过运行一些来尝试
实验,但这只能让我对当前的情况有一些了解
设置。我想了解什么是标准行为以及什么是
可以定制...

我希望我已经给出了一些见解。标准行为通常可以在文档中找到。

Addressing your questions one by one:

(1) Can I do git-push without first git-pull?. Is it not allowed at
all or github can be configured (by admin) to behave in a certain way
-- say to crib during push that pull was not done or to not crib at all..

Pushing without pulling first is not necessarily a problem. It is only a problem if others pushed changes to your target branch, and you have not pulled these changes. In that case the push will be denied. The error message you will get will be something like:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to XXX

If you want to push without pulling first, this error can be overriden using git push -f (though the server may be configured to still reject your push). However, this is rarely advisable, as you will then overwrite changes others have made. For details (and why not to do this) see e.g. How do I properly force a Git push? .

(2) Following up on (1) question, say I did do git pull first, but
then I didn't really merge anything gracefully and just overwrote in
local files and then try to git push, will this go through?. I mean
for namesake/record, I did git-pull, though I didn't honor it...what
prevents a user from doing this (if at all).

Yes, it will go through, and no, there is nothing in git preventing this. While git can indeed tell (to some extent) whether you integrated or discarded changes by others when merging, there is no way for git to tell whether this is right. However, your changes (including your merge) will be visible in git's history, so your colleagues can complain to you later :-).

(3) Is there a way to configure a branch so that pushes to the branch
only happen thru pull request and not directly (say from command line
etc). Is there a notion of something like branch owner who can
configure whether to allow direct push or not?.

In core git itself, there is no such thing. However, when hosting a central repository for a project on a server, the server will usually not just run git, but a hosting service application that supports git (such as Gitlab, Bitbucket or Gitea). These hosting applications usually support restrictions on who can push where and when.

(4) I understand that some of this could be tried by running some
experiments, but that would only give me some idea on my current
setup. I want to understand what is the standard behavior and what all
can be customized...

I hope I have given some insight. The standard behaviour can usually be found in the documentation.

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