Introduction to Git & GitHub

Kawsikan K.
5 min readMar 17, 2021

--

Git is a free and open-source distributed version-controlling system that developers use to manage their code. It helps to track every small change that is made to the code and it is easy to learn. GitHub is a cloud-based hosting service provider that lets you manage Git repositories with a rich user interface and easily manageable structure. There are also other hosting sites like GitLab and Bitbucket, but here we are demonstrating GitHub.

You can install git for windows, Linux, and Mac operating systems by clicking the following link. https://git-scm.com/downloads

In mac operating system mostly git is pre-installed. In case if it is not you can download it. If you have downloaded you can check if your system has git installed by using the below terminal command. These commands are the same for windows and Linux-based systems as well as Mac.

git --version

Then we select the directory which we need to manage using git. It can be a folder where you have stored your project files. Initially, we need to make our directory git managed. Therefore we need to go to that directory through a terminal or command prompt. ( cd file-path)

Then we need to first initialize our directory for git. For that, we need to give the following command.

git init

This will create a hidden folder called .git. Now we can use the below command to find the status of our folder.

git status

Initially, you cannot see any git-managed files because the directory is empty. Let's create a file named test1.txt inside our directory. (Any file eg. HTML files) After creating a file inside the directory if we can check the status of our directory.

git status

You will see untracked files message. Now we need to add our file to git using the below command.

git add test1.txt

This will add our test1.txt to git. We also need to commit the file using the git commit command. Here we must give a commit message to track your changes.

git commit -m “added test1.txt”

This will commit out files in the git. Now our working tree is clean.

git status

The above command will display the below message.

You can also see the entire log of your commits by using the below command.

git log

Now successfully we have added our file git locally. Now it is time to add our files to the remote directory. Here we are using GitHub.

For that first, you need to have an account in GitHub. You can create it if you don't have it. Then you need to create a new repository. You can give a proper name for your repository and select private or public. Since here we are only learning how git & GitHub works, we can ignore the description part, and also we can ignore the last 3 options as well.

When you click create repository you will then will see the below page.

Here, the purple color highlighted part is your repository name.

You can copy the below command from your GitHub page.

git remote add origin YourRemoteRepositotyUrl

Here remote repository URL is the URL of your repository. Now paste the command on your local directory terminal and hit enter. Then you can push the local git files to GitHub by the below command.

git push -u origin master

Now if you visit your GitHub repository page and reload you can see your test1.txt will have added there.

You can make some changes to your test1.txt file in Github by clicking the edit button. After making changes to the remote directory(GitHub), you need to give a commit message, and then you can pull those changes to your local directory by following the command.

git pull origin master

Now if you check the local file changes you made in GitHub are now visible in your local file.

Also it possible to revert back to the previous stage if this change does not suit you.

git log — oneline

This prints the git log in one line. Here you need to select the id of the commit you need to go back. Then

git checkout ce02b89(id of previous commit)

This will return you back to that stage & you go can go back again to the last commit by giving the below command.

git checkout master

Creating Branches

Now we will see how to create branches. You can create a new branch by giving the below command.

git branch dev

This will create a new branch called dev. You can check existing branches through the below command.

git branch

Now you can see 2 branches master and dev. The master will be highlighted in green color because it is the current branch. You can check out to dev branch by giving the below command.

git checkout dev

Now the current branch is dev. Then make soma changes in the test1.txt file. After making some changes you need to stage the changes. (git stage & git add does the same)

git stage test1.txt

Then add a commit message and commit.

git commit -m “dev changes”

Now it is committed in the dev branch.

git checkout master

Now again if check out to master you will not see the last change you did in the dev branch.

Then again go to the dev branch.

git checkout dev

You can now push the dev branch to GitHub.

git push — set-upstream origin dev

Now you will able to see 2 branches in GitHub.

I think I have demonstrated some basics of git and version controlling and I hope it would help you in catching up on the things.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kawsikan K.
Kawsikan K.

Written by Kawsikan K.

I am an undergraduate student following software engineering degree. I am interested in learning new technologies and I love writing.

No responses yet

Write a response