What happens when user does "git push" before "git add" and "git commit"?

3.4k views Asked by At

I was working on a project made some changes in my code (in Itellij IDE) and by mistake I did below two steps in my terminal and I saw some changes went into the main respository (not my changes or anything I had worked previously). Does anyone know why it did it ?

Note: Changes that were pushed were not my code.

  1. git pull ( all-upto-date, I received) from my terminal
  2. git push (rather than "git add" and "git commit") from my terminal

    • Adding additional information (edited) Yes, I had some changes in my local repository before I did a git push. But when I did the mistake of "git push" none of my local committed changes were pushed but rather someone else code was pushed which I had pulled.

I could not understand why git did that and thought of asking a question here to understand.

The question was asked to know possible cause without influencing my view on the answer.

2

There are 2 answers

0
Tim Biegeleisen On BEST ANSWER

It is difficult to say precisely what happened since you did not tell us the state of your local branch at the time you did the accidental git pull followed by git push. Assuming you started off with no local commits which did not already appear on the remote tracking branch, then I would have expected git push to fail by saying that the remote is already up to date. The sign for this would be if you had run git status right before the accidental git pull and Git told you that your branch was 0 commits ahead of the remote.

For the second step, you did a git push. Assuming this went through, then I would interpret it to mean that you did in fact have some local commits which were not pushed yet. So, all that happened is that some of your previous local work was pushed to the repository, perhaps prematurely. Assuming those commits were made in good faith, you may have nothing to worry about. If not, then you could always revert one or more of those commits by using git revert.

0
Srini V On

This is what happens when I tried replicating

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

Git pull is a success and I made a change to my file.

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

Nothing is spotted.

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")

When I do a git status it has identified a change

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

Added the file for a commit.

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

Again nothing is spotted

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

Status has identified the change

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%)

Committed locally

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

Now the push is a success.

In your case, there must have been some locally committed changes when you applied a push.