24

I have added a new file to my local branch. On running the command git status it gives below output:

# On branch MyLocBranch
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   mypath/nextDir/myfile.py
nothing added to commit but untracked files present (use "git add" to track)

This is fine as I have untracked file and it shows in red in the terminal.

Now to stash this changes, I ran the commands git stash and git stash save "some message". But I get the error No local changes to save which is weird. The changes should have been stashed.

Deca
  • 1,155
  • 1
  • 10
  • 19
  • @Matt Thanks. That's what I was looking for! – Deca Jul 05 '16 at 12:09
  • So you can mark the answer as the accepted one and also upvote if you feel like it, even thought your question is marked as duplicate – Matt Jul 05 '16 at 21:27

2 Answers2

31

By default git stash doesn't save untracked files.

In order to also stash your untracked files, you can use the --include-untracked (or -u) option.

Matt
  • 3,422
  • 1
  • 23
  • 28
16

stash is used to save the changes in known/tracked files. Since this is a new/untracked file, git has nothing to save.

Note: This file will remain in your local directory even if you switch to different branch.

Mohan Kumar P
  • 3,122
  • 1
  • 14
  • 17
  • 1
    But git documentation says, Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit. So a bit confusion – Deca Jul 05 '16 at 10:17
  • Also one more from the git document, Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command. Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time – Deca Jul 05 '16 at 10:19
  • 2
    In order to accomplish your wish, whenever you are creating a new file you need to stage the file for committing immediately with "git add". Then git will understand it as your change and add into stash. – Mohan Kumar P Jul 05 '16 at 10:40
  • Thanks much!!!! :) – Deca Jul 06 '16 at 13:19
  • that's what I thought but when I use `git checkout ` I get the message: "The following untracked working tree files would be overwritten by checkout" (and it lists the untracked files) – zok Jun 01 '17 at 14:07
  • It means you have the same file in another branch. In current branch it is not committed but in other branch it was committed. So, whenever a new file is created, staging it for commit with "git add" will be a good behavior to avoid these issues. – Mohan Kumar P Jun 02 '17 at 04:59