Git StashSometimes you want to switch the branches, but you are working on an incomplete part of your current project. You don't want to make a commit of half-done work. Git stashing allows you to do so. The git stash command enables you to switch branches without committing the current branch. The below figure demonstrates the properties and role of stashing concerning repository and working directory. ![]() Generally, the stash's meaning is "store something safely in a hidden place." The sense in Git is also the same for stash; Git temporarily saves your data safely without committing. Stashing takes the messy state of your working directory, and temporarily save it for further use. Many options are available with git stash. Some useful options are given below:
Stashing WorkLet's understand it with a real-time scenario. I have made changes to my project GitExample2 in two files from two distinct branches. I am in a messy state, and I have not entirely edited any file yet. So I want to save it temporarily for future use. We can stash it to save as its current status. To stash, let's have a look at the repository's current status. To check the current status of the repository, run the git status command. The git status command is used as: Syntax: Output: ![]() From the above output, you can see the status that there are two untracked file design.css and newfile.txt available in the repository. To save it temporarily, we can use the git stash command. The git stash command is used as: Syntax: Output: ![]() In the given output, the work is saved with git stash command. We can check the status of the repository. ![]() As you can see, my work is just stashed in its current position. Now, the directory is cleaned. At this point, you can switch between branches and work on them. Git Stash Save (Saving Stashes with the message):In Git, the changes can be stashed with a message. To stash a change with a message, run the below command: Syntax: Output: ![]() The above stash will be saved with a message Git Stash List (Check the Stored Stashes)To check the stored stashes, run the below command: Syntax: Output: ![]() In the above case, I have made one stash, which is displayed as "stash@{0}: WIP on the test: 0a1a475 CSS file". If we have more than one stash, then It will display all the stashes respectively with different stash id. Consider the below output: ![]() It will show all the stashes with indexing as stash@{0}: stash@{1}: and so on. Git Stash ApplyYou can re-apply the changes that you just stashed by using the git stash command. To apply the commit, use the git stash command, followed by the apply option. It is used as: Syntax: Output: ![]() The above output restores the last stash. Now, if you will check the status of the repository, it will show the changes that are made on the file. Consider the below output: ![]() From the above output, you can see that the repository is restored to its previous state before stash. It is showing output as "Changes not staged for commit." In case of more than one stash, you can use "git stash apply" command followed by stash index id to apply the particular commit. It is used as: Syntax: Consider the below output: Output: ![]() If we don't specify a stash, Git takes the most recent stash and tries to apply it. Git Stash ChangesWe can track the stashes and their changes. To see the changes in the file before stash and after stash operation, run the below command: Syntax: The above command will show the file that is stashed and changes made on them. Consider the below output: Output: ![]() The above output illustrates that there are two files that are stashed, and two insertions performed on them. We can exactly track what changes are made on the file. To display the changed content of the file, perform the below command: Syntax: Here, -p stands for the partial stash. The given command will show the edited files and content, consider the below output: Output: ![]() The above output is showing the file name with changed content. It acts the same as git diff command. The git diff command will also show the exact output. Git Stash Pop (Reapplying Stashed Changes)Git allows the user to re-apply the previous commits by using git stash pop command. The popping option removes the changes from stash and applies them to your working file. The git stash pop command is quite similar to git stash apply. The main difference between both of these commands is stash pop command that deletes the stash from the stack after it is applied. Syntax: The above command will re-apply the previous commits to the repository. Consider the below output. Output: ![]() Git Stash Drop (Unstash)The git stash drop command is used to delete a stash from the queue. Generally, it deletes the most recent stash. Caution should be taken before using stash drop command, as it is difficult to undo if once applied. The only way to revert it is if you do not close the terminal after deleting the stash. The stash drop command will be used as: Syntax: Output: ![]() In the above output, the most recent stash (stash@{0}) has been dropped from given three stashes. The stash list command lists all the available stashes in the queue. We can also delete a particular stash from the queue. To delete a particular stash from the available stashes, pass the stash id in stash drop command. It will be processed as: Syntax: Assume that I have two stashes available in my queue, and I don't want to drop my most recent stash, but I want to delete the older one. Then, it will be operated as: Consider the below output: ![]() In the above output, the commit stash@{1} has been deleted from the queue. Git Stash ClearThe git stash clear command allows deleting all the available stashes at once. To delete all the available stashes, operate below command: Syntax: it will delete all the stashes that exist in the repository. Output: ![]() All the stashes are deleted in the above output. The git stash list command is blank because there are no stashes available in the repository. Git Stash BranchIf you stashed some work on a particular branch and continue working on that branch. Then, it may create a conflict during merging. So, it is good to stash work on a separate branch. The git stash branch command allows the user to stash work on a separate branch to avoid conflicts. The syntax for this branch is as follows: Syntax: The above command will create a new branch and transfer the stashed work on that. Consider the below output: Output: ![]() In the above output, the stashed work is transferred to a newly created branch testing. It will avoid the merge conflict on the master branch. Next TopicGit Ignore
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com