I neither committed anything or stashed.
These changes are not in Git. Your entire working tree is not in Git. It was initially created by Git—Git put the initial set of files into it—but as you do work in your working tree, any updates you make to these files are unknown to Git.
You must git add
and git commit
, or at the least use git stash
or something similar, to make Git see and save the updated files. Until then, none of your changes are anywhere in Git. If they're truly gone, Git cannot help you recover them, because they never were in Git in the first place. However:
I then checkout master branch using git checkout master
This should either fail (telling you that it can't do that), or else preserve your changes. For the gory details about when, how, and why Git sometimes succeeds here (preserving your changed files), see Checkout another branch when there are uncommitted changes on the current branch.1
Did a git pull
.
In modern Git, this should generally fail unless you've:
- chosen
git pull --rebase
as your default, i.e., set up git pull
to run git fetch
and then git rebase
instead of git fetch
and then git merge
, and
- set up
git rebase
to use "autostash" mode.
If you did this, you may have a stash made (automatically!) before the git pull --rebase
. If the stash did not apply cleanly, or the rebase stopped in the middle, you may still have this stash. Check to see if you're still in the middle of an unfinished rebase!
Later when I checkout my feature branch using git checkout <feature-branch-name>
, I don't my changes anymore.
If you're still in the middle of an unfinished rebase, you'll need to terminate the unfinished rebase. Be aware that some versions of Git have some bugs with the autostash code such that the stash commit will still exist but you may have trouble finding it. For anyone to say anything further, you'll have to specify your precise Git version.
(I avoid the autostash feature as I don't like git stash
at all. This saved me from ever encountering these bugs myself.)
1Note that except for the special case of creating a new branch (git checkout -b newbranch
or git switch -c newbranch
), I generally don't recommend doing this.
If you have a case of "I meant to commit these to otherbranch, let me try git switch otherbranch
now to see if I can do that for free", that's also OK: if the git switch
or git checkout
works, you can now safely git add
and git commit
on the other branch.
If you can't git switch otherbranch
, I generally would recommend creating a new branch then and there, and committing on the new branch. You can then cherry-pick that commit to wherever you intended to work, and once you're all set, just delete the new temporary branch you made. But some people do like git stash
for this, and git stash
is faster than create-branch, cherry-pick, delete-branch.