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
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
If you want to apply the saved changes but not remove them from the stack, you can use git stash apply
instead:
bash
Viewing the Git Stash Content
You can view a list of all the saved stashes with the following command:
bash
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
If you want to remove all saved stashes, you can use:
bash
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
If you decide to proceed with the deletion of those files, you can use:
bash
To also remove untracked directories, use the -d
option:
bash
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.
- Introduction to Git and Version Control
- Installation of Git and Initial Setup
- Understanding Repositories
- Basic Workflow in Git
- Working with Branches in Git
- Collaborating with Other Developers
- Undoing Changes in Git
- Working with Tags in Git
- Rebase and Squash in Git
- Stashing and Cleaning in Git
- Advanced Git Commands
- Hooks and Automation in Git
- GitHub and Repository Management
- Best Practices in Git
- Conclusion and Final Tips