4

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!

halfer
  • 19,824
  • 17
  • 99
  • 186
Shajo
  • 873
  • 2
  • 11
  • 22

3 Answers3

3

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

Shajo
  • 873
  • 2
  • 11
  • 22
ferengee
  • 31
  • 2
  • @ferengee- Thanks,I would like to know how to recover the lost changes ! – Shajo Sep 08 '14 at 07:03
  • If you have any changes stashed, and you wish to apply them to the current branch. use 'git stash pop' as noted by @jcragun (see: http://git-scm.com/docs/git-stash) – ferengee Sep 08 '14 at 08:10
  • I didn't store anything using git-stash...Won't git client auto saved the changes somewhere that we can access it using some git command ? – Shajo Sep 08 '14 at 11:54
2

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.

Community
  • 1
  • 1
jcragun
  • 2,060
  • 10
  • 8
  • is it possible to recover the lost changes ? – Shajo Sep 06 '14 at 06:58
  • 1
    It's not sounding good based on what you've said. However, I'm really surprised it said aborting operation but still overwrote your changes. I'll do a few tests on my machine to see what happens. – jcragun Sep 08 '14 at 14:50
  • I tired to reproduce the bug I could do it two times sequentially but after that I couldn't reproduce the bug – Shajo Sep 08 '14 at 19:41
  • 1
    I just tested this out on a repo and didn't get the warning message you're talking about. I was able to successfully move between branches without saving changes and didn't lose any of my work. The GitHub client is doing some stash tricks whenever switching branches. – jcragun Sep 18 '14 at 07:16
  • even i am also couldn't reproduce it..All I know is something went wrong in GitClient at that time .. – Shajo Sep 18 '14 at 16:17
0

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.

continuousqa
  • 508
  • 4
  • 14