DEV Community

Cover image for Git Explained: The Basics

Git Explained: The Basics

Milu on May 09, 2020

Git is scary when you are starting up. There is this imminent fear that you could possibly lose hours of hard work by using the wrong commands. Kee...
Collapse
 
aritdeveloper profile image
Arit Developer

This is AMAZING! Please keep the git tutorials coming 🙏🏽 Your explainations are quite beginner-friendly 😄

Collapse
 
milu_franz profile image
Milu

Thank you Arit! I'm glad you found my explanations easy to follow. I will make sure to continue providing beginner-friendly git content in my future posts :)

Collapse
 
damsalem profile image
Dani Amsalem

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?

Collapse
 
damsalem profile image
Dani Amsalem

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 :)

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.

Collapse
 
gcristiantoma profile image
gcristiantoma

Git push origin [branch-name] , I would like to ask, origin stands for ? Thanks for answering and I love your post :)

Collapse
 
milu_franz profile image
Milu

This is a great question Cristian! origin stands for your remote repository. So when you use "git push origin [branch-name]", you are saying push my code to a branch called [branch-name] in this specific remote repository called "origin". Most of the time you only have one remote repository and "origin" is the default name git gives to it. You can change the default name "origin" to whatever you want using "git remote rename origin [new-remote-name]". Hope this helps!

Collapse
 
gcristiantoma profile image
gcristiantoma

Thanks for answering , very kind ! :) so it's a good practice to have just one repository, not to create one for each new project ? Sorry for the confusion :D

Thread Thread
 
milu_franz profile image
Milu

In most instances, you are going to have one remote repository per project. However, I've had a few abnormal circumstances where I've had to use two remote repositories for one project (i.e. one repository on Gitlab and one on Bitbucket for two different teams working on the same project). It is a bit confusing and unusual but I'm planning to make a blog post about it in case someone needs it in the future :)

Thread Thread
 
gcristiantoma profile image
gcristiantoma

Great explanation ! Thank you :-)

Collapse
 
leolenzo_37 profile image
Othniel

Amazing explanation thanks.

Collapse
 
milu_franz profile image
Milu

Thanks Othniel!

Collapse
 
enriqueedelberto profile image
Edelberto Enrique Reyes

Thanks for sharing. It's really interesting

Collapse
 
milu_franz profile image
Milu

Thank you Edelberto!

Collapse
 
dansimiyu profile image
dan-simiyu

Really good read on Git basics. I love it

Collapse
 
milu_franz profile image
Milu

Great! Thank you for the kind words!

Collapse
 
jsifontez profile image
Juan Sifontez

AMAZING explanation Milu! If You could add how do the best commit messages, I'll be very grateful (I always struggle with that 😅😅). Thanks!

Collapse
 
milu_franz profile image
Milu

That is a great idea Juan! I also used to struggle to formulate my commit comments. I will add some advice in my next post. Also, thank you so much for taking the time to read this post! :)

Collapse
 
nickleake profile image
Nick Leake

Awesome explanations. 🙏

Collapse
 
milu_franz profile image
Milu

Thank you Nick! I will do my best to continue providing content that is easy to understand. Thank you for taking the time to read my post :)

Collapse
 
fabiocaettano profile image
fabiocaettano

Thank you very much.

Finally I understood how to use the branches!!!

How do I reverse a commit correctly?

Collapse
 
milu_franz profile image
Milu

Thank you for taking the time to read this post, Fabio! Glad it helped you understand branches.
To answer your question, you can use: "git revert HEAD" to reverse to your last commit. It is important to point out that git won't delete the reversed commit, instead, it will add a new commit that undoes the changes you added in your last commit. This is a super safe way to go back in time because you are keeping all your code in history! I can provide more details about git revert in a future post.

Collapse
 
anderspersson profile image
Anders Persson

Good explanations, can't wait for more.

Collapse
 
milu_franz profile image
Milu

Thank you Anders! I appreciate you taking the time to read this post. I'm planning to share more git explanations and advice every Saturday :)

Collapse
 
viswaas profile image
viswanath

nicely written and easy to follow, thanks for writing this up.

Collapse
 
milu_franz profile image
Milu

Thank you Viswanath! I really appreciate your encouragement.