Git and GitHub are integral to the software development workflow of many large teams. They also power some of the most popular open-source projects. It’s essential that you get familiar with Git and make GitHub an integral part of your workflow as a developer.
Today, we will provide a quick introduction to Git and GitHub. You will learn the basics of these tools and learn how to create a new repository on your local machine and on GitHub. You will also learn how to synchronize your local repository with the remote one on GitHub. Let’s get started!
Today, we will cover:
Master Git with our hands-on course today.
This comprehensive course is your ultimate guide to learning Git and version control. You’ll start with the basics of Git version control, such as setting up your Git config and running basic terminal commands like the echo command. Then, you’ll explore GitHub as a development platform and understand Git snapshots. You’ll advance your skills by creating and managing Git commits, undoing changes with Git undo commit, and reviewing your project’s history using Git logs. You’ll also tackle branching—learn how to rename or delete branches and confidently resolve Git merge conflicts. Additionally, you’ll master repository management with Git commands and organize changes using Git checkout, Git stash, and more. After completing this course, you’ll be ready to use Git commands in your projects—a career-boosting skill for every software developer.
Git is a distributed version control system designed to track changes in a project’s files. Git was released in 2005 by Linus Torvalds for the Linux kernel. Other kernel developers contributed to its development, and it has become the most popular choice for software teams. Git offers strong support for non-linear development and speed.
Version control systems are a software tool that manage changes to a computer program, document, or collection of files. They help a user or a group of users track changes made to a project over time.
Git works by saving a snapshot of the state of the project files at a certain time. It keeps the files that did get updated and references of the files that did change since the last snapshot. Git is essential to software development since most projects are developed by teams.
Git is the de facto standard for version control. More than 87% of developers use Git as a control system. Any developer needs at least a base knowledge of Git to be successful and relevant to the market.
Most companies, big and small, use Git as a repository, including:
Git has three primary states that it allows the project’s files to acquire. The three states are outlined below.
A repository is a directory or folder where your project lives, and it can either be local (on your computer) or hosted online. It contains the entire codebase and its revision history. Popular online hosting services for repositories are GitHub, GitLab, and Bitbucket. We will learn more about GitHub later.
Think of Git like a tool that manages a timeline. Commits are the units that timeline, like milestones or events. With the commit command, we are creating those individual events.
The commit command captures a snapshot of a project at a particular moment in development. Committed snapshots will not change on their own. Prior to the execution of git commit
, we use the git add
command to promote any changes that we will later store. Commit and add are the most popular commands.
The git checkout command is a way of switching between branches or restores working tree files. Think of this as switching between different versions or features of a project. This command uses three entities: files, commits, and branches.
The git clone
command is used to target a repository and copy it. You then have your own version of that repo and can start editing it without altering the original. In general, the original is held on a remote server, like GitHub.
The git pull
command can be used to access and download files from a remote repository and update your local repository so it matches the updates to the original. Think of this like you’re merging your version and the original. The git pull command is made up of two commands: git fetch
and then git merge
.
Tags are a reference to a specific moment in a Git repo’s file history. Think of this like a branch that doesn’t change. Tagging allows you to mark important checkpoints in a project’s timeline (i.e. software version releases).
What is GitHub?
GitHub is a platform that can be used to host code online. Think of GitHub as a platform that stores the whole codebase in a remote repository. It comes with tools to collaborate on projects of any size.
By enabling the creation of remote repositories, GitHub allows developers to have a unified source of truth for their source code. This is what makes open source possible.
To create your repositories on GitHub or contribute to open source projects, you will need to create a personal account on GitHub.
If your codebase is in a remote repository on GitHub:
The Git software is a powerful command-line tool that you can install on your machine, whether you use Windows, macOS, or Linux.
For a Debian-based Operating System such as Ubuntu, you can run the following command to install Git in a one-time operation:
sudo apt install git-all
If you are using a different operating system or Linix distribution, you can find installation instructions on this link.
Once you have git installed, you can then run git commands locally. To confirm that Git has been installed successfully, run this command in your terminal:
git --version
The output should be the current version of Git installed in the system.
The git config
command allows you to set configuration variables that control how git looks and operates. The config command works on different levels:
git config
writes to a local level when no configuration is passed.The first thing you configure after a successful installation is your email address and username; this will identify our contributions and changes in the project source code. To set up your user name and email, run the following in your command line:
git config --global user.email "educative_learner@example.com"
git config --global user.name "Educative Learner"
You can verify that the commands worked by running the following commands:
git config user.email
git config user.name
To see all the configurations currently saved on your working directory, run:
git config -l
If you run the previous command with a --global
flag, your output will only contain configuration settings at the system level.
Master Git with our hands-on course today.
This comprehensive course is your ultimate guide to learning Git and version control. You’ll start with the basics of Git version control, such as setting up your Git config and running basic terminal commands like the echo command. Then, you’ll explore GitHub as a development platform and understand Git snapshots. You’ll advance your skills by creating and managing Git commits, undoing changes with Git undo commit, and reviewing your project’s history using Git logs. You’ll also tackle branching—learn how to rename or delete branches and confidently resolve Git merge conflicts. Additionally, you’ll master repository management with Git commands and organize changes using Git checkout, Git stash, and more. After completing this course, you’ll be ready to use Git commands in your projects—a career-boosting skill for every software developer.
Let’s see how we can set up a new project using Git.
First, create a new directory using the following command:
mkdir git_started
Next, you need to navigate to the newly created directory:
cd git_started
To initialize a git repository in this folder, which is now your working directory, run the following command:
git init
Running this command would initialize an empty Git repository in the current directory. A new subfolder named .git
is created which contains several files and more subdirectories that Git will use to keep track of changes in the project.
Let’s create the first file that we will be tracking using Git. Ensure you are still in the git_started
directory on your terminal, then run the following command:
touch README.md
This will create a file named README
with the Markdown extension.
Note: Markdown is a lightweight markup language with plain-text-formatting syntax.
Most software used to browse Git repositories visually (by providing a GUI) look for a readme.md
file and render the contents on the project’s homepage. You would typically include information about your project such as setup instructions in this file.
Open the file in your editor and add the following lines to it:
# git_started
This is your first Git Project. We will be working on this file as we learn more about Git.
Save and close this file. With the file saved, the state of your repository should have changed. You can check this status by running the following command:
git status
This command doesn’t change or update anything. Instead, it prints out which files are modified, staged, or untracked. An untracked file is one that hasn’t been added to the git index yet, while a file that has been changed since your last commit was made will be seen as modified by git.
To start tracking files in your project directory, you will utilize the git add
command. This command adds the file(s) you specify to a staging area. Files that have been staged will be added to the next commit.
A commit is a snapshot of the entire state that your project is in at that moment. Git keeps track of your project through a series, or chain, of commits. The most recent snapshot of your repository is referred to as the HEAD.
As soon as you create a new commit, it will directly link to the HEAD. However, since the latest commit is now the most recent one, it will be considered the HEAD instead, replacing the previous one.
Let’s commit our project’s readme file now. First, stage the file by running:
git add .
Note that this command adds all untracked and modified files to the staging area. If you want to add a specific file instead, specify the filename like this:
git add README.md
You can also remove files from staging by running the following command:
git rm --cached [filename]
Next, you need to make a commit with an accompanying commit message. Commit messages are short descriptive messages describing what changes were made.
Let’s commit the file that’s currently staged by running the following command:
git commit -m "add readme"
This command saves a new commit with the message “add readme”. Now, when you run git status
, you will get a message indicating that your working tree is clean (no modified or untracked files).
Sometimes there are untracked files that you would not like to add to your git index. These may be user-specific configuration files, like a .env
file, or folders with large files, like the vendor folder for PHP projects or node_modules
in JavaScript projects.
You can instruct git to ignore these files by adding glob patterns to a .gitignore
file. This file will live in your project’s root directory and can contain any number of patterns. Any untracked file or folder that matches a pattern specified in the .gitignore
file is ignored by git.
Let’s see what this looks like in practice. Create a file named .gitignore in your current working directory.
touch .gitignore
Now, open this file and add the following lines to it:
# Config files
.env
.env.local
/.vscode
# Large folders
/node_modules
/vendor
Each line in a .gitignore
file specifies a pattern. Lines that begin with a #
sign are comments. The patterns here are similar to what you would typically see in a PHP or JavaScript project.
The following are examples of patterns used in a .gitignore
file:
*.env
matches all files with a .env
extension/vendor
matches the folder named vendor, as well as its subfolders and filesbuild_output.log
would match the .log
file named build_output
.To add git to an already existing project, follow these steps in order:
git init
.gitignore
file and specify files and folders to be ignored (if this is not necessary, you can skip to step 4)git add .
Commit your changes with an appropriate commit message, for example:
git commit -m "first commit"
Let’s create a remote repository on GitHub. Once you are logged in and are on the homepage, you will notice a button on the top left side titled ‘New’ (see figure below).
Once you click on the ‘New’ button, GitHub will redirect you to a different page where you will have to provide a name for the repository. You can also add a description of your repository, but this is optional.
GitHub allows you to make your repositories either public or private.
Our project already has a README and .gitignore
file, so we can leave the options to initialize with a README and .gitignore
unchecked.
Having created the repository, we now need to add the files from our local repository to the remote one on GitHub. To do this, we first have to link to the remote repository from our local project.
Git provides the remote command to help with this. The git remote
command allows Git to track remote repositories and connects local repositories to those remote ones. For example, let’s say we want to add our remote repository to the local one. The command command format is:
git remote add <remote_name> <remote_url>
Therefore, the command we will run will be:
git remote add origin <url_to_remote_repository>
Note: The name origin is essentially a more human-readable way to link to the remote repository instead of always having to use the actual URL.
origin
is simply a conventional name, but we can use any other name as well.
Once we have added the remote repository URL to our local one, we will want to push or upload our local code and its revision history to the remote repository. This can be done using git push
.
The
git push
command will update the remote repository code with all the updates that were made in the local repository.
Let’s make use of this command in our project. First, commit the .gitignore
file we created earlier.
git add .
git commit -m "add .gitignore"
Now you can run the following command to push your code to the remote repository:
git push origin master
And there you have it. You have successfully pushed your code to a remote repository.
To update your local repository from a remote one, you need to run the git pull
. This command fetches every change to the remote branch and automatically merges it to your local repository.
Every change made to your local repository must be committed before a merge can occur.
Congratulations on creating your first local and remote Git repository! You will need to have a good grasp of Git and GitHub as many teams utilize these tools in their product development workflows. There’s still a lot to learn. The next things to look into are:
To get started with these concepts and more, check out Educative’s course A Guide to Git & Version Control. This course takes you through Git basics to more advanced concepts. You will learn how to work with branches, how to utilize the git stash command, merging code and resolving merge conflicts among other concepts.
Happy learning!
Free Resources