Git

How to Stash Untracked Files in Git?

Git stash is a useful feature of git when the git user needs to switch from one working directory to another working directory for fixing any issue and the modified files of the current working directory are required to store before switching. Git slash stores all modified tracked, untracked, and ignored files and helps the user to retrieve the modified content when requires. It allows the git user to save changes that can be required in the future and helps the user to clean the current working directory quickly. It stores the uncommitted changes of the tracked files by default and omits the untracked and ignored files. But sometimes, it requires saving the untracked files. The untracked files can be slashed in by using –include-untracked option of git stash command and git add command. How these commands can be used to save untracked files in git has shown in this tutorial.

Pre-requisites

Install GitHub Desktop
GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the installer of the latest version of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account
You will require to create a GitHub account to publish any local repository.

Create a Local Repository
You have to create a local repository with one or more files and folders to check the commands used in this tutorial to stash the untracked files.

Initialize the git Repository
Go to the local repository folder from the terminal and run the following command to initialize the local repository.

$ git init

Add a File in the Repository

Run the following command to add the basic.py in the repository.

$ git add basic.py

Check the Tracked and Untracked File

Run the “ls” command to check the list of all files and folders of the local repository folder.

$ ls

Run the “git commit” command to check the tracked and untracked files:

$ git commit

The following output shows that the repository folder contains two files. These are basic.py and test.py. The basic.py is added to the repository before. So, basic.py is a tracked file and test.py is an untracked file, respectively.

Stash Untracked Files Using “git stash”

The modified untracked files can be saved using the “git stash” command in two different ways. One way is to use the –include-untracked option with the “git stash” command. Another way is to use the -u option with the “git stash” command. The use of these options has been shown below.

A) Using –include-untracked Option
Run the following commands to check the file and folder list of the repository folder. Save the untracked files and check the file and folder list of the repository folder again. Here, the “ls” command is used to show the list of files and folder of the repository folder, and the “git stash –include-untracked” command is used to save the untracked files.

$ ls
$ git stash --include-untracked
$ ls

The following output shows that the repository folder contains two files, named basic.py and test.py, before executing the “git stash” command. Here, basic.py is tracked file and test.py is an untracked file. The untracked file is removed from the repository folder after executing the “git stash” command.

B) Using -u Option

Run the following commands to check the tracked and untracked files of the repository. Save the untracked files and check the file and folder list of the repository folder again. Here, the “git ls-files” command is used to show the list of tracked files, the “ls” command is used to show the list of files and folder of the repository folder, and the “git stash -u” command is used to save the untracked files.

$ git ls-files
$ ls
$ git stash –u
$ ls

The following output shows that the repository folder contains two files, named basic.py and test.py, before executing the “git stash” command. Here, basic.py is tracked file and test.py is an untracked file. The untracked file is removed from the repository folder after executing the “git stash -u” command.

Stash Untracked Files Using “git add”

The untracked files of the repository can be saved without using the –include-untracked or -u option of the “git stash” command. You have to add the untracked files of the repository by using the “git add” command and run the “git stash” command to save the untracked file and clean the current directory for working by removing the untracked file from the repository folder.

$ git ls-files
$ ls
$ git add new.py
$ git slash
$ ls

The following output shows that the list of tracked and untracked files of the current repository is like the previous commands of stashing the unstacked file. The untracked file is removed from the repository folder after executing the “git stash” command.

Retrieve the Stashed File

There is another git command to restore the saved untracked files in the repository folder when needed. Run the following command to check the list of files and folders of the current repository folder and restore the previously stashed untracked files in the repository folder. Here, the “ls” command is used to show the list of files and folder of the repository folder, and the “git stash apply” command is used to restore the untracked files.

$ ls
$ git slash apply
$ ls

The following output shows that the repository has one tracked file and after executing the “git stash apply” command, the untracked file that is stashed before is restored in the repository folder.

Conclusion

Different ways of stashing untracked files and cleaning the current repository directory have been described in this tutorial using the “git stash” command. How the untracked files can be restored using the “git stash” command was also shown here. The concept of the tracked and untracked files and the way to stash the untracked files in git will be cleared for the git users after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.