I'm doing work on a branch, and I want to switch to a different branch to quickly change something else, so I do
git stash
then git status
shows
On branch <branch-1>
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: <test-file>
So I swap to the second branch
git checkout <other-branch>
but when I then do
git status
it shows
On branch <branch-2>
Untracked files:
(use "git add <file>..." to include in what will be committed)
file
nothing added to commit but untracked files present (use "git add" to track)
the changed file (while tracked in the original branch) shows in the untracked list in the second branch even though its stashed. If its stashed, shouldn't it be hidden?
EDIT-1: I'm trying to stash changes to a binary file if that makes a difference.
EDIT-2: Full reproducable example
Have branch-1
, and branch-2
On branch-1
with changes to file
git add .
git stash
git checkout <branch-2>
then git status
shows
On branch <branch-2>
Untracked files:
(use "git add <file>..." to include in what will be committed)
file
nothing added to commit but untracked files present (use "git add" to track)
CodePudding user response:
The git stash
command—at least when used the way you are using it—can only save tracked files. Any untracked files are not in Git. Files that are not in Git are not "on" any branch and therefore do not get swapped in and out when you change branches. This is all very loosely and improperly phrased, but almost certainly covers what you're doing.
If [a file is] stashed, shouldn't it be hidden?
No. That's not what git stash
is about.
I recommend that everyone—newbies and experienced Git users both—avoid git stash
as much as possible. It's full of many nasty traps for the unwary, and even those who are good at Git can fall afoul of some of its odder behaviors. What git stash
really does is make some commits (and then run git reset --hard
for you). The main thing that is special about these commits is that they are on no branch, which means it's easy to "un-stash" them on any branch later. Unfortunately, they are then in a format that most Git commands don't deal with correctly, so that only the git stash
command can be used with these on-no-branch commits, and if you need something that git stash
is bad at—which is almost everything—you can't use the rest of Git to help you out.1
So just make ordinary commits instead. To work temporarily on something else, on another branch, use git worktree
(provided your Git is at least 2.5, preferably at least 2.15).
1Fortunately, there's git stash branch
, which can turn an old stash into a branch. So if you've made some stash(es) and need to convert them, you can use this. Unfortunately, if you've used git stash -u
or git stash -a
, even this doesn't work: if you're stuck with this problem, $deity help you.
CodePudding user response:
Use git stash --include-untracked
if you also want to stash your untracked files