Chuck's Academy

GIT

Stashing and Cleaning in Git

As you work on larger projects, you will likely find yourself in situations where you need to switch quickly from one task to another without losing your current work. This is where git stash becomes extremely useful. Additionally, over time, the repository might get filled with unwanted or untracked files. In this chapter, you will learn how to use git stash to save your changes temporarily and clean your repository to keep it organized.

Saving Changes Temporarily with Git Stash

The git stash command allows you to save your current changes in the working area without having to commit. This is useful when you need to switch branches or perform another task before finishing what you were doing, without losing your work in progress.

To save your current changes, run:

bash
"With the 'git stash' command, you save the current changes in a temporary stack, leaving the working area clean so you can change context without losing those changes."

This command saves the modified and uncommitted files, as well as those in the staging area. After using git stash, your working area will appear as if no changes were made, allowing you to switch branches or perform other tasks.

Recovering Changes with Git Stash Pop

When you're ready to return to your saved changes, you can recover them with git stash pop. This will apply the saved changes and remove them from the stack.

bash
"The 'git stash pop' command applies the previously saved changes and removes them from the stash stack, returning them to your working area."

If you want to apply the saved changes but not remove them from the stack, you can use git stash apply instead:

bash
"With 'git stash apply', you retrieve the saved changes but keep them in the stack so you can apply them again if needed."

Viewing the Git Stash Content

You can view a list of all the saved stashes with the following command:

bash
"The 'git stash list' command displays a list of all the stash entries you've temporarily saved."

Each stash entry is numbered, allowing you to apply or delete a specific stash.

Deleting a Stash

If you no longer need a specific stash, you can delete it using git stash drop:

bash
"The 'git stash drop followed by the stash number' command removes that entry from the stash stack, freeing up space and preventing confusion."

If you want to remove all saved stashes, you can use:

bash
"The 'git stash clear' command removes all the saved stashes, completely clearing the stash stack."

Cleaning the Repository with Git Clean

In some cases, your repository might get filled with unwanted or untracked files. Git offers the git clean command to delete these files and keep your working area organized.

To see which untracked files will be deleted, you can use the following command in dry-run mode:

bash
"With the 'git clean -n' command, you perform a dry run to see which untracked files will be deleted without actually removing them."

If you decide to proceed with the deletion of those files, you can use:

bash
"The 'git clean -f' command permanently removes untracked files from your working area, cleaning the repository."

To also remove untracked directories, use the -d option:

bash
"The 'git clean -fd' command removes both untracked files and directories from your repository."

Conclusion

In this chapter, we have explored how to use git stash to save changes temporarily and how to use git clean to delete untracked files and directories. These tools allow you to work more efficiently and keep your repository clean and organized. In the next chapter, we will learn advanced commands like git cherry-pick and git bisect, which will help you select specific commits and find bugs in your project.


Ask me anything