Patrick McDermott
Patrick McDermott

Reputation: 1220

Resetting a new file in staging area to working directory in Git

In Git I have checked out a new branch branch band made multiple changes including edits, new files, folders etc. I need to revert back to an old branch branch a to make changes, so I want to stash all changes made in branch b. However I have some untracked files and folder in branch b so if I revert back to branch a, they follow as untracked files.

I attempted to add and then reset these untracked files with the intention of them moving to the working directory, but they revertedback to an untracked file (no surprise).

Question

How do I add these untracked files in branch b to the working directory ready to be stashed? (without committing)

Upvotes: 0

Views: 368

Answers (3)

Venkataraman R
Venkataraman R

Reputation: 12969

First add the untracked file to stage area, using git add command.

git add /pathToUntrackedFile
git stash 

If you don't want to stage it and keep them as untracked, you can use below commands

git stash -u # means --include-untracked 

Now, you can switch branch a and work on it

git checkout branch a

In future, if you want to again work on branch b

git checkout branch b
git stash pop

Upvotes: 1

Chris Maes
Chris Maes

Reputation: 37742

You might be interested to use

git add --intend-to-add

to start tracking files without really adding/committing them.

From the manual:

-N, --intent-to-add

Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is useful for, among other things, showing the unstaged content of such files with git diff and committing them with git commit -a.

Upvotes: 3

Johnny Wong
Johnny Wong

Reputation: 975

At branch b, add all the new files to the staging area, and do a save stash action, done.

After stashed, the new files will be saved to the stash, and removed from the working directory.

Upvotes: 1

Related Questions