Git Tutorial: Using the Stash Command
Summary
TLDRThis video tutorial explores the Git stash command, a useful tool for temporarily saving changes when you're not ready to commit them. It demonstrates how to use 'git stash save' to store modifications, 'git stash list' to view stashes, and 'git stash apply' or 'git stash pop' to reapply changes. The video also covers 'git stash drop' to remove stashes and 'git stash clear' to delete all stashes. Practical examples are provided, such as switching branches without committing and handling uncommitted changes across branches.
Takeaways
- 📚 The git stash command is useful for temporarily saving changes that are not ready to be committed.
- 🔄 Git stash allows you to switch branches without committing changes, and revert to the previous state of the working directory.
- 💾 Stashed changes are saved with a unique ID and a descriptive message to help you remember what was stashed.
- 🔍 You can view the list of stashes with 'git stash list', which shows the stash ID and the message associated with each stash.
- 🔙 To reapply stashed changes, you can use 'git stash apply' followed by the stash ID, which reapplies the changes but does not remove the stash.
- 📑 'git stash pop' is used to apply the most recent stash and then remove it from the stash list.
- 🗑️ 'git stash drop' can be used to remove a specific stash from the stash list, freeing up space and cleaning up the stash history.
- 🧼 'git stash clear' is a command to remove all stashes at once, which is useful for discarding all temporary changes.
- 🔀 Stashing is particularly helpful when you need to switch branches but have uncommitted changes that you don't want to commit on the current branch.
- 🛠️ The script provides a practical example of using git stash to work on a Python file, demonstrating how to save, apply, and drop stashes.
Q & A
What is the primary purpose of the git stash command?
-The git stash command is used to temporarily save changes made to tracked files in a dirty working directory, allowing you to switch branches or revert to a clean state without committing the changes.
How can you create a stash in Git?
-You can create a stash by using the command `git stash save` followed by a descriptive message to remind yourself of what the stash contains.
What happens to your working directory after stashing changes?
-After stashing changes, your working directory is cleaned up, and the changes are no longer visible in the file system, but they are saved in a stash.
How can you view the list of stashes you have created?
-You can view the list of stashes by using the command `git stash list` which shows all the stashes along with their IDs and messages.
What is the difference between `git stash apply` and `git stash pop`?
-`git stash apply` re-applies the changes from a stash to the working directory but keeps the stash in the stash list, whereas `git stash pop` both reapplies the changes and removes the stash from the list.
How can you remove a specific stash in Git?
-You can remove a specific stash by using the command `git stash drop` followed by the stash's ID or index number.
What does `git stash clear` do?
-`git stash clear` is used to remove all stashes from the stash list, effectively discarding all the changes that were stashed.
How can you apply the most recent stash and remove it from the list?
-You can apply the most recent stash and remove it from the list by using the command `git stash pop`.
What is a common use case for `git stash` when switching branches?
-A common use case for `git stash` when switching branches is when you have uncommitted changes on one branch and need to switch to another branch without committing or discarding those changes.
How can you add a new function to your code and stash the changes before switching branches?
-You can add a new function to your code, then use `git stash save` with a message to stash the changes, and then switch branches using `git checkout`.
Outlines
💻 Introduction to Git Stash
This paragraph introduces the Git stash command, which is useful for temporarily saving changes that are not ready to be committed. The video demonstrates how to use git stash when you need to switch branches or revert to a previous state without committing your changes. The presenter uses a simple Python file as an example, showing how to work on the 'add' function and then stash the changes before switching back to the master branch. The process of stashing changes, listing stashes, and applying them back to the working directory is explained, including the difference between 'git stash apply' and 'git stash pop'.
🔄 Managing Changes with Git Stash
The second paragraph delves deeper into managing changes with Git stash. It illustrates how to add a new function, 'square', to the Python file and then stash the changes. The video shows how to list stashes, apply them, and pop them off the stash list. It also addresses the scenario where you might need to remove a stash, either individually using 'git stash drop' or clearing all stashes with 'git stash clear'. The paragraph concludes with an example of how to use git stash when you have uncommitted changes on a branch you want to switch from, emphasizing its utility in branch management.
📚 Conclusion and Call to Action
The final paragraph wraps up the tutorial on Git stash by summarizing the key points covered in the video. The presenter encourages viewers to ask questions in the comments section if they have any doubts and reminds them to subscribe for more tips and tutorials. The paragraph serves as a conclusion, highlighting the practicality of Git stash for managing changes across different branches and its role in efficient Git workflows.
Mindmap
Keywords
💡git stash
💡git branch
💡git checkout
💡git diff
💡git status
💡stash apply
💡stash pop
💡stash list
💡stash drop
💡stash clear
Highlights
Introduction to the git stash command and its utility for managing changes.
Demonstration of creating a new branch 'ad' to work on the add function.
Explanation of how to use git stash save to temporarily store changes.
Guidance on adding a descriptive message when stashing changes.
Use of git stash list to view stashed changes and their IDs.
Application of git stash apply to reapply stashed changes.
Clarification that git stash apply does not remove the stash.
Introduction of git stash pop for applying and removing stashed changes.
Example of stashing changes when adding a new function to avoid conflicts.
Illustration of how to manage stashes when switching between branches.
Tutorial on using git stash drop to remove a specific stash.
Explanation of git stash clear to remove all stashes at once.
Scenario of stashing changes when needing to switch branches unexpectedly.
Step-by-step guide on how to apply stashed changes to the correct branch.
Conclusion summarizing the practical uses of git stash.
Encouragement for viewers to ask questions and subscribe for more tutorials.
Transcripts
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
Voir Plus de Vidéos Connexes
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git and GitHub - 0 Experience to Professional in 1 Tutorial (Part 1)
13 Advanced (but useful) Git Techniques and Shortcuts
Git Tutorial For Dummies
Day-11 | Git Interview Q&A and Commands for DevOps | Real World Example |#devops #github #git #2023
Day-9 | Git and GitHub | What is GIT ? | What is Version Control ? | #devops #2023 #github #gitlab
5.0 / 5 (0 votes)