Git

How to Temporarily Save Changes in Git?

While working on numerous tasks at the same time on various branches, users are required for switching between branches. However, if they have some changes in the working area that have not been committed yet. Git does not allow switching between branches. For that purpose, Git provides the facility to save the changes temporarily in Git and then switch to the other branch. After completing the task, they can restore the temporarily saved changes.

This post will briefly explain the process of temporarily saving changes in Git.

How to Temporarily Save Changes in Git?

To temporarily save changes in Git, check out the below-stated procedure:

  • Go to the Git local repository.
  • Make a new file using the “touch” command.
  • Track changes from the working area to the staging area by executing the “git add” command.
  • Run the “git status” command to view the current working repository status.
  • Use the “git stash” to temporarily save the changes in Git.

Step 1: Navigate to Local Repository
First, move toward the Git local repository using the “cd” command:

cd "C:\Users\user\Git\testrepo"

Step 2: Generate a File
Make a new file by executing the below-provided command:

touch file1.txt

Step 3: Track Changes
Next, track the generated file from the working area to the staging area by executing the “git add” file:

git add file1.txt

Step 4: View Current Working Repository Status
Now, check the current state of the working repository with the help of the following command:

git status

The below-stated output indicates that the file has been tracked successfully from the working area to the staging area and is ready to commit:

Step 5: Save Changes Temporarily
To temporarily save the staging index changes in Git, run the “git stash” command:

git stash

As a result, the changes have been temporarily saved into Git successfully:

Step 6: Verification
To ensure that the changes have been temporarily removed from the staging area or not, execute the below-given command:

git status

It can be observed that the tracked changes have been removed from the staging index successfully:

Step 7: Undo Temporary Saved Changes
To undo the temporarily saved changes, use the “git stash pop” command that will restore the changes in the staging area:

git stash pop

That’s all about temporarily saving the changes in Git.

Conclusion

To temporarily save the changes in Git, initially, go to the Git local repository and make a new file using the “touch” command. Next, track it from the working area to the staging index. Then, run the “git status” command to view the current working repository status and use the “git stash” to temporarily save the changes in Git. This tutorial illustrated the method for temporarily saving changes in Git.

About the author

Hafsa Javed