I have learned bits and pieces of Git over the last year and a half, but I still struggle with it.
I really liked these two linesIt is not advised to work directly on the master branch because there is a big chance you would have a broken project while you are adding some progress. Instead, you want to create a different branch, work on your changes or new feature and when you are done and everything is working as expected, merge that branch into master.
But I was very confused with the first line explaining Git stash:Saves any changes you have made locally and it reverts the working directory to match the HEAD of your last commit.
Do you have any more information you can link to explaining Git stash?
While reading your next article I noticed one of your images showed where "Head" is in the Git timeline. I've invented this meaning for "Head", the active point in the Git timeline where you are working.
"Working directory" would likely be the directory (folder) containing the files you are working on.
So my understanding of Git stash is: It lets you add your files to Git (with no commit?) and then see the state of your files in your last commit.
I'm hoping you can confirm/deny this explanation :)
Hi Dani,
This is are great question! To start answering you, I’m going to provide you with some definitions that might clear up some of your questions.
HEAD: a reference to the last commit in the current checkout branch. So when you are in your master branch, HEAD is in the last commit of master. If you do git checkout new-branch, HEAD would be at the last commit of the new-branch branch.
Working directory: the current local directory where you are working on. So if you have two branches (master and new-feature), you checkout new-feature branch, and changed a file. Your working directory in new-branch has uncommitted changes, however it is tracking all those changes. If you do git status, you are going to see a list of all the files you have modified in your working directory but haven’t committed yet.
Git Stash is a really cool tool because it allows you to save those changes separately without committing them.
Problem - let’s say you are in the middle of creating a new feature in a branch called new-feature. A team member needs your help in another branch and you have to take care of that right now. You don’t want to commit the changes done for that new feature yet because it’s not in a working state. You cannot checkout this other branch though because git is yelling at you telling you have unsaved changes.
Possible solution using git stash - You can use git stash and git will save all those changes separately. If you then do git status, you won’t see any of the changes you made in your current working directory, it will be all clear as if you just made the latest commit. Then you can checkout the other branch, take care of whatever you team member needed, then go back to your new feature branch and use git stash pop to recover all those changes you want to continue working on. You don’t have to use it, I find it super helpful though.
I have learned bits and pieces of Git over the last year and a half, but I still struggle with it.
I really liked these two lines It is not advised to work directly on the master branch because there is a big chance you would have a broken project while you are adding some progress. Instead, you want to create a different branch, work on your changes or new feature and when you are done and everything is working as expected, merge that branch into master.
But I was very confused with the first line explaining Git stash: Saves any changes you have made locally and it reverts the working directory to match the HEAD of your last commit.
Do you have any more information you can link to explaining Git stash?
While reading your next article I noticed one of your images showed where "Head" is in the Git timeline. I've invented this meaning for "Head", the active point in the Git timeline where you are working.
"Working directory" would likely be the directory (folder) containing the files you are working on.
So my understanding of Git stash is: It lets you add your files to Git (with no commit?) and then see the state of your files in your last commit.
I'm hoping you can confirm/deny this explanation :)
Hi Dani,
This is are great question! To start answering you, I’m going to provide you with some definitions that might clear up some of your questions.
HEAD: a reference to the last commit in the current checkout branch. So when you are in your master branch, HEAD is in the last commit of master. If you do
git checkout new-branch, HEAD would be at the last commit of the new-branch branch.Working directory: the current local directory where you are working on. So if you have two branches (master and new-feature), you checkout new-feature branch, and changed a file. Your working directory in new-branch has uncommitted changes, however it is tracking all those changes. If you do
git status, you are going to see a list of all the files you have modified in your working directory but haven’t committed yet.Git Stash is a really cool tool because it allows you to save those changes separately without committing them.
Problem - let’s say you are in the middle of creating a new feature in a branch called new-feature. A team member needs your help in another branch and you have to take care of that right now. You don’t want to commit the changes done for that new feature yet because it’s not in a working state. You cannot checkout this other branch though because git is yelling at you telling you have unsaved changes.
Possible solution using git stash - You can use
git stashand git will save all those changes separately. If you then dogit status, you won’t see any of the changes you made in your current working directory, it will be all clear as if you just made the latest commit. Then you can checkout the other branch, take care of whatever you team member needed, then go back to your new feature branch and usegit stash popto recover all those changes you want to continue working on. You don’t have to use it, I find it super helpful though.I hope these explanations help!
Thank you for the excellent, detailed explanations Milu. You are a superb educator.