Git Tutorial: Using the Stash Command

Corey Schafer · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

The video demonstrates the use of the Git stash command to save and manage changes in a temporary place, allowing for easier switching between branches and version control. The tutorial covers various Git stash commands, including save, list, apply, pop, delete, drop, and clear.

Full Transcript

hey what's up everybody in this video I thought we could take a look at the git stash command uh the git stash command is great whenever you have uh some changes that you're not quite ready to commit and maybe you need to uh switch branches or uh even uh just you want to revert back temporarily to where it was that you started um and you don't know what to do with your changes uh well you can do a stash on those changes and it will save them in a temporary place to where you can go and do what it is that you need to do and then come back to those changes later on uh so the best way is just to show you an example here um so I've got a very simple Pi file over here it's just an add subtract multiply and divide uh functions and right now uh they don't have any return values so let's say that we wanted to start working on this uh python file so we could just do a um git Branch ad because we want to work on the ad function first and then we can check that out get checkout ad and now let's go in here and start working on our ad function so we can do return a plus b and save that now if I go over here and do a get diff you can see that we have uh changed these lines here now let's say for whatever reason we need to uh switch back to our Master branch and check something out or we need to see uh what our um Pi file looked like before we started messing with it um so what we can do here is we can just stash our changes and to go back to where we were so to do that we can do a get stash save and now we need to type in a message for ourselves to remind us of what this um stash was doing so I can just say worked on ADD function so hit enter there and you can see that it is uh now saved and now if I clear this out and do a get diff nothing shows up and if I do a get status you can see that there's nothing to commit and if I go back over here to my file you can see that all the changes that we made are now gone they've been wiped out but they're not gone for good if I do now we just stashed it so if I do a get stash list then you can see here it's a stash and all the stashes have these um IDs here this one is zero and you can see the message that we put in uh that we gave ourselves to remind us of what what these changes were so I said that we worked on our ad function so now you can go and do whatever it is that you needed to do you can switch branches uh you know work on other things but whenever you're ready to come back uh to these changes that you made um so we want to bring these changes back there's two ways we can do this we can do get stash apply and then let's copy that and just paste that in there if we do a get stash apply I'm going to go ahead and hit enter now you can see that it has made these changes again if I go back to my file you can see that our changes are now back but now if I do a get stash list again uh you can see that our stash is still there it didn't get rid of it that's because when we do get stash apply uh we're uh taking all the changes that were in that stash but we're not getting rid of the stash so it's still there um so let me do a get checkout and I'm just going to that just resets us back to where we were so now if I go back to the file this is where we started from and so now instead of doing let me do a get stash list instead of doing a get stash apply to fetch those changes I'm going to do a get stash pop and what pop will do is it will grab the very first stash in the list of stashes and we only have one here so it's going to be the top one and then it's going to apply those changes and then it's going to drop the stash so now if I do a get stash list you can see that we don't have any stashes available but even though it deleted that stash uh if I click over here on the file it did give us our changes back so if I do a get diff uh you can see where we made our changes here now let's say that I just decided to go ahead and go and uh fill in the uh rest of these functions here so I can do a minus B turn a Time B and return a / B save that so if I do a get diff you can see we have all of our changes Here and Now what if somebody told us that they wanted that uh they wanted us to add in a square function um well let's go ahead and stash the changes that we've made so far so get stash change and we can just do um we'll call these Cal functions and now just to make sure that that worked if I do a get stash list you can see that we have our stashed value here and if I click back over here onto the file you can see that it set everything back uh to what we started with so now if I want to add in this squared function then I can just do a square there and save that and now if I do a get diff then you can see uh that we have those changes of adding in uh that squared function but now let's say that you know maybe we need to change branches again or something like that so we're going to go ahead and stash these changes so let's do get stash save and I'll say added Square function that's okay that I misspelled that so now I do a get stash list and you can see that it pushed our Cal functions down down to the stash one and now our most recent change is up here at stash zero so if if at this point I get did a get stash pop then it's going to uh pop off the uh squared function because it was in that top position um so now let me go ahead and if I did a get stash list you can see that it popped that one up PO that one off this one's back to the zero position now I'm going to stash this again with the same message that we had before and if I could do a get stash list you can see that now it put that square function right back on top to where it was now what if somebody uh came and told us that um you know all of a sudden we don't need the square function anymore um so all these changes we we can just go ahead and get rid of these um so to do that we can do a get stash drop and and it's just like get stash apply we got we're going to take the name of the one that we want to drop here so I'm going to get the one that is on the ad Square function I'm just going to paste that in so we do our get stash drop with the stash that we want which is our top one hit enter and then if I go back over here to my file uh you can see that it is where we started and if I go back over here to my terminal and do a get stash list then you you can see that the only stash that we have is our changes to the Cal functions and so one more time just here for uh an example let me go ahead and make a couple more changes over here I'm going to make one more stash I'm just going to put the square function back in here again and let me just go ahead and do a get stash save add Square function again get stash list and you can see now we have our two stashes again and now we say that we wanted to get rid of all of our stashes and not just one stash um so this is going to get rid of all the changes that we have in there now be careful when you run this because um it's going to get rid of all the stashes that you have but say that all those changes that you made were just junk and you no longer want them anymore you can just run get stash clear and what that's going to do is it's going to you can see that our file here is exactly the way it was whenever uh we first started and if I do a get stash list we don't have any of those stashes with any of the changes that we made so that is a brief overview of uh the ways that you're most likely uh going to use get stash most of the time um one way that this really comes in handy is if I check out my master Branch here um say that I went in and made uh my changes to add right now so I did um return a plus b and save that and then I did a get status and I saw that I modified the file and a get diff and I can see the lines that I changed here but then I realize that I'm on my master branch and whenever I try to change to my uh ad Branch it's saying hey you have uncommitted files here uh we can't do that um and you don't want to commit those changes to your master branch and you meant to do that on your ad Branch well the best way to do that is just to do a get stash because stash is over from Branch to Branch so right here I can do a get stash save and just call this add function there and then so now if I do get status you can see that there's nothing to commit and I'll do get checkout add and then get stash pop which Pops at top change off and get diff and then from there um those changes that you meant to commit to add now you can do that just um add all your changes and you know put in a commit message there so that's a brief overview of get stash and a couple of examples of how you would use that um you know hopefully this uh uh quick video was useful for you guys if you have any questions about it you know just ask in the comment section below um be sure to subscribe for future tips and tutorials and thank you guys for watching what

Original Description

In this video, we will take a look at the git stash command. Git stash is extremely useful when you have some changes that you want to save but aren't ready to make a commit. There are many different scenarios where knowing how to use stash properly will make your life with git much easier. ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Git
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 30 of 60

1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

The video teaches how to use the Git stash command to temporarily store changes, switch between branches, and manage stashes. This skill is essential for effective version control and branch management. By mastering the Git stash command, developers can work more efficiently and avoid common pitfalls.

Key Takeaways
  1. Create a new branch using git branch
  2. Switch to the new branch using git checkout
  3. Make changes to a file and use git diff to review them
  4. Use git stash save to temporarily store the changes
  5. Use git stash list to view all stashes
  6. Use git stash apply to apply changes from a stash
  7. Use git stash pop to apply changes from the first stash and delete it
  8. Use git stash delete, drop, or clear to manage stashes
💡 The Git stash command is a powerful tool for managing changes and switching between branches, and mastering it can greatly improve a developer's workflow.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →