In this article (and the accompanying video I created) you will learn what Git is and how you can use it to manage your code in a more organized way.
We're going to go over the basics of Git and explain the most important commands along the way.
- Creating a repository (project) with GitHub
- Cloning the repository to your local computer
- Adding a file to your local repository and committing the changes
- Pushing your changes to your master branch
- Pulling the changes to your local machine
- Creating a branch, making and committing the change
- Opening a pull request (propose changes to the master branch)
- Merging your branch to the master branch
Just a note…
Along with writing this article, I have also created a YouTube video! I encourage you to first watch the video and follow along and code while watching. The video goes into even more depth and has more comprehensive explanations than this article does. You can then use the concise code blocks in this article as a reference.
Here's a link to the full video: https://www.youtube.com/watch?v=XpvA00MRZ0o&feature=youtu.be
Git is the most popular version control system out there and for good reason. In this video, we'll go over the basics of what git is and how to use it within the command-line. We will look at and work with all of the basic and most important commands such as add, commit, push and more. This tutorial is very beginner friendly.
Git is essential version control technology for web developers and programmers.
A version control system allows you to track the history of a collection of files. It supports creating different versions of this collection. Each version captures a snapshot of the files at a certain point in time and allows you to switch between these versions. These versions are stored in a specific place, typically called a repository.
Git is currently the most popular implementation of a version control system.
If you have a GitHub account and have installed and configured Git on your computer, skip ahead.
You can verify if Git is installed by using the following command:
Create your first repository
In your computer, create a folder for your project.
Let’s call the project folder demo-git-repository.
Go into your project folder and add a local Git repository to the project by using the following command.
First on the list of the most important git commands is -
It simply initializes an empty Git repository.
Now our folder is initialized as a Git repository and we can do all the git stuff in it.
Optional step: clone the existing repository
Cloning a Git repository is an optional step. You do it only if you want to start a project with someone else's code and then build on top of it.
You can do that by going to a GitHub repository and clicking "clone or download". Afterwards copy the link.
Now we can use that link in the second Git command we're going to cover.
git clone <link> will copy all the files from that Git repository to your local one. After doing that you can work on those files and save the changes.
Do this step only if you want to continue editing already existing files. If you want to start with a clean directory, you can move on to the next step.
Add files to your local repository
After you have been working on your project and created a few files it’s time to track and add files to the staging area.
Let’s say we add files called
We need to tell Git to track each one of these files.
Use the following command for staging a single file:
git add index.html
In case you want to stagetra multiple files you can use:
git add index.html style.css
This is really uncommon though. Most of the times you simply want to track all of the files you create and you're not going to want to add each one separately.
If you want to add all the files inside your project folder to the staging area, use the following command:
git add .
Commit (save) the changes
After we add those files to the staging area, then we can create a checkpoint called commit. Commit is usually created after we are finished working on a certain feature or fixing a certain bug. These commits can be used to track which features were added on which commits and by whom, so we can easily track or even revert the whole commit if something bad happened to the source code.
A commit in a git repository records a snapshot of all the files in your directory. It's like a giant copy and paste, but even better!
To add a commit we can use the following command:
git commit -m "<commit message>"
-m means that we are going to add a certain message.
In this example, we are going to create our first commit in Git. We are going to make a commit with message “Initial commit”.
We can do that by typing
git commit -m "initial commit".
Commit message needs to indicate what code changes were done in that particular commit.
Protip: the widely used practice for writing commit messages is to write them in imperative. For example, write
add navbar and not
added navbar. That way, a person who is going to merge your branch (more on that later) will know exactly what will that commit do when merged; it will "add navbar".
Create a remote repository
A remote in Git is a common repository that all team members use to exchange their changes. In most cases, such a remote repository is stored on a code hosting service like GitHub or on an internal server.
Head on GitHub and create a new repository. Enter a repository name and click "Create repository".
You'll be greeted with a series of commands you need to enter to set it up.
As you can see, we already did most of the steps. Now we only need to add the newly created repository as our remote repository. We can do that with the following command:
git remote add origin <link>
Push your changes to your master branch
When all the files are added and committed, the only thing left to do is to push them to our remote repository.
For the first time you're pushing files to a remote directory, you need to enter the following command:
git push -u origin master
And every other time, you'll just need to type:
Create a branch
Branches in Git are incredibly lightweight. They are simply pointers to a specific commit - nothing more. The use of branches is thoroughly explained in the video.
You can create a new branch with a following command:
git branch <branch>
Then, you can use the following command to switch to that existing branch:
git checkout <branch>
You can also use the next command to create a new branch and immediately switch to it. Most people use this instead of separate branch and checkout commands:
git checkout -b <branch>
Merge your branch to the master branch
If you’ve made a bunch of changes to a branch, let’s call it “change-styles”, and you want to merge that branch back into your master branch, you use this command:
git merge <branch>
For our specific example; to merge change-styles into the master branch you’ll want to checkout to the master branch, and then run:
git merge change-styles
Pull recent changes
If you’re working with multiple people, you’ll find yourself in a position where a repo was updated on GitHub, but you don’t have the changes locally. If that’s the case, you can use git pull origin <branch> to pull the most recent changes from that remote branch.
git pull origin <branch>
You made it all the way until the end! If you get stuck along the way, feel free to ask questions and leave feedback.
Most helpful would be the support on YouTube since I recently created a channel! Click here, there is a lot of interesting stuff coming soon! :)
You can also check out the article I did on:
Learning Async/Await on a Real World Project.