Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Stash Operation in Git: A Practical Guide

Understand Stash Operation in Git with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and common...

Table of Contents

Stash Operation in Git is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Suppose you are implementing a new feature of your product. Your code is in progress, suddenly a visitor. Because of this, you have to go out for a few hours. You cannot commit this code and cannot throw it away at your changes. So you need some temporary space, where you can keep these local changes and then return to commit it.

In Git, stash operations help you stash edited files, changes, and save them on a stack of uncompleted changes, and from there you can re-apply at any time.

 [jerry @ CentOS project] $ git status -s 
 M string.c 
 ?? string 

Now you want to switch the branch by an unexpected guest, but you don't want to commit what you did on it, so you stash the changes. To push these new stash into the stack, run git stash.

 [jerry @ CentOS project] $ git stash 
 Đang ghi thư mục làm việc và chỉ mục cơ sở dữ liệu WIP trên master: e86f062 Added my_strcpy function 
 HEAD is now at e86f062 Added my_strcpy function 

Now your working directory is clean and all changes are kept on a stack. Please verify it with the git status command.

 [jerry @ CentOS project] $ git status -s 
 ?? string 

Now you can safely move the branch and do anything. We can observe a list of stash changes using the git stash list command.

 [jerry @ CentOS project] $ git stash list 
 stash @ {0}: WIP on master: e86f062 Added my_strcpy function 

Suppose you have solved the problem with the new customer and you go back to making your unfinished code, you just need to perform the git stash pop command, to move the changes from the stack and put them into Current working directory.

 [jerry @ CentOS project] $ git status -s 
 ?? string 

 [jerry @ CentOS project] $ git stash pop 

The above command will produce the following result:

 # On the master branch 
 # Changed but not updated: 
 # (use "git add ." to update what will be committed) 
 # (use "git checkout - ." to discard changes in directory working) 
 # 
 # 
 modified: string.c 
 # 
 # Untracked files: 
 # (use "git add ." to include in what will be committed) 
 # 
 # 
 string 
 không thay đổi thêm vào commit (sử dụng "git add" and / or "git commit -a") 
 Dropped refs / stash @ {0} (36f79dfedae4ac20e2e8558830154bd6315e72d4) 

 [jerry @ CentOS project] $ git status -s 
 M string.c 
 ?? string 

FAQ

What is Stash Operation in Git?

Suppose you are implementing a new feature of your product.

Why is Stash Operation in Git important?

A clear understanding of Stash Operation in Git helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.

How should beginners approach Stash Operation in Git?

Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.