git-branchgit-commitgit-stashgithub-for-macgit-status

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!


Solution

  • 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.