What is Git?
Git is a distributed version control system or VCS that tracks the changes we make in our files. It is designed for people and teams to collaborate on projects together. As developers make any changes to the project, any earlier version of the project can be recovered at any time. We can also make a copy of the files or the project and do changes to the copy and then merge the changes in the original copy.
Using Git developers can review the project history to find out which changes are made, who made the changes, when were the changes made and which changes are needed.
Git is not limited to source code files, we can also track text or images and other files. That's why not only developers but anyone can find Git helpful.
Git Repositories
A repository (repo in short) or git project is the entire collection of files and folders associated with a project. It tracks and saves the history of all the changes made to the files in a git project in a directory called .git
. If we delete the .git
folder the project history will be deleted.
Basic Git Commands
To use Git there are some commands we have to use. These commands can be used to create, change, merge or copy code. Here are some of the git commands -
git init
: It initializes a new git repository and begins tracking the existing directory. It adds the hidden.git
folder in the existing directory which is required for tracking the history of changes or version control.git clone
: It creates a local copy of a project that exists remotely in a repository with all the files, history and branches.git status
: It shows the status of the changes in the project files or code as untracked, modified or staged.git add
: Git tracks the changes in a git project but it is necessary to stage the changes and take a snapshot of them to add them to the project's history. Thegit add
command performs the staging part of this process.git commit
: After thegit add
command the second part of tracking the changes is to take the snapshot of the project's history.git commit
takes the snapshot and saves it and completes the change tracking process. Anything that is staged with thegit add
command will be part of the snapshot.git branch
: This command shows the branches. If we use a command likegit branch my-branch
, it will create a new branch named my-branch.git branch
: This command is used to merge or combine the changes that are made in two different branches.git push
: It pushes the changes from the local repository to the remote.git pull
: This command is used to fetch and download the content from a remote repository and update the local repository to match the content.
What is GitHub?
GitHub is an online hosting service for git repositories that offer the version control and source code management of Git and also adds some features of it's own. GitHub hosts Git repositories and provides developers with tools to ship better code through command line features, issues, pull requests etc. There are some other alternatives of GitHub also like Bitbucket and GitLab. All of these are referred to as remotes.
It is not necessary to use GitHub or any other remotes to use Git. But remotes make sharing the code easy and also provide a Graphical User Interface for Git.
Now that we have an idea of what Git*,* GitHub and some Git Commands are used for, let's discuss a little bit about how to use them to push our code to GitHub.
How to Push Repository to GitHub?
The first step is to install Git in our local system. We can go to this https://git-scm.com site and can download git based on our operating system. After installing we can check the current version using the git --version
command on the command line. It will also ensure that git is correctly installed in our system or not.
Configure Git
After installing Git the next step is to configure git for our system. We have to set our username and email address to configure git. Git will use this information to identify who made the specific changes to the files. We have to use the commands git config --global user.name "username"
and git config --global user.email "email"
to set username and email and in the place of username
we have to put our GitHub username and in the place of email
we have to put the email linked with our GitHub account.
Push Repository to GitHub
To push the local repository to GitHub first we have to create a new repository in GitHub in which we will push the files of the local repository. Let's suppose that I made a new repository named Food Delivery Project and I want to push all the files from my local system to the Food Delivery Project repository. Then we have to follow the below steps -
Push a new repository to GitHub -
In Windows OS go to the local project file and open Git Bash there by right-clicking. For other operating systems we can also open the project files in a VS Code folder and can use the VS Code terminal to execute the commands.
Then we have to use the following commands -
git init touch .gitignore git add . git commit -m "first commit" git branch -M main git remote add origin git@github.com:Sayantan-23/Food-Delivery-Project.git git push -u origin main
git init
initializes the local git repository and creates the folder.git
.
git add .
stages all of the changes for all of the files in the project.
touch .gitignore
this command creates a new file named.gitignore
. Now there may be some files in the project we don't want to push into the GitHub repo for example in a JavaScript project the node modules files we don't push to GitHub. So in that scenario, we just have to write the file name or the path of the file from the base directory in the.gitignore
file. Then thegit add .
command will ignore those files for staging those files will not be pushed into GitHub. This step is not necessary if we don't have a file that we don't want to push.
git commit -m "first commit"
this command takes the snapshot of the changes of the files that are staged by thegit add .
command. In the place offirst commit
we can write anything but it should be meaningful and should give a hint about which changes are made in this commit. This is the first time we are committing so we can usefirst commit
orinitial commit
as by seeing only the commit, we can understand the commits that this is the first commit of this project.
git branch -M main
command changes the main branch's name to "main". The default main branch might be created as "master" but after this command, the main branch's name will be "main". If we want our main branch name as "master" then there is no need to execute this command.
git remote add origin git@github.com:Sayantan-23/Food-Delivery-Project.git
this command creates a connection between our local repository and the GitHub repository.
git push -u origin main
this will push all the code and other files to the GitHub repository into the "main" branch. If we don't change the name of the main repository then we can usegit push origin master
command to push our code into the main branch of the GitHub repository.Update and existing repository -
Now let's suppose we did some changes in the Navbar of the website of the Food Delivery Project in our local system and we want to update our GitHub repo also. In that case, we have to use these commands -
git status git add . git commit -m "Updated Navbar" git push -u origin main
git status
command will tell us which files are untracked or changed. This command is not necessary for the updation but it helps us to see what files are changed, added, deleted and untracked.
git add .
command stages all the changes in the files.
git commit -m "Updated Navbar"
command takes a snapshot of the changes that are staged by thegit add .
command and theUpdated Navbar
message will help us to understand that the Navbar was updated in this commit.
git push -u origin main
this command will push all the files to the main branch named "main". If the main branch is named "master" then we have to use thegit push origin master
command.
Use of GitHub for Students
Git and GitHub are also great tools for learners or students to showcase their skills. Using Git and GitHub we can store and share our projects and we can make beautiful and decorated repositories using the readme.md
markdown files. This is very helpful when anyone is trying to get a job. A good GitHub profile can impress interviewers and it is also helpful for interviewers as they can clearly see how much work is done by the interviewee as in GitHub Profile it can also be tracked that in which day I am pushing my code. So it is very helpful for the interviewer or anyone to track the progress of the interviewee or any student.