How to stash uncommitted changes with Git Stash in Git

Share via:

Dear Readers,

In this article,we will see How to stash uncommitted changes with Git Stash in Git.

Implementation Steps

  1. Create a empty file “kt-stash”.
  2. Add and commit the file “kt-stash”.
  3. Perform first task.
  4. Create stash for first task.
  5. Perform second task.
  6. Create stash for second task.
  7. Apply git stash for first task.
  8. Complete first task.
  9. Apply git stash for second task.
  10. Resolve conflict and complete second task.
  11. 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

Create a empty file “kt-stash”

Add and commit the file “kt-stash”

Adding file to staging area

Commit the file into Central Repository

To see the latest commit

Perform first task
Add content to file “kt-stash”

To see the status of git

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.

Verify file “kt-stash”

We can see the file is in empty because we have stashed the content to stash repository.(cut and paste)

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

To see the status of git

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

Verify the file “kt-stash”

The file “kt-stash” is empty.

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

Complete first task

Verify the file “kt-stash”

We can see the content of first task.

Add content to file “kt-stash”

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

To see the latest commit

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

Resolve conflict and complete second task

Open and modify the file “kt-stash”

Add and commit the file “kt-stash”

Adding file to Staging Area

Commit the file into Local Repository

To see the latest commit

Clean the stash repository

Thank you for giving your valuable time to read the above information.
Follow us on 
Website  www.ktexperts.com
Facebook Page KTexperts
Linkedin Page : KT EXPERTS

Follow Me
Ramesh’s Linkedin : https://www.linkedin.com/in/ramesh-atchala/

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

2 thoughts on “How to stash uncommitted changes with Git Stash in Git

Add Comment