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. Keep reading if this is how you feel every time you are about to use git. This post is going to define git, explain why you need to learn it, and break down the following basic commands in detail: clone, checkout, pull, add, commit, stash, and push.
What is git and why do you need it?
Git is one of the most popular version control systems and it helps software developing teams manage changes to their source code over time. In other words, version control keeps track of every change in your code and it allows you to go back in time when something goes wrong.
Also, it is really helpful to prevent concurrent work from conflicting when multiple people are working in the same project. An individual may be working on the sidebar navigation while another one is simultaneously updating the header.
Version control systems facilitate the work of multiple individuals by allowing them to use different branches as part of one “file tree” and merge their updated code to one source of truth when it's ready.
Git clone
Downloads a copy of the project (remote repository) to your local computer. To use this command, follow these steps:
1) Copy the clone or download link
2) Open your terminal
3) Access the location on your computer where you want to copy the project: cd [desired-location]
3) Clone the project: git clone [copied-link]
4) Use the commands cd [project-name]
, followed by ls
and you should see the list of files you just cloned!
Git branch
Lists all your project branches and highlights your current branch. If you just cloned a project, you are going to be in the master branch (your source of truth). 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.
Git checkout
Selects the branch you want to work on.
If the branch already exists:
git checkout [branch-name]
If you want to create a new branch:
git checkout -b [branch-name]
Git pull
Pulls recent changes from the remote repository to your local files. If one of your team members merged a new feature into master, you need to pull the recent code added to master to keep your local files up to date.
Git status
Lists all the files that you have worked on and have not been saved in a commit yet.
Git add
Stages file that you would like to commit.
If you want to stage all the files that you have worked on: git add .
If you want to stage only one particular file: git add [file-name]
Git commit
Saves the group of changes you staged to your local repository. Add a comment with each commit that summarizes the change.
git commit -m “commit message”
Git stash
Saves any changes you have made locally and it reverts the working directory to match the HEAD of your last commit.
See a list of all the changes you have stashed: git stash list
Bring back the last changes you stashed: git stash pop
Clear all your stashed entries: git stash clear
Git push
Saves your local committed changes into the remote repository so everybody else has access to your work.
git push origin [branch-name]
What is next?
This is the first one from a series of posts about git. We will explore more advanced commands, git submodules, multiple remotes, and squashing commits before a pull request in the next weeks.
I hope this information was helpful and you feel a little more confident about working with git!
Top comments (27)
This is AMAZING! Please keep the git tutorials coming 🙏🏽 Your explainations are quite beginner-friendly 😄
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 :)
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 stash
and 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 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!
Thank you for the excellent, detailed explanations Milu. You are a superb educator.
Git push origin [branch-name] , I would like to ask, origin stands for ? Thanks for answering and I love your post :)
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!
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
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 :)
Great explanation ! Thank you :-)
Amazing explanation thanks.
Thanks Othniel!
Thanks for sharing. It's really interesting
Thank you Edelberto!
Really good read on Git basics. I love it
Great! Thank you for the kind words!
AMAZING explanation Milu! If You could add how do the best commit messages, I'll be very grateful (I always struggle with that 😅😅). Thanks!
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! :)
Awesome explanations. 🙏
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 :)
Thank you very much.
Finally I understood how to use the branches!!!
How do I reverse a commit correctly?
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.
Good explanations, can't wait for more.
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 :)