当用户在"git add"和"git commit"之前进行"git push"时会发生什么?



我正在做一个项目,对我的代码进行了一些更改(在Itellij IDE中),并且错误地在终端中执行了以下两个步骤,我看到一些更改进入了主存储库(不是我的更改或我以前处理过的任何东西)。 有谁知道为什么这样做?

注意: 推送的更改不是我的代码。

  1. git 拉取(我收到的所有最新)
  2. 从我的终端推送 git 推送(而不是"git add"和"git commit")

    • 添加其他信息(已编辑) 是的,在我进行 git 推送之前,我的本地存储库中有一些更改。但是当我犯了"git push"的错误时,我的本地提交更改都没有被推送,而是推送了我拉出的其他代码。

我不明白 git 为什么要这样做,并想在这里问一个问题来理解。

这个问题被问到知道可能的原因,而不会影响我对答案的看法。

很难确切地说出发生了什么,因为您在进行意外git pull然后是git push时没有告诉我们您当地分支机构的状态。 假设您一开始没有本地提交,这些提交尚未出现在远程跟踪分支上,那么我希望git push通过说远程已经是最新的来失败。 这样做的迹象是,如果您在意外git pull之前运行了git status并且 Git 告诉您您的分支在远程之前提交为 0。

对于第二步,您执行了git push。 假设这已经通过,那么我会将其解释为您实际上确实有一些尚未推送的本地提交。 所以,所发生的只是你以前的一些本地工作被推送到存储库,也许是过早的。 假设这些提交是善意的,您可能没有什么可担心的。 如果没有,那么您可以随时使用git revert还原其中的一个或多个提交。

这就是我尝试复制时发生的情况

user@machine MINGW64 /c (11.1.0)
$ git pull
Already up-to-date.

Git pull 是成功的,我对我的文件进行了更改。

user@machine MINGW64 /c (11.1.0)
$ git push
Everything up-to-date

什么也没发现。

user@machine MINGW64 /c (11.1.0)
$ git status
On branch 11.1.0
Your branch is up-to-date with 'origin/11.1.0'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified:   ReleaseNotes/Release_Notes_11.1.0.docx
no changes added to commit (use "git add" and/or "git commit -a")

当我执行 git 状态时,它已识别出更改

user@machine MINGW64 /c (11.1.0)
$ git add .

添加了用于提交的文件。

user@machine MINGW64 /c (11.1.0)
$ git push
Everything up-to-date

再次没有发现任何东西

user@machine MINGW64 /c (11.1.0)
$ git status
On branch 11.1.0
Your branch is up-to-date with 'origin/11.1.0'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified:   ReleaseNotes/Release_Notes_11.1.0.docx

状态已识别更改

user@machine MINGW64 /c (11.1.0)
$ git commit -m 'Release notes amended'
[11.1.0 28697fa] Release notes amended
1 file changed, 0 insertions(+), 0 deletions(-)
rewrite ReleaseNotes/Release_Notes_11.1.0.docx (62%)

本地提交

user@machine MINGW64 /c (11.1.0)
$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 50.57 KiB | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for 11.1.0:
remote:   https://bitbucket.org/URL
remote:
To bitbucket.org:Project/repo.git
7db5eb6..28697fa  11.1.0 -> 11.1.0

现在推动是成功的。

在您的情况下,应用推送时必须有一些本地提交的更改。

最新更新