GitHub is a website where you can host your code, navigate through other’s codes and collaborate with other developers who are working on the same project. GitHub is also a distributed version control system, so if you face any major error, you can always go back to the point where it was working normally.
You can use GitHub in multiple ways.
- Using stand-alone GitHub Desktop application
- Using stand-alone GitHub website
- Using Git Command Line Interface and GitHub website (combined)
However, in this blog, you will learn about the no. 3 way, i.e. Using Git Command Line Interface and GitHub which is the most common way.
[Windows] + [R] to the open Run dialog box and type
cmd to open Command Prompt and type the command
You will get the above result saying ‘git’ is not recognized as an internal or external command, operable program or batch file.
It is because you have not installed Git. I did this deliberately to show you why you must download and install Git on your computer.
Open a browser and search for ‘download git’. Click the one that says ‘Git – Downloads – Git SCM’.
You will be greeted with this page.
Under ‘Downloads’ section, select your operating system and download Git.
Keep on clicking Next (leave everything default) and install.
Once again, hit
[Windows] + [R] to open Run dialog box, type
cmd to open Command Prompt and type
git. You will see a different result this time. It will look something like this.
Congratulations! You’ve successfully installed Git. Now you can use Git from the command line.
Now, next move on to the next part.
Sign in to your GitHub account. If you haven’t signed up yet, go to https://github.com/ and create an account.
After you have signed in or created your GitHub account, it’s time to link GitHub and Git.
The first thing you should after installing Git is set your username and email address, which you just set up in GitHub. This is an important step because every Git commit uses this information. Type the following in command prompt.
git config --global user.name "Your GitHub Username"
git config --global user.email “Your GitHub Email”
Once you have configured the global username and email, go to GitHub and click on the + sign and click on ‘New Repository’.
Give your repository a name based on the project you are working. This is a test example, so I’ve set the name as ‘test_repository’. Also, write a description of it. Tick on ‘Initialize this repository with a README’. Then click on Create repository.
Once the repository is created, you will see something like this.
In the green box, you will see ‘Clone or download’. Click on it and you will see ‘Clone with HTTPS’ option with a link. Copy the given link.
The link will generally be in this order: https://github.com/username/repository_name
Open Command Prompt, and go to your desired drive. Create a folder called ‘GitHub Projects’ (optional).
Type the following code:
git clone [paste the code]
git clone https://github.com/samikshya-gautam/test_repository.git and hit enter.
Once done, it will look something like this.
Now if you go to G:\GitHub Projects, you will see that a folder named ‘test_repository’ has been created. This is the same repository that we just created on GitHub website a few moments ago. What we did is, we simply copied the whole repository into our local machine. Whatever operations we perform now will only affect the local repository, not the one on the GitHub website. Only when we are ready to commit the changes, we shall push it.
Inside test_repository, you will see two things: .git folder and a README.md file.
The .git folder contains all the information about your project. All git operations take place inside this folder. It also contains information about commits, remote repository address, etc. The good thing is we don’t have to take care of it. The README.md file contains the repository’s name and the description you provided.
Go inside the folder test_repository by typing
cd test_repository. You work inside this folder. If you have your project folder somewhere else, copy it to this folder.
For this example, I am creating a new text file called ‘TestFile.txt’ inside test_repository folder. Write some text in the text file. I’ve written ‘This is just a text file.’
In the command prompt, type
You will see the following on your screen.
You will see that ‘TextFile.txt’ is red. This means, you have made changes in your repository but haven’t added it to the staging area yet. Staging is a step before the commit process in git. So, a commit in git is performed in two steps: staging and actual commit.
git add -- all
You will see that the color of ‘TestFile.txt’ has changed into green. This means our files have been added to the staging area and are ready to commit.
git commit -m “First Commit”
You should always write a comment like “First Commit” and describe in short what changes you made in that file. For example, if you’d added a new method named ‘getElements()’ in your code and added into the staging area, then your commit should be like
git commit -m “Add getElements() method”
You should practice this so that you and other developers working on the project know what changes you did in that commit.
Once you’ve committed the final changes, it’s time to push your progress into GitHub repository.
git push -u origin master
This command says to push the commit in the branch named master to the remote named origin. When this is executed, all the stuff that you last synchronized with the origin will be sent to the remote repository (i.e. repository in your GitHub) and others will be able to see them there.