Reputation: 2401
I was working on the master branch when I note that the changes were too big and I decided that I should make them on another branch. So I did git checkout -b new_branch_name
on Git Bash and then (without committing on any of both branches) I discarded all the changes on the master branch (from Github Desktop). To my surprise, that also discarded the changes of the new branch, I thought they weren't linked.
How can I undo the discard?
EDIT:
Sorry for my poor terminology, I am new on git.
Upvotes: 0
Views: 71
Reputation: 24812
tl;dr: when in doubt :
git stash
.
I'm not sure what you call discard as this is not within git terminology. Also, I'm not sure what you mean by changes.
So what I'll suppose that by changes you mean things you've done but not comitted, i.e. that you had your workspace or index dirty.
That you were on the branch A
:
unstaged changes
↑
A
↑
…
and that you created a new branch B
unstaged changes
↑
A,B
↑
…
and that then you did what you call a discard, but what really is a reset --hard
of the current branch.
A,B
↑
…
If that's so, then I'm really sorry to tell you that your changes are lost.
Forever gone.
And you should hurry rewriting them as long as it's still fresh in your mind.
What you need to understand is that when you manipulate git, you're handling the following levels of changes :
as a rule of thumb, if it's not in a commit (i.e. 1. or 2.), then it can disappear. Otherwise, there are solutions to fix things.
But you sometimes want to protect your dirty index without making new commits. It's what the stash
is for. So every time you want to play around with branches, you can git stash
and your work is saved, your workspace and index are clean. Once you're done, you can git stash pop
and you'll find again your changes.
From what you say, there's a misunderstanding about that last part : local changes and changes to the index are not relative to a branch, it's relative to your filesystem. If you change the branch, they stay there and are applied to the new branch (if possible or otherwise you'll get an error).
Upvotes: 1