DEV Community

Discussion on: Git Explained: The Basics

Collapse
 
milu_franz profile image
Milu • Edited

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 hope these explanations help!

Thread Thread
 
damsalem profile image
Dani Amsalem

Thank you for the excellent, detailed explanations Milu. You are a superb educator.