git stash -how to switch between uncommitted versions

57 Views Asked by At

[git newbie here]

Say I'm working on a new file and I'm writing the following story:

Version1: A guy walks into a bar
          [here I tell my story]

At this point I like what I wrote but I wish to make some changes, so I stash my changes like this:

$git stash save -u 'version1: a guy walks'

I get the following message:

Saved working directory and index state On dev_story: version1: a guy walks

Next, I make a change:

Version2: A nice dude walks into a bar
          [here I tell a slighly different story]

and stash it again:

$git stash save -u 'version2: a dude walks'

received message:

Saved working directory and index state On dev_story: version2: a dude  walks

Checking my stash list I see:

$ git stash list
stash@{0}: On dev_story: version2: a dude walks
stash@{1}: On dev_story: version1: a guy walks

I wish to show my work to a friend and debate version1 vs version2.

when I try to apply stash@{0} it doesn't show 'a guy walks...' what I have tried:

$ git stash show stash@{0}  //new line no error

$ git stash apply stash@{0} 

test/story.txt already exists, no checkout
error: could not restore untracked files from stash
1

There are 1 best solutions below

0
Schwern On BEST ANSWER

$ git stash show stash@{0} //new line no error

Git showed you nothing here because your file was untracked. git stash show will not show you untracked files unless you ask with -u.

$ git stash show -u stash@{0}
test/story.txt | 1 +
1 file changed, 1 insertion(+)

$ git stash show -u -p stash@{0}
diff --git a/test/story.txt b/test/story.txt
new file mode 100644
index 0000000..f5d91d3
--- /dev/null
+++ b/test/story.txt
@@ -0,0 +1 @@
+A nice dude walks into a bar

$ git stash apply stash@{0} 

test/story.txt already exists, no checkout
error: could not restore untracked files from stash

You already had a test/story.txt file with changes. Because your stashed change creates a new file, git is protecting you from it overwriting the existing test/story.txt.


See Stashing and Cleaning for more about the stash.

In general, I find it quite easy for the stash to get messy and to forget what's in there or what it applies to. Its useful to stash away changes for just a moment. For anything longer, I would recommend using commits and branches.

If you need to save some unfinished work, make a commit and log it as a "wip" (work in progress). Then amend the commit later (see Changing the Last Commit. If you want to have two different versions, make a branch; that's what they're good at. Branches are very cheap.