Josh Park
Josh Park

Reputation: 33

Unable to recover files in the created branch

Does anyone here knows how to recover the updated files in the branch? It happens when I'm back doing a task that I left a long time but upon checking, I was in my created branch, unfortunately, I already change a lot of it but still not added nor committed it. I decided to look at the master branch and noticed that there are files that were not yet added so I did the git add and commit consecutively, then upon switching back to my branch all the updated files I've made are no longer there. Sadly I already changed a lot of them. The only things the I could recover are those files that happen to be still open in my IDE.

Upvotes: 1

Views: 36

Answers (1)

Daemon Painter
Daemon Painter

Reputation: 3470

I'll try to wrap up the situation.

I was in my created branch, unfortunately, I already change a lot of it but still not added1 nor committed it

At this point in time, you are on branch A and you have a lot of edits that git is not tracking nor taking care of. If at any moment now you would decide to delete the file, there would be no way to get it back through git.

I decided to look at the master branch

Assuming you did this via git checkout master or git switch master, starting from branch A, you would have been prompted about uncommitted changes. Here git suggests you to either stash them or discard them.

Happy ending scenario: you did git stash. Then you can easily get your changes back by git switch branchA and git stash pop. Basically, asking git to give back what you stashed.

Sadly ending scenario: you did git reset of git checkout -- .. All your changes are lost and can't be recovered through git.

then upon switching back to my branch all the updated files I've made are no longer there

this happens because git doesn't want to switch branches when there is work done on tracked files which is not stashed nor committed.

Sadly I already changed a lot of them.

As a good practice, teach yourself to commit more often. Remember: commit often[, squash], push once.

The only things the I could recover are those files that happen to be still open in my IDE.

again I'm assuming that your IDE is going to tell you that your files where modified by an external source, do you want to reload them? By hitting NO, you are able to save something.


1. I'm assuming that there files are not added, but were already tracked. Any file that is not tracked by git is not considered as a blocking point when you switch branches. If these files were not tracked, and you switched to master, and added them and committed them there, your files are on master, but no longer on branch A. git merge master onto branchA --> think carefully about what are you about to do!

Upvotes: 1

Related Questions