Git Stashing and Cleaning
Today I’m going to share a little information about the Stashing and Cleaning in GIT.
While working in environment we all face some messy state, when we are in middle of some new project and we want to checkout to another branch to solve any high priority bug or else. The problem is, we cant commit the half done code changes. After some hours we need to get back to this point.
So in this case, the problem will be solved with git stash
command.
Stashing
It takes the modified file and staged changes and saves it on the stack. And you can reapply your stashed changes anytime.
To know the changes you have made in your files, just try with git status
command.
$ git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: MyDoc.txt no changes added to commit (use "git add" and/or "git commit -a")
In the above code snippet, MyDoc.txt file is modified so now we want to checkout the new branch, so we will stash the changes.
To push the changes into the stash, run git stash
or git stash push
command.
$ git stash Saved working directory and index state WIP on master: 5bf464e My New Document
So now the current working branch got cleaned.
$ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
To view the list of stashed changes use git stash list
:
$ git stash list stash@{0}: WIP on master: 43fbcc0 Content updated stash@{1}: WIP on master: f41583a Changes done stash@{2}: WIP on master: 5bf464e My New Document stash@{3}: WIP on master: 5bf464e My New Document
In this case, you can reapply the stashed changes using the git stash apply
. If you want to apply the particular stashed changes you can mention like this git stash apply stash@{2}
. If you don’t specifies any stash Git will apply the most recent stashed changes.
$ git stash apply On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: MyDoc.txt no changes added to commit (use "git add" and/or "git commit -a")
This git stash apply
will doesn’t remove the changes from stack. git stash pop
you can use this to apply your stash and to drop it from the stack immediately.
To remove it, you can run git stash drop
with the name of the stash to remove:
$ git stash drop stash@{2} Dropped stash@{2} (f1f438ec1e2c8a59d9774f926e91416a4c7fb1e0)
If not sure about the which stash you want to pop to your working directory, you can use git stash
show stash@{2} -t
. It will show you the content stashed.
$ git stash show stash@{2} -t diff --git a/MyDoc.txt b/MyDoc.txt index a832b34..ec1e56b 100644 --- a/MyDoc.txt +++ b/MyDoc.txt @@ -1 +1,2 @@ -Hai \ No newline at end of file +Hai +File is edited. \ No newline at end of file
Cleaning
Sometimes our working directory would be filled up with unwanted files, so that’s where git clean
command would be useful. It will remove the untracked files from local.
Always make sure once when you deleting the unwanted files. Use git clean -n
to see what are file going to be deleted.
$ git clean -n Would remove MyNewDoc2.txt
Once you OK with the files that are going to be delete you can run git clean -f
$ git clean -f Removing MyNewDoc2.txt
Below are some more options to do the cleaning,
- To remove directories, run
git clean -f -d
orgit clean -fd
- To remove ignored files, run
git clean -f -X
orgit clean -fX
- To remove ignored and non-ignored files, run
git clean -f -x
orgit clean -fx
Note the case difference on the X
for the two latter commands.
Happy Learning 🙂