Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Stashing in Git

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 701
    Comment on it

    Hi Friends,

    Git is the great tool for project versioning and code management, but mostly we are unaware so many things. So here I am going to tell you what is stashing in Git.

    Sometimes we are working on some code and we don't want it to commit and we need to switch branch, and whenever we try to switch from it to another branch, it always says gives the message "Commit your changes or stash them". So stashing is basically a place where you can keep your changes without committing them, so whenever you are switching to another branch and after that come to your previous branch, you can retrieve your changes from stash.


    So the best to understand stashing is doing practice. So lets go into one of your project and change few files and then run git status

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
      modified:   app/controllers/application_controller.rb
    
    no changes added to commit (use "git add" and/or "git commit -a")

     

    Now what you want to do is that you don't want to commit the change in the file and you want to switch to another branch, so you can stash the changes by doing git stash.

     

    $ git stash
    Saved working directory and index state WIP on master: 1c3ab1a Adding Locations with Article and Getting articles with filters location
    HEAD is now at 1c3ab1a Adding Locations with Article and Getting articles with filters location


    Now if you check again the status, you will see

     

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    nothing to commit, working directory clean

     

     

    So now you are free to switch branches and do whatever you want to do.

    To list all the stash in your project, you can hit git stash list, in our case we have only one, so we will get only one stashed index

     

    $ git stash list
    stash@{0}: WIP on master: 1c3ab1a Adding Locations with Article and Getting articles with filters location


    Now lets add another stash and see what will happen, so change any file and then again do git stash and then again to git stash list

     

    $ git stash list
    stash@{0}: WIP on master: 1c3ab1a Adding Locations with Article and Getting articles with filters location
    stash@{1}: WIP on master: 1c3ab1a Adding Locations with Article and Getting articles with filters location
    
    So we can see, we have 2 stash here. We can perform multiple operations here:
    
    
    # To get the last stashed change
    git stash apply
    
    
    # To get the any specific stash change
    stash apply stash@{n} #n is the one stash you want to retrieve
    
    
    # To drop any particular stash
    git stash drop stash@{n} #n is the one stash you want to drop
    
    
    # Reversing the applied stash or unapplying the stash
    git stash show -p stash@{n} | git apply -R
    
    
    # Clearing all the stashes
    git stash clear

     

    Hope you enjoyed reading my blog.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: