

Try to stash ignored file with the git stash -a command, and see that it has been re-applied after running git stash apply, even though it is ignored by Git. gitignore file, but it does not show M圜omponent.tsx as an untracked file): gitignore:Īnd verify that it is indeed ignored (the git status command shows that we only changed the. Let's add the newly added M圜omponent.tsx to the. Obviously it doesn't by default, but we can get it working by using a magic flag - ( -a or -all). However, there is a magic flag that enables stashing untracked files - ( -u, or -include-untracked):Īnother good question is whether git stash works with ignored files (an ignored file is a file that is tracked in a special file called. If the file is added to tracked with the git add command, it can be stashed and re-applied later: This is because untracked files are not stored in Git. When we tried to stash an untracked file, Git responded with a message: No local changes to save. The git stash command does not store untracked files by default: Tracked files are usually marked with a green color: To move a file from the untracked to the tracked state, run the git add command.

Tracked files are files that should be committed the next time the git commit command is executed.

Git tells us that the file is untracked with the following message: Untracked files. I created a new M圜omponent.tsx component and executed the git status command: Untracked Filesīy default, when you create a new file in your working directory, it will show as untracked because it is not in the Git version system. Now that we know that git stash is capable of working with both staged and unstaged changes, we need to know how it works with untracked and tracked files.īut before we answer that question, let's learn what untracked and tracked files are in Git. Now that we've learned what staged and unstaged changes are, we can answer the question - git stash command saves both staged and unstaged changes, but after re-applying them, all changes become unstaged: It is also possible to have both staged and unstaged changes in the same file: Staged And Unstaged Changes In The Same File Staged changes are usually marked with a green color: To move a change from unstaged to staged state, run the git add command. Staged changes are changes that should be committed the next time the git commit command is executed. Git tells us that changes are unstaged with the following message: Changes not staged for commit. Unstaged changes are changes that exist in the working directory but haven't yet been added to the Git version history: Switch back to the feature/1 branch after the bug has been fixed, and re-apply the stashed changes with a git stash apply command:īefore you learn whether git stash works with both staged and unstaged changes, you should know what staged and unstaged changes are and how they differ.Switch to the main branch with the git checkout main command and work on a bug fix:.Stash the changes with a git stash command:.Check how many changes have been made with a git status command:.Remember that stash is local to your repository - it will not be transferred to the server after pushing changes.įor example, I'm working on a secret feature on a feature/1 branch when I get a request to fix a bug noticed by a tester in production (in other words, on the main branch), but I'm only halfway done with my secret feature, so that's what I usually do: This is handy when you need to quickly switch context and work on something else without losing unfinished work. You can get back to stashed changes and re-apply them at any time. The git stash command works like a clipboard - it temporarily saves the current state of a working directory and undoes it, so you can start coding new features from scratch.

You don't want to discard the changes and start over the next day, right?įortunately, Git provides a command to easily handle such situations. This situation leads to stopping the development and switching to another task, but what if you didn't manage to complete the current work and you are not ready to commit the changes? The developers work is often interrupted by requests to implement more important features or even to fix some critical bugs.
