Git Tutorial: Using the Stash Command

Corey Schafer
17 Apr 201510:32

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

00:00

💻 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'.

05:01

🔄 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.

10:02

📚 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

The 'git stash' command is a feature in Git version control system that allows users to temporarily store modified, tracked files without committing them. It's useful for cleaning up the working directory without losing changes, which can be particularly helpful when switching branches or when you need to revert to a previous state. In the video, the command is used to save changes made to a Python file without committing them, allowing the user to switch branches and then reapply the changes later.

💡git branch

The 'git branch' command is used to create a new branch in Git. Branches in Git are pointers to a commit, and creating a new branch allows developers to work on different features or bug fixes independently. In the video, the user creates a new branch called 'ad' to work on the 'add' function separately from the main development line.

💡git checkout

The 'git checkout' command is used to switch branches or restore working tree files. It is essential for navigating between different branches in a Git repository. In the video, the user checks out a new branch to start working on a specific feature and later checks out the master branch to switch back to the main development line.

💡git diff

The 'git diff' command shows the difference between the working tree and the index or a named commit, or between two commits. It is a crucial tool for reviewing changes before committing them. In the video, 'git diff' is used to display the changes made to the Python file before stashing them.

💡git status

The 'git status' command displays the state of the working directory and the staging area. It shows which changes have been staged, which haven't, and which files are untracked. In the video, 'git status' is used to check the status of the working directory before and after stashing changes.

💡stash apply

The 'git stash apply' command re-applies a stashed change to the working directory. Unlike 'git stash pop', 'apply' does not remove the stashed state from the stash list, allowing the user to reapply the same changes multiple times if needed. In the video, 'apply' is used to reapply the changes made to the 'add' function after switching back to the branch where the changes were originally made.

💡stash pop

The 'git stash pop' command applies the most recent stashed changes and removes them from the stash list. It is a convenient way to reapply and then discard a stash in one step. In the video, 'pop' is used to bring back the changes made to the Python file and then automatically remove the stash entry.

💡stash list

The 'git stash list' command shows the list of all stashed changes. Each stash is given an ID and a message that was provided when the stash was created. This command helps users keep track of their stashes and choose which one to apply or drop. In the video, 'stash list' is used to view the available stashes and their messages.

💡stash drop

The 'git stash drop' command removes a specific stashed state from the stash list. It is used when the changes in a stash are no longer needed, allowing the user to clean up the stash list. In the video, 'stash drop' is used to remove a stash that contained changes for a function that was later deemed unnecessary.

💡stash clear

The 'git stash clear' command removes all the stashed states. It is a way to clean up all stashed changes at once, which is useful when all stashes are no longer needed. In the video, 'stash clear' is mentioned as a way to get rid of all stashes if the changes stashed were not required anymore.

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

play00:00

hey what's up everybody in this video I

play00:02

thought we could take a look at the git

play00:04

stash command uh the git stash command

play00:07

is great whenever you have uh some

play00:09

changes that you're not quite ready to

play00:10

commit and maybe you need to uh switch

play00:13

branches or uh even uh just you want to

play00:17

revert back temporarily to where it was

play00:19

that you started um and you don't know

play00:22

what to do with your changes uh well you

play00:23

can do a stash on those changes and it

play00:26

will save them in a temporary place to

play00:29

where you can go and do what it is that

play00:31

you need to do and then come back to

play00:32

those changes later on uh so the best

play00:35

way is just to show you an example here

play00:37

um so I've got a very simple Pi file

play00:41

over here it's just an add subtract

play00:43

multiply and divide uh functions and

play00:47

right now uh they don't have any return

play00:49

values so let's say that we wanted to

play00:51

start working on this uh python file so

play00:54

we could just do a um git Branch ad

play00:59

because we want to work on the ad

play01:00

function first and then we can check

play01:03

that out get checkout ad and now let's

play01:05

go in here and start working on our ad

play01:07

function so we can do return a plus b

play01:11

and save that now if I go over here and

play01:14

do a get diff you can see that we have

play01:17

uh changed these lines here now let's

play01:20

say for whatever reason we need to uh

play01:23

switch back to our Master branch and

play01:24

check something out or we need to

play01:27

see uh what our um Pi file looked like

play01:33

before we started messing with it um so

play01:35

what we can do here is we can just stash

play01:37

our changes and to go back to where we

play01:39

were so to do that we can do a get stash

play01:43

save and now we need to type in a

play01:45

message for ourselves to remind us of

play01:47

what this um stash was doing so I can

play01:51

just say worked on ADD function so hit

play01:55

enter there and you can see that it is

play01:58

uh now saved and now if I clear this out

play02:01

and do a get diff nothing shows up and

play02:05

if I do a get status you can see that

play02:07

there's nothing to commit and if I go

play02:09

back over here to my file you can see

play02:12

that all the changes that we made are

play02:14

now gone they've been wiped out but

play02:16

they're not gone for good if I do now we

play02:19

just stashed it so if I do a get stash

play02:21

list then you can see here it's a stash

play02:25

and all the stashes have these um IDs

play02:28

here this one is zero and you can see

play02:30

the message that we put in uh that we

play02:32

gave ourselves to remind us of what what

play02:35

these changes were so I said that we

play02:37

worked on our ad function so now you can

play02:40

go and do whatever it is that you needed

play02:41

to do you can switch branches uh you

play02:44

know work on other things but whenever

play02:46

you're ready to come back uh to these

play02:48

changes that you made um so we want to

play02:51

bring these changes back there's two

play02:52

ways we can do this we can do get stash

play02:56

apply and then let's copy that and just

play03:00

paste that in there if we do a get stash

play03:02

apply I'm going to go ahead and hit

play03:04

enter now you can see that it has made

play03:06

these changes again if I go back to my

play03:08

file you can see that our changes are

play03:10

now back but now if I do a get stash

play03:14

list again uh you can see that our stash

play03:17

is still there it didn't get rid of it

play03:20

that's because when we do get stash

play03:21

apply uh we're uh taking all the changes

play03:25

that were in that stash but we're not

play03:26

getting rid of the stash so it's still

play03:28

there um so let me do a get checkout and

play03:33

I'm just going to that just resets us

play03:35

back to where we were so now if I go

play03:38

back to the file this is where we

play03:40

started from and so now instead of doing

play03:44

let me do a get stash list instead of

play03:46

doing a get stash apply to fetch those

play03:48

changes I'm going to do a get stash pop

play03:52

and what pop will do is it will grab the

play03:55

very first stash in the list of stashes

play03:58

and we only have one here so it's going

play03:59

to be the top one and then it's going

play04:02

to apply those changes and then it's

play04:05

going to drop the stash so now if I do a

play04:07

get stash list you can see that we don't

play04:10

have any stashes available but even

play04:12

though it deleted that stash uh if I

play04:14

click over here on the file it did give

play04:17

us our changes back so if I do a get

play04:19

diff uh you can see where we made our

play04:22

changes here now let's say that I just

play04:25

decided to go ahead and go and uh fill

play04:28

in the uh rest of these functions here

play04:30

so I can do a minus B

play04:35

turn a Time B and return a / B save that

play04:42

so if I do a get diff you can see we

play04:44

have all of our changes Here and Now

play04:47

what if somebody told us that they

play04:48

wanted that uh they wanted us to add in

play04:51

a square function um well let's go ahead

play04:54

and stash the changes that we've made so

play04:56

far so get stash change and we can just

play04:59

do

play05:00

um we'll call these Cal functions and

play05:04

now just to make sure that that worked

play05:05

if I do a get stash list you can see

play05:08

that we have our stashed value here and

play05:11

if I click back over here onto the file

play05:13

you can see that it set everything back

play05:15

uh to what we started with so now if I

play05:20

want to add in this squared function

play05:22

then I can just do a square there and

play05:26

save that and now if I do a get diff

play05:29

then you can see uh that we have those

play05:33

changes of adding in uh that squared

play05:35

function but now let's say that you know

play05:38

maybe we need to change branches again

play05:40

or something like that so we're going to

play05:41

go ahead and stash these changes so

play05:44

let's do get stash save and I'll say

play05:48

added Square function that's okay that I

play05:51

misspelled that so now I do a get stash

play05:55

list and you can see that it pushed our

play05:58

Cal functions down down to the stash one

play06:02

and now our most recent change is up

play06:05

here at stash zero so if if at this

play06:09

point I get did a get stash pop then

play06:13

it's going to uh pop off

play06:16

the uh squared function because it was

play06:19

in that top position um so now let me go

play06:22

ahead and if I did a get stash list you

play06:26

can see that it popped that one up PO

play06:28

that one off this one's back to the zero

play06:30

position now I'm going to stash this

play06:32

again with the same message that we had

play06:35

before and if I could do a get stash

play06:38

list you can see that now it put that

play06:41

square function right back on top to

play06:42

where it was now what if somebody uh

play06:45

came and told us that um you know all of

play06:47

a sudden we don't need the square

play06:49

function anymore um so all these changes

play06:52

we we can just go ahead and get rid of

play06:53

these um so to do that we can do a get

play06:57

stash drop and and it's just like get

play07:00

stash apply we got we're going to take

play07:02

the name of the one that we want to drop

play07:04

here so I'm going to get the one that is

play07:06

on the ad Square function I'm just going

play07:08

to paste that in so we do our get stash

play07:11

drop with the stash that we want which

play07:14

is our top one hit enter and then if I

play07:17

go back over here to my file uh you can

play07:20

see that it is where we started and if I

play07:23

go back over here to my terminal and do

play07:25

a get stash

play07:28

list then you you can see that the only

play07:30

stash that we have is our changes to the

play07:33

Cal functions and so one more time just

play07:36

here for uh an example let me go ahead

play07:39

and make a couple more changes over here

play07:41

I'm going to make one more stash I'm

play07:42

just going to put the square function

play07:44

back in here again and let me just go

play07:47

ahead and do a get stash save add Square

play07:50

function again get stash list and you

play07:53

can see now we have our two stashes

play07:56

again and now we say that we wanted to

play07:59

get rid of all of our stashes and not

play08:01

just one stash um so this is going to

play08:04

get rid of all the changes that we have

play08:05

in there now be careful when you run

play08:07

this because um it's going to get rid of

play08:11

all the stashes that you have but say

play08:13

that all those changes that you made

play08:15

were just junk and you no longer want

play08:16

them anymore you can just run get stash

play08:19

clear and what that's going to do is

play08:21

it's going to you can see that our file

play08:23

here is exactly the way it was whenever

play08:26

uh we first started and if I do a get

play08:29

stash list we don't have any of those

play08:32

stashes with any of the changes that we

play08:34

made so that is a brief overview of uh

play08:38

the ways that you're most likely uh

play08:41

going to use get stash most of the time

play08:44

um one way that this really comes in

play08:46

handy is if I check out my master Branch

play08:49

here um say that I went in and made uh

play08:53

my changes to add right now so I did um

play08:57

return a plus b and save that and then I

play09:01

did a get status and I saw that I

play09:04

modified the file and a get diff and I

play09:06

can see the lines that I changed here

play09:08

but then I realize that I'm on my master

play09:10

branch and whenever I try to change to

play09:13

my uh ad Branch it's saying hey you have

play09:16

uncommitted files here uh we can't do

play09:18

that um and you don't want to commit

play09:21

those changes to your master branch and

play09:23

you meant to do that on your ad Branch

play09:25

well the best way to do that is just to

play09:27

do a get stash because stash is over

play09:29

from Branch to Branch so right here I

play09:32

can do a get stash save and just call

play09:36

this add function there and then so now

play09:40

if I do get status you can see that

play09:42

there's nothing to commit and I'll do

play09:44

get

play09:45

checkout add and then get stash pop

play09:50

which Pops at top change off and get

play09:52

diff and then from there um those

play09:55

changes that you meant to commit to add

play09:58

now you can do that just um add all your

play10:01

changes and you know put in a commit

play10:05

message

play10:08

there so that's a brief overview of get

play10:11

stash and a couple of examples of how

play10:13

you would use that um you know hopefully

play10:15

this uh uh quick video was useful for

play10:18

you guys if you have any questions about

play10:20

it you know just ask in the comment

play10:21

section below um be sure to subscribe

play10:24

for future tips and tutorials and thank

play10:26

you guys for watching

play10:29

what

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
Git StashVersion ControlCode ManagementGit TutorialSoftware DevelopmentCoding TipsGit CommandsBranching StrategyDeveloper ToolsCode Stashing
Besoin d'un résumé en anglais ?