Git Explained: The Basics

milu_franz

Milu

Posted on May 9, 2020

Git Explained: The Basics

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.

Alt Text

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.

Alt Text

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.

Alt Text

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

Alt Text

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!

💖 💪 🙅 🚩
milu_franz
Milu

Posted on May 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related