Git is a distributed version control system that is used to track the changes (different versions of the software) made by many different people (distributed) to the repository (where the software is stored) of the software. It helps developers to easily submit those changes which are made to the source code just by executing a few git commands. Whenever a developer makes any changes to the code and wants them to be merged with the official repository of the project then he/she has to make a pull request which will eventually submit those changes to the repository.
At a time, thousands of people push changes to the same repository and keeping track of all these changes is very difficult. For this, we need a version control system. In this post, we will discuss the various git commands which we can use for creating a branch, checking the status, pushing the changes, updating the project and much more.
For doing all of this we will be using a website called GitHub where we can host our open source projects and is based on git version control system.
Before diving deep lets first understand all the git commands which we will be using in this blog post.
|git clone||To clone the repository from GitHub|
|git init||Initialize the git repository|
|git add .||To add files to the staging area|
|git status||Check files in the staging area|
|git commit -m “message”||Committing changes with a commit message|
|git checkout||Used to switch between branches|
|git merge||To merge your own branch with the master branch|
|git remote add origin||Adding a remote repository|
|git push||Pushing local changes to the remote server i.e on GitHub|
There are even more git commands which you can go through which are available here.
Recommended Books to learn Git & GitHub
- Git: Learn Version Control with Git: A step-by-step Ultimate beginners Guide
- Version Control with Git: Powerful tools and techniques for collaborative software development
Using Git Commands on GitHub
Firstly, to use git commands with GitHub you will need a GitHub account, if you don’t have one then you can create it here.
After creating an account login and click on “+” icon at the top-right and select “New repository” to create a new GitHub repository. You will see the below screen where you have to enter the details related to your repository like the name of the repository, description, choose whether you want to make your repository public or private and lastly, you can choose whether to initialize your repository with a README.md file or not. After that, you can select a license for your repository and then create the repository.
Now that you have created the repository you can now add files to this repository using git. You will only see a README.md file in your repository where you can write documentation related to your project. You can modify this file as many times as you want.
If you want to run git commands on Windows or Mac systems then you have to install Git through this link. And if you are using Linux you can run this command inside your terminal:
sudo apt-get install git (for Debian) or
sudo yum install git (for Fedora).
Before you can make changes or add files to the repository you first have to clone the repository from GitHub on to your local machine by pressing the button “clone or download” from your repository page or just by running the command which is shown below.
git clone https://github.com/sahilbhosale63/FirstRepo.git
Here in the place of “sahilbhosale63”, you have to put your own username and in place of “FirstRepo.git” you have to add your own GitHub repository name. Make sure that your repository name mentioned here matches your actual repository name. If this is not the case then you will get an error.
Now that you have the project locally available on your system you can make changes to this project locally and then you can follow the steps below to push this project onto your GitHub repository.
The changes can vary depending upon the needs of what exactly you want to do like fixing an issue or writing some snippets of documentation, etc. It doesn’t matter what exactly you want to do the commands used to push the changes to the remote server remains the same.
Initially, go inside the folder (in this case FirstRepo) from your terminal and then you have to initialize the repository. This is the primary step that you have to do when you are initially pushing changes to the remote server i.e on your GitHub repository.
After that, you have to tell git that which files you want to upload on to the staging area, all the files which are available locally or only the files in which you have made the changes.
To add all of the files inside the folder to the staging area you have to run
git add . or for a specific file use
git add [file-name.txt]
In this case, we will be creating a file called “sample” using
gedit sample which is a text file and will be adding this file to the staging area.
git add sample
Before you can do anything else you have to add your name and email address using the following command.
git config --global user.name 'Your Name' git config --global user.email 'firstname.lastname@example.org'
To check what’s in the staging area we can use the command below.
Since we have previously added the “sample” file to the staging area therefore when we run
git status command we can see that files which we have added in the staging area.
If you want to remove any file from the staging area you can run the command below. Here, the “sample” is the name of a file.
git rm --cached sample
Now after you have done with adding files to the staging area you can now commit those changes for that you have to run the following command.
git commit -m "Initial Commit"
Here, the “-m” stands for a message where you have to enter your commit message meaning you have to write a message about what changes you have made so that whenever you push these changes on the GitHub other people working on that project can easily recognize these changes.
In this case, we will enter “Initial Commit” as our commit message. You can put whatever you want to until and unless it is related to the changes which you have made.
Now that we have committed these changes lets talk about the concept of branches in git.
Till now whatever we were doing was directly onto the master branch (our project on the remote location). So if you have not created any branch then whenever you commit changes it will by default apply to the master branch. But this is not a proper way of committing changes instead we can create a separate branch and make our changes on that branch.
To create a new branch.
git branch FirstBranch
You can give whatever name you want to your branch here we have given “FirstBranch”.
But if you run the
git status command then it will tell you that you are still on the master branch. So to switch to the “FirstBranch” branch which we have created run the following command.
git checkout FirstBranch
Let’s create a new file called login.html using
touch login.html command and add it to the staging area by using
git add . and then commit it using
git commit -m 'Login File' through the “FirstBranch” branch.
Now to merge the changes which we have made in the “FirstBranch” with the “master” branch then we have to first switch to the master branch by
git checkout master command. And then merge those changes using
git merge Firstbranch command.
Finally, to push all of these changes to the remote repository on GitHub you have to run the following two commands.
//To add origin git remote add origin https://github.com/sahilbhosale63/FirstRepo.git //To push changes git push -u origin master
Here it will ask you to enter your GitHub credentials, so enter your username and password and after that, all your local changes will be pushed onto your GitHub repository. You can go to your repository page on GitHub where you will see your changes live.
Thanks for reading and if you like the content then support us on Patreon. Your support will surely help us in writing more of such content.
If you have any questions regarding Git and GitHub you can comment them down below. And don’t forget to share this blog post with others on social platforms.