Git and GitHub are two popular terms used regularly in the Coding Platforms. Git is a version control system that lets us manage and keep track of our source code history where GitHub is a cloud-based hosting service that lets us manage Git repositories. Repositories are nothing but like a folder that will contain all our code files in it. So in this article, we will see the most essential commands used in git and GitHub and how to use it.

Install Git from the link below:
https://git-scm.com/downloads

We will use the windows system for demonstrating git and GitHub in this article.

1. Create a repo in github

Sign-in into the github.com and create a repository by clicking the New option in the Repositories section.

Give a name for your repository, select whether your repo should be public or private, and click Create repository option to create the repo.

That’s it, we have now created our repository and we will see a list of commands in the next screen and we will look into it after setting up our code.

2. Setting up the Code

Create a folder that will contain all your code files. I have named the folder “Git”.Inside the folder put your code files. In this example, I have created two HTML files.

There are two HTML files in the Git folder. To push these files to the GitHub, there are commands to do it.

3. Pushing the code to the Repository

Now that we have a repository in GitHub, we can now push our files to the repository.

Open the terminal and move to your folder path.

1. First, we have to initialize the git using the command. This should be done only once.

git init

2. Then we need to add the files which are to be pushed to the git repository.

git add .

Note: The . denotes that all files in the folder are selected. You can also select specific files using the file name. Example: git add index.html

3. Now we have selected the files, then we have to commit those in the local system before pushing to the git repository. For that the command is,

git commit -m “test commit”

Note: It is mandatory to type the message about the commit inside the “ ”. So that we can identify when it has been committed and why.

4. Then we have to set the origin for these files, which is we have to connect to the git repository.

git remote add origin https://github.com/vishnuvarshan/sample.git

Note: Add your repo link as the origin and this should be done once.

5. Finally, we have to push these files to the repo, for that there is a command which is,

git push -u origin master

Thus, we have successfully pushed our files to the git repository. Now we can view those files in GitHub.

Thus, we have pushed our files to the GitHub repository and we can view and edit all the codes here. By default, all the files will be in the master branch, if we need further branches we can create it and work on it as same as the master branch.

git branch test will create a new branch named “test”.

If further changes have been made to the files or code, we can easily push those also to the repository by easily adding, committing, and then pushing it to the cloud.

git add .

git commit -m “code updated”

git push -u origin master

6. We can also change the code in the GitHub editor by clicking on edit option, to get those changes into your system project folder, we have to pull those changes.

git pull

Added a new line of code in GitHub editor

After editing code in Github editor, we have to commit here also. For this, there is a simple option on the bottom of the editor screen. Simply type the commit message and click commit.

To get the changed code to the project folder on the system, we can use the code git pull.

Pulled code from GitHub

Thus, we have pulled the code from the GitHub editor to the project folder.

We can view all our previous commits by using a git command.

git log --oneline

commit history

There are lot more things in git and GitHub that are used widely but these are the most essential and fundamental commands in git.

The sample files used in this article are attached here: https://github.com/vishnuvarshan/sample

Git Cheat Sheet:
https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf

The end.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store