Dear Readers,
In this article,we will see How to stash uncommitted changes with Git Stash in Git.
Implementation Steps
- Create a empty file “kt-stash”.
- Add and commit the file “kt-stash”.
- Perform first task.
- Create stash for first task.
- Perform second task.
- Create stash for second task.
- Apply git stash for first task.
- Complete first task.
- Apply git stash for second task.
- Resolve conflict and complete second task.
- Clean the stash repository
Git Stash
The git stash command is probably one of the most powerful commands in Git.
Git stash is a temporary storage. When you’re ready to continue where you left off, you can restore the saved state easily.
Git stash is used in order to save all the changes done to the current working directory and to go back to the last commit done on the branch (also called HEAD).
The “git stash” command can help you to (temporarily but safely) store your uncommitted local changes.
You need to work on that urgent bug.
First, you want to save out unfinished work changes without committing them.
This is where git stash comes as a savior.
To see the current branch
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Create a empty file “kt-stash”
1 2 3 4 5 6 |
Make a new file "kt-stash" [root@ip-172-31-42-20 mumbaigit]# touch kt-stash [root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file1.txt file2.txt file3.txt ktexperts.java kt-stash |
Add and commit the file “kt-stash”
Adding file to staging area
1 |
[root@ip-172-31-42-20 mumbaigit]# git add . |
Commit the file into Central Repository
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "commited kt-stash empty file" [master 57b2258] commited kt-stash empty file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 kt-stash |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit 57b2258f29b22b359e16e4654e605b292972d897 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Thu Dec 19 10:28:43 2019 +0000 commited kt-stash empty file |
Perform first task
Add content to file “kt-stash”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# vi kt-stash [root@ip-172-31-42-20 mumbaigit]# cat kt-stash My first task |
To see the status of git
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch 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: kt-stash no changes added to commit (use "git add" and/or "git commit -a") |
Create stash for first task
Sending first task content to stash repository (cut and paste)
When you run git stash
, the uncommitted code disappears without being committed.
1 2 3 4 5 |
root@ip-172-31-42-20 mumbaigit]# git stash Saved working directory and index state WIP on master: 57b2258 commited kt-stash empty file [root@ip-172-31-42-20 mumbaigit]# git stash list stash@{0}: WIP on master: 57b2258 commited kt-stash empty file |
Verify file “kt-stash”
We can see the file is in empty because we have stashed the content to stash repository.(cut and paste)
1 |
[root@ip-172-31-42-20 mumbaigit]# cat kt-stash |
Note
Your working directory is now clean and all uncommitted local changes have been saved.
You’re ready to start your second task.
Perform second task
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# vi kt-stash [root@ip-172-31-42-20 mumbaigit]# cat kt-stash My second task |
To see the status of git
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch 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: kt-stash no changes added to commit (use "git add" and/or "git commit -a") |
Note
If you want do first task again you need to send the second task task content to stash repository then bring first task content to your workspace.
Create stash for second task
Sending the content of second task to stash repository
1 2 3 4 5 |
[root@ip-172-31-42-20 mumbaigit]# git stash Saved working directory and index state WIP on master: 57b2258 commited kt-stash empty file [root@ip-172-31-42-20 mumbaigit]# git stash list stash@{0}: WIP on master: 57b2258 commited kt-stash empty file stash@{1}: WIP on master: 57b2258 commited kt-stash empty file |
Verify the file “kt-stash”
The file “kt-stash” is empty.
1 |
[root@ip-172-31-42-20 mumbaigit]# cat kt-stash |
Apply git stash for first task
In order to apply your Git stash specific stash to your current working directory.
use the “git stash apply stash@{1}” command.
Bring first task content from stash repository
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git stash apply stash@{1} On branch 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: kt-stash no changes added to commit (use "git add" and/or "git commit -a") |
Complete first task
Verify the file “kt-stash”
We can see the content of first task.
1 2 |
[root@ip-172-31-42-20 mumbaigit]# cat kt-stash My first task |
Add content to file “kt-stash”
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# vi kt-stash [root@ip-172-31-42-20 mumbaigit]# cat kt-stash My first task My first task is finished |
Add and commit the file “kt-stash”
Adding file to staging area
[root@ip-172-31-42-20 mumbaigit]# git add .
Commit the file into Local Repository
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "My first task is finished" [master b86c07c] My first task is finished 1 file changed, 2 insertions(+) |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit b86c07c743ef173034ac1ef7a4ec4fd627666a38 (HEAD -> master) Author: Ram <ram@gmail.com> Date: Thu Dec 19 10:56:05 2019 +0000 My first task is finished |
Apply git stash for second task
In order to apply your Git stash specific stash to your current working directory.
use the “git stash apply stash@{0}” command.
Bring second task from stash repository
1 2 3 4 5 6 7 |
[root@ip-172-31-42-20 mumbaigit]# git stash apply stash@{0} Auto-merging kt-stash CONFLICT (content): Merge conflict in kt-stash [root@ip-172-31-42-20 mumbaigit]# ls file1.java file2.java file3.java ktexperts ktexperts.php myfile file1.php file2.php file3.php ktexperts-1 ktexperts.txt file1.txt file2.txt file3.txt ktexperts.java kt-stash |
Resolve conflict and complete second task
Open and modify the file “kt-stash”
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# vi kt-stash [root@ip-172-31-42-20 mumbaigit]# cat kt-stash My second task My second task is finished |
Add and commit the file “kt-stash”
Adding file to Staging Area
1 |
[root@ip-172-31-42-20 mumbaigit]# git add . |
Commit the file into Local Repository
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "My second task is finished" [master e1058f4] My second task is finished 1 file changed, 2 insertions(+), 2 deletions(-) |
To see the latest commit
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git log -1 commit e1058f4e9f0283098ba250d4901089acf8871cee (HEAD -> master) Author: Ram <ram@gmail.com> Date: Mon Dec 23 09:56:43 2019 +0000 My second task is finished |
Clean the stash repository
1 2 3 4 5 |
[root@ip-172-31-42-20 mumbaigit]# git stash list stash@{0}: WIP on master: 90e69a7 commited kt-stash empty file stash@{1}: WIP on master: 90e69a7 commited kt-stash empty file [root@ip-172-31-42-20 mumbaigit]# git stash clear [root@ip-172-31-42-20 mumbaigit]# git stash list |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Follow Me
Ramesh’s Linkedin : https://www.linkedin.com/in/ramesh-atchala/
Priya
Best learning platform for devops students
santhi
Hello. Thanks for the article. An Update 1 of 2020? When could we expect that to roll out? Thanks for response.