Git and GitHub are important tools for all developers to understand and use along their development journey. They make it easier to manage different software versions and allow multiple people to work on the same software project.
This article is a step-by-step guide on how to use Git and GitHub. First, we will explain what is Git and how to use it. Then we will show you how to download and install it on your computer and how to integrate Git with GitHub?.
Related Articles:
- 3 easy steps to create React application.
- How to create React Web App with a Node.js API Backend.
- How to make Axios Requests in React to a nodejs API.
- How to use React Hooks in Functional Components.
- 5 Steps to Set up React Development Environment.
- How to clone GitHub repository and branches.
- What are Component Lifecycle Methods in React.
- How to open Visual Studio code from Command Line.
- How to deploy React app to Heroku.
- Complete Guide on how to use Git and GitHub.
- How to get Request Origin in Express.js?
- How to get the Request Host in express.js?
- How to Get Current Date and Time in JavaScript.
What is Git and how to use it?
Git is one of the most popular version control systems and it is an open source tool, so it is free. You can use Git to track and log the changes you make to your code or project over time and give you the possibility to review or restore earlier versions of your code.
Git is also Known as a Distributed Version Control System which means each version of your project is saved as a snapshot instead of a list of changes and allows developers to share all versions of a specific project and work on this same project by pushing and pulling changes to and from installations on other computers. But unfortunately these interactions are not happening in real-time and a member of your team can easily overwrite your code which makes Git more suitable for individual use.
Besides the distributed versions control, Git provides the branching model which allows you to keep developing and coding a new feature without affecting the main part of the project or the primary branch. Keep in mind that:
- The primary or default branch in Git is the master branch, and it is created as soon as the repository does.
- The code is separated only from the point of creation of a branch so you can switch to the new branch and start the development of the extra feature.
- You can easily delete, merge or recall branches.
How to download and install Git?
To use Git you have to download it first then install it locally in your computer.
You can download Git from their official site download Git.

Allow the Git installation on your computer:

Make sure to choose the LTS version corresponding to your operating system and follow the installation instructions.

To verify that your installation was successful and get the Git version, run the following command:
git --version
Git commands
Git is a command-line tool, but to understand the following commands lines and how to use them you need first to know that a git files:
1- can be in one of this three states :
- Modified state: means that the files have changed or revised but unrecorded.
- Staged state: staged state means that the files are modified and prepared to be saved into the .git repository during the next commit snapshot and Git is authorized to monitor the file staged version.
- Committed state: When files staged versions are successfully recorded and stored into the git repository, they are in committed state.
2- can be in one of these locations:
- Working directory: is any local folder you created anywhere on your system and contains files in the modified state.
- Staging area or index: is a file located in the .git repository. It stores informations about files in staged state that are ready to be committed to the .git repository. When you move files to the staging area in Git, you actually gather and prepare files for Git before committing them to the local repository.
- Git directory or repository (.git directory): is a folder created by Git inside the working directory you have created and instructed Git to track to store the object databases and metadata of the files.
NOTE: .git repository is a hidden directory in the project’s root directory. But you still can see it in your code editor.
Git configuration commands
- git config is used to configure and set up Git for the first time:
$ git config
The first step is the registration: enter your name and email using this command:
$ git config --global user.name "your name"
$ git config --global user.email "your-email"
Note that you can change your name and email by running the same commands.
To view your Git configurations run the following command:
$ git config --list
Create and initialize a Git Repository
There are two ways to create and initialize a git repository: the first way is to clone an existing repository from GitHub. We will talk in details about this method in the next section of this article.
The second way is to initialize any regular folder in your system and set up it as a new git directory.
- To create or initialize a new Git project or repository, navigate to your working directory or folder, then run the git init :
$ cd /yourWorkingDirectory
$ git init
This command creates a .git repository with all the tools and data necessary to maintain versions. It is very important to note that this command is used only once per project for an initial setup.
- In order to check the status of your Git repository, use the following command :
$ git status
The output of git status includes helpful messages to direct the user to manage their repository. It shows the user the current commitment, the modified files, the new files not being tracked by Git and which branch of the repository you’re currently working on.
- The git add command is usually performed before a commit. It is used to add files to the staging area and track them for a commit. These files will be included in the next Git snapshots of the repository.
to add one file use the command:
$ git add file_name.js
to add several files use the command:
$ git add file_name1.js file_name2.txt file_name3.js
to add more than three files use the command:
$ git add .
To add all new and updated files from all other directories inside the root directory use the command:
$ git add --all
OR:
$ git add -A
Now if you run git status the files will appear in the Changes to be committed section of the git status output.
- To remove a file from the staging area and untrack it run:
$ git rm --cached file_name.js
OR:
$ git reset file_name.js
If you run git status the file_name.js will appear in the Untracked files section of the git status output.
Git commit commands
- The command git commit is an important Git command. It takes a snapshot of your repository after making changes to your project and creates a checkpoint to which you can restore your project later if it’s necessary. To use this command, you should have at least added one file to the staging area.
$ git commit -m "Add your message here"
The -m indicates that the following section of the command should be read as a message. Note that a commit message must tell what your commit does.
- To avoid running two commands while tracking and adding files to the staging area, then committing them at the same time, you can use the following command:
$ git commit -a -m "Your message here"
- To cancel the last commit in the master branch run:
git reset --soft HEAD^
After running this command, you can make changes to the staging area and commit again. Note that HEAD is just a pointer to the master branch.
- To rectify a commit after changing a file or add new one to the staging area use following commands:
$ git add file_name.html
$ git commit --amend -m "Add your message here"
Git branch commands
Git branch commands are very helpful specially when you are working on a project with other developers. Using branches keeps the stable part of the project safe and allows the team members to develop new features on different versions of the project simultaneously without affecting the code in the master branch.
- To get the list of all branches and the current branch you are in type:
$ git branch
- In order to develop a new feature, you can create a new branch by using the following command:
$ git branch branch_name
- To switch to the newly created branch run the command:
$ git checkout brunch_name
After switching to the new branch, you can make your own changes to files and push files to a remote repository without affecting the master branch.
- When you’re done working on a branch, you can merge your changes back to the master branch, but first you have to switch to the master branch then merge all the changes you made to the branch to add them to the master branch.
$ git checkout master
$ git merge your_branch_name
- To delete a branch after merging it to the master branch run these commands:
$ git checkout master
$ git branch -d branch_name
Git Push and Pull commands
Git push and git pull are both Git remote commands. In order to use them, you need to create a remote repository, in this article we will use GitHub to host the git repository. But first you have to create a GitHub account, then create a new repository for your project and bind it to your local Git repository (more details in the next section of this article).
- git push: you can use this command when you are working on your local git repository (project) and you want to commit and push up the changes you made to GitHub.
$ git push -u origin branch_name
The above command is used only when you push your files for the first time. It will push your files in the branch branch_name to the GitHub remote repository located on the server after the option origin.
Note that the branch can be master or any other branch of your project.
For the next push you can run only the following command:
$ git push
- git pull: is used to grab updates of the current branch, to pull changes made by other developers and synchronize your local code with the GitHub repository.
$ git pull
Git help commands
- For get help and to bring up the most common Git commands type:
$ git help
You can also use this command in case you want to figure out a specific command:
$ git help <theCommandName>
You can find a Git commands cheat sheet HERE.
In the Git pull and push commands, we have mentioned the remote repository and GitHub. So what is GitHub? how to create a GitHub account? and how to create a new repository and clone it to the local Git repository?
What is gitHub?
GitHub is a collaborative cloud-based platform owned by Microsoft and offers an interesting free plan. It is built for developers to offer them a better way to work together. GitHub is known as the largest online storage space where you can host, review, share and publish code, manage projects, and build software alongside millions of developers. This interaction between developers makes GitHub a networking site that aims to encourage open source development and helps young projects grow.
Actually, GitHub is a web-based graphical interface that provides a Git repositories hosting service which allows you to keep track of and share your Git version control projects outside of your local computer.
Besides the hosting feature, GitHub offers many other features that make it powerful, such as:
- Fork: fork a repository is the process of copying a repository from one user’s account to another. This allows you to copy a project and modify it under your own account name.
- Pull request: After making your changes to a project and you want to share them, you can send a notification called a “pull request” to the original owner.
- Merge: The project owner can add your recent changes to his project by merging your repository with the original repository and give you credit for your contribution.
Create a GitHub repository
To create a new GitHub repository, we need first to create a GitHub account.
Create a GitHub account
To create a new account go to GitHub website and click on Sign up.

Next, enter your username, email and password then click on Create account.

Once your account is created, log in and click on the plus sign in the upper-right corner of your homepage to create a new repository.

Enter a repository name, choose whether you would like your repository to be public or private, and check Add a README file. Once you have completed this, you click on Create repository.

How to push Git repository to GitHub?
Once your GitHub repository has been created, navigate to the new repository and click on Code, then grab the HTTPS link: The link will look similar to this: https://github.com/YourUsername/your-repository-name.git.

The next step is to bind the GitHub repository to your local repository by using the following command:
$ git remote add origin https://github.com/devdotcode-account/repository-example.git
After running this command git will add a repository with the same name as the GitHub repository, linked or connected to your project on GitHub. Now you can start your development and make code changes.
To verify that your repository is correctly connected and view a list of all GitHub repositories, you are connected to use the command:
$ git remote -v
When you are done with the code changes, add all your changes to the staging area:
$ git add --all .
Then commit the changes:
$ git commit -m "Add your message here"
Finally, push the changes to GitHub
- git push: you can use this command when you are working on your local git repository (project) and you want to commit and push up the changes you made to GitHub.
$ git push -u origin branch_name
The above command is used only when you push your files for the first time. It will push your files in the branch branch_name to the GitHub remote repository located on the server after the option origin.
Note that the branch can be master (root repository) or any other branch of your project.
For the next push you can run only the following command:
$ git push
To avoid enter the username and the password each time you run git push command, in the step where you grab the HTTPS link to connect the GitHub repository click on Use SSH and grab the SSH link instead the HTTPS one including a certificate issued by your organization’s SSH certificate authority.

How to fork a GitHub repository?
Fork a repository is one of the most important GitHub features. It allows you to copy a repository from another user’s account, then modify it and make changes freely (including creating branches and opening pull requests If you are hoping to contribute back to the original repository) under your own account. In this section, we will show you how to fork a GitHub repository and copy it to your account.
You can fork a repository in three simple steps:
- First login to your GitHub account.
- Then, navigate to the repository you want to fork to your account.
- Finally, click on the Fork button to create a copy of the repository in your account.

Summary
In this article we explained in depth how to use Git and GitHub by covering the following points:
- Explain what is Git and how to use it.
- What is GitHub?.
- how to integrate Git with GitHub and how to create a new repository.
- how to fork a GitHub repository.
Hope this post was helpful for you and if you want to know in details about how to clone a GitHub repository and branches, check out this article: How to clone GitHub repository and branches.
You might also like:
3 easy steps to create React application.
How to create React Web App with a Node.js API Backend.
How to make Axios Requests in React to a nodejs API.
How to use React Hooks in Functional Components.
5 Steps to Set up React Development Environment.
How to clone GitHub repository and branches.
What are Component Lifecycle Methods in React.
How to open Visual Studio code from Command Line.
How to deploy React app to Heroku.
Complete Guide on how to use Git and GitHub.
How to get Request Origin in Express.js?
How to get the Request Host in express.js?
How to Get Current Date and Time in JavaScript.
How to add Custom Error Handler Middleware to your Node.js/Express API.
Complete JWT Authentication and Authorization System for MySQL/Node.js API.
How to Build a Complete API for User Login and Authentication using MySQL and Node.js.
Node.js + MySQL : Add Forgot/Reset Password to Login-Authentication System.
Nodemailer + Gmail: How to Send Emails from Node.js API using Gmail.
How to store Session in MySQL Database using express-mysql-session.
How to interact with MySQL database using async/await promises in node.js ?
How to use Sequelize async/await to interact with MySQL database in Node.js.
MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.
ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js
ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.
How to add Routes to insert data into MySQL database-related tables in Node.js API?
Example How to use initialize() Function in Node.js/Express API .