git 推送而不先 git 获取是否安全? (或git拉)

Safe to git push without git fetch first? (or git pull)

我终于开始理解 git 的基础知识,但我只是想确保我理解它的这个特定部分。

我一直在使用 Visual Studio 中的 git 功能,所以我已经习惯了 "sync" 按钮。啊。现在我正在从命令行执行所有操作,但是我只是想确定当我推送到 origin master 时我做的事情是正确的。我是唯一一个在远程工作的开发人员,所以这是一个低风险的情况(相对而言)......但是我应该在推送之前至少 git 获取吗?似乎我必须确保我推送到我的本地仓库更新到最新的远程仓库。正确的?如果我推送和远程/本地不同步会发生什么?我收到错误了吗?

如果您有不兼容的更改,

git 将简单地拒绝 push

您不必在每次推送之前都pull/fetch。如果您的推送与远程存储库中已有的内容冲突,git 会报错,如下所示:

$ git push
To git@git.example.com:your/repository.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@git.example.com:your/repository.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.