git Tutorial
Introduction Installing Git Understanding Git architecture Initializing the new Git respository Cloning new Git repository .git directory in Git repository Viewing Git configuration First time Git configuration Using SSH keys with Git Checking Git status Adding files into staging area in Git Committing changes to the local repository Ignoring files using .gitignore Viewing the history of commits Viewing the file contents in working directory, staging area and repository Viewing difference between files Viewing the history of files Git revert Git Reset git reflog Creating and switching to new branches in Git Viewing existing branches Merging branches Resolving conflicts Rebase Deleting branches Renaming the branch in Git Push Pull Stashing the changes in Git Tags in Git Patches in Git Creating new repository on GitHub Pushing local repository to remote repository like GitHub IntelliJ IDEAStashing the changes in git
We often need to switch branches while working on different features or bug fixes. Most of the times we are in the middle of changing something on Branch A and suddenly we need to switch to different branch to fix something that is very urgent. In such situations, we are not in a position to commit the changes as we have not finished the development yet in current branch. If we switch to the different branch, uncommitted changes are also seen in other branch which we usually do not want. So what to do in these situations? That’s when Git stash comes into picture. We can stash the uncommitted changes which puts the branch in last commit status and working directory becomes clean. Then we can safely switch to other branch, complete the work in that branch then switch to back earlier branch and get the stashed changes back to the working directory. Below image shows that we have modified the file f1.txt in master branch. Below image shows that we have switched to branch b1 and the changes from master branch are also visible there. We do not want that…So let us use stash. To save the changes in stash, you can use below command. git stash save “stash1” After putting the changes on stash, working directory gets clean and you can switch to other branch and after finishing work in other branch you can come back to main branch. Then we can apply the stash so that saved changes would appear in working directory. Applying the stash To apply the most recent stash, you can use below command.
git stash apply
To apply the specific stash, you can use below command.
git stash apply stash@{2}
Below image shows the above command in action Clearing the stash items When you apply the stash, that stash is not removed from stash list. You can use below command to remove item from stash.
git stash drop stash@1Below command applies the most recent stash and then removes it from stash
git stash pop
Viewing all items in stash You can view all items in stash using below command.
git stash list
Clearing all entries in stash You can clear all entries from stash using below command.
git stash clear
Here is the output of above example.
Web development and Automation testing
solutions delivered!!