Home > Back-end >  Forgot to commit the changes in git after adding them and directly pushed into the branch
Forgot to commit the changes in git after adding them and directly pushed into the branch

Time:06-05

I mistakely pushed the changes in a branch instead of committing them. I noticed that it was pushed successfully( git doesnot give any error message if we forget to commit). Now I do not get those changes neither in local, nor in git. They are lost somewhere . How can I get my uncommitted changes back?

Everyone ask/reply for the problems regarding committed changes and not pushed changes, but I could not see questions/replies of uncommitted and pushed changes.

enter image description here

CodePudding user response:

I mistakenly pushed the changes in a branch instead of committing them.

You literally can't do that. The git push command takes commits that you have, that some other repository lacks, and sends those commits to the other repository. It does not send individual files, and Git itself does not store changes in the first place, so you cannot push changes, nor push files: you can only push commits.

The screenshot has something very odd in it. This is my text transcription and it may contain errors, so beware, but I'll try to be accurate here:

$ git status
On branch feature/sswhntrW020Action
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:  lib/jackson-annotations-2.5.0.jar
        new file:  lib/jackson-annotations-2.9.0-javadoc.jar
        new file:  lib/jackson-annotations-2.9.0-sources.jar
        new file:  lib/jackson-annotations-2.9.0.jar
        new file:  lib/jackson-core-2.5.3.jar
        modified:  src/main/resources/templates/rebate/hn/sswhntrW020/sswhntrW020.html


Acer@Tulsi MINGW64 /d/WP/java/ft-rebate-internal (feature/sswhntrW020Action)
$ git add .
 create mode 100644 lib/jackon-data-fromat-csv-2.9.3-javadoc.jar
 create mode 100644 lib/jackson/dataformat-csv-2.9.3-sources.jar
 create mode 100644 lib/jackson-dataformat-csv-2.9.3.jar

The git add command is normally entirely silent and the create mode 100644 messages cannot be the result of git add.

They can be the result of running git commit, however: after committing, git commit runs a quick git diff --summary against the previous commit. It seems likely that something, somewhere, somehow, ran git commit right around this point. Oddly, the three file names mentioned here match none of the file names mentioned in the git status output above this point.

Here's an example of git commit printing this kind of message:

$ echo new > new.txt
$ git add new.txt
$ git commit -m new-file
[example 4669569] new-file
 1 file changed, 1 insertion( )
 create mode 100644 new.txt

Compare this to, e.g.:

$ git diff --summary HEAD^
 create mode 100644 new.txt

Note that git commit is not the only thing that runs git diff --summary like this: you also get it when doing what Git calls a fast-forward merge operation (from a git pull that executes a fast-forward, for instance). So there are several possible sources for these create mode 100644 ... messages. Since whatever was actually run is absent from your screenshot, we can only guess whether it was a git commit, or a git merge --ff-only, or whatever. But something operated and did some committing and/or resetting, because git push merely transfers commits without altering the state of your working tree.

Following the three create mode ... messages we have your git push, which indeed pushed, or began to push, some commit(s): it computed the need to send 54 Git objects. Oddly, this command's output is unfinished: it got to the point of printing:

Compressing objects:  53% (17/32)

and then there is nothing at all: no success message, no failure message, nothing. Did the push finish? Did it succeed? We don't know. The end of the compression phase should always end with 100%, in this case 32/32 objects; this phase is then followed by the sending, and the acceptance or rejection of any name-update requests by the receiving Git repository and software. But those are just not shown.

Last, there's your git status command that shows that there is nothing to commit. Exactly how things got into this state is not visible.

CodePudding user response:

If you havent committed the changes, dont think push will do anything (it will work but wont really add anything new). I am not entirely sure how the changes can be lost just by pushing. But there are ways to see what really happened. We can check the logs by (git log --oneline --graph --all). This will show all the logs in a graphical format and you can see where your remote branch is and where your local branch is. Then we can run a git diff to see the differences between your local branch and remote branch. I believe git log will make it clear as to what happened. Also pushing the changes doesnt delete the changes from your local branch so I dont know what you meant by "changes were lost".

  • Related