Shajo
Shajo

Reputation: 203

Git-branch switching all the uncommited changes are gone

I have made so many changes in so many files in my local git repo.

But when I switched the branch using mac Git-Client without committing changes a warning window came saying there are uncommitted changes so its going to abort the operation.

I thought I couldn't switch to other branch without committing. But what happened is all my changes are simply gone away.

I saw the status using git status terminal and its response was

On branch branchname Your branch is up-to-date with 'origin/branchname'.

nothing to commit, working directory clean

I tried to know if Git-Client stores it as stash using git stash and the response was

No local changes to save

I couldn't recover the uncommitted changes of that branch!

Upvotes: 4

Views: 3100

Answers (4)

Andre Nel
Andre Nel

Reputation: 442

this saved me -> if using and in "visual studio code" -> "show and run commands" in drop-down at the top-middle of "visual studio code", then type:

git stash pop or git stash apply

...if in "visual studio code" then choose: "Git: Pop Latest Stash"

for more help see: How to recover stashed uncommitted changes

git stash apply may be better... as it doesn't remove it from stash allowing for more attempts...

Upvotes: 0

continuousqa
continuousqa

Reputation: 508

I just had the same problem. I'm new to git... i created a branch and the next day found I was not working in the branchI created. My changes were uncommitted. I did try and commit them (git add . and git commit -m "...") but I got "you are already up to date, there is nothing to commit." I thought this might be due to my work being off branch the second day. At this point, my work was there, just not committed. so I switched branches to my initial branch. When I did this - the project updated and all my work was gone. git stash list returns nothing

Switching back to master, doesn't have my changes. My branch doesn't have my changes... I just lost about a days worth of code.

The only thing I do recall was that when I did git branch it listed HEAD as where I was.

I guess my work is gone and I'm not sure what I did wrong... I realize I started working in a branch on Day 1, and it was my error to not ensure I was still in that branch on Day 2... but somewhere along the lines I was writing code in a place that couldn't commit it.

Upvotes: 0

jcragun
jcragun

Reputation: 2198

According to this blog, you have to add any new files to the index before git stashing.

Before you start git stashing, make sure any new files added to the working directory have been added to the index: git stash will not stash (save) files in the working directory unless the files are being tracked (some version of the file has been added to the index).

This question also seems relevant, especially the answer and comments.

"You need a clean state to change branches." is only true if the branch change affects the 'dirty files'.

And then this

For the stash method, I typed "git stash save", "git checkout otherbranch", then finally "git stash pop".

I'm not familiar with the GitHub Mac client, but it should be using git commands underneath which is why it aborted. The only way to switch branches when files are dirty, is to do git checkout -f <branch>.

I tested out the GitHub Mac client and found it to magically auto stash when switching branches. This is confirmed in the documentation.

Upvotes: 2

ferengee
ferengee

Reputation: 31

I would like to point out that you should use git stash list to see of the git client put anything in the stash. git stash is shorthand for git stash save

Upvotes: 3

Related Questions