Git and GitHub for beginners

SaizalSinha
6 min readAug 8, 2021

--

We all might have heard about Git and GitHub at some point in time since we started programming. In this blog, I will be introducing you to how git works and how you can use it to achieve various powers as a programmer. We will also be talking about how you can use git on the command line to do things such as version control or how to clone repositories, how to folk, make pull requests, merge repositories, and a lot more exciting stuff. Let us first start with version control.

VERSION CONTROL

Assume we make a new code file and we write a few lines in it. Now I decide to put it on the version control using git and let’s call this our first checkpoint so it is my first version. Later on, as I progress, I may write a few more lines of code and at this point, I decide to make another checkpoint and I call it my second version. Further down the line, suppose I accidentally made some irreparable errors in my code file which are quite complex to be fixed and you get screwed up. In such a case, you would simply want to go back to the last checkpoint and this can be done using Git. Not only the last one but you could go back to any of the previous versions that you made.

Version Control using Command line

Make a new file and write a few lines in it. Open your command line and change the directory to the required folder containing the file. Now, let’s create a git local repository and start tracking some of these local file changes. To initialize git, we simply write git init. Now inside our working directory, to start tracking changes in my files, I need to add this file to the “Staging area” which is an intermediate place where you can pick and choose which files from the working directory you want to commit. To see what’s currently inside of your staging area, use the git status command and the files that are not being tracked will be shown in red. To add a file to the staging area, we have to use the command git add filename. Now let’s go ahead and commit this under version control, so the command is git commit -m “commit message” where we have added the -m flag to add a commit message that helps you keep track of what changes you have made each commit. Also, we can see what commits we have made using the git log command. Now suppose we have several files that we need to commit, then it would be a very tedious process to do this one by one. The better way possible is git add . where the dot specifies all the files in the working directory. Also, to remove all the files from the staging area, we can use git rm — cached -r . command.

Now let us suppose that we made some changes in the committed file but we want to revert to its previous version but before we do that let’s first see about a git command that checks what are the differences between the current version of the file and the last checkpoint in our fit repository. So to do that we use the command git diff filename. To go back to the previous version, use the git checkout filename command.

Until now we have gone through some of the local implementations of git and now we will be talking about GitHub and its remote repositories.

GITHUB AND REMOTE REPOSITORIES

Photo by Luke Chesser on Unsplash

A remote repository is a repository that is hosted on somebody else’s server by using GitHub. Create a new repository on GitHub and copy the URL on your clipboard from the Quick Setup section to get started. Now head over to the command line and to set up a remote repository, use the git remote add origin URL command. Here URL is the one copied on your clipboard and the origin is the name of your remote. Now git push -u origin master can be used to push our local repository to the remote repository using u flag, which links both repositories.

Next, we will be talking about gitignore and how you can hide a sensitive piece of information such as API keys, passwords, etc to your remote repositories.

GITIGNORE

So this is all about how you can set rules to prevent committing certain files to your local and remote repositories. A common example that people tend to add to their gitignore files is ds store files. ds store file is a settings file that saves certain things.

The first thing we need to do to make a gitignore file is to write the touch .gitignore command on our command line. Now we can write the name of the files to be ignored separately in each line of the .gitignore file. Also, we use the pound sign (#) for comments.

Further, we will be talking about git clone and how to clone repositories to your local system.

CLONING

We have already talked about how we can push our local repository to a remote repository, now we will be talking about cloning a remote repository to pull it to your local machine. To do this, copy the URL from the ‘clone or download’ section of the desired repository on GitHub. Now use the command git clone URL to clone the repository.
Moving ahead, we will be talking about something more advanced like forking and making pull requests and merging repositories.

BRANCHING AND MERGING

Say if we had versions 1 and 2, now at this point, we realize that we want to build a new feature. So what we can do is instead of continuing to commit to the master branch which is the main branch, we can also create a side branch and continue to commit as an experimental branch. So now we have two branches that are parallel to each other and they could be developed simultaneously. Now maybe later, if we find the experimental branch to be fruitful and we are willing to merge it with the master branch, then that can be achieved easily by simply giving a merge request. Now let’s see how this works on the command line.

git branch sub-branch1 can be used to create a new branch named sub-branch1. Now you can also check what branches you have by writing git branch. We can switch to a different branch by writing git checkout branch-name. After modifying and committing files on your new branch, we can head over to the master branch and use the git merge branch-name command to merge the desired branch.

FORKING AND PULL REQUESTS

We have already looked at implementing source control locally and remotely, so now we will be talking about collaboration using remote repositories. This is one of the best ways to start working as a team and to contribute to an open-source project. Suppose if someone wants to contribute in an open-source, so they can make a copy of the repository into their own GitHub account where they can make changes to it. This is called forking. Now when the contributor has forked, made some changes, and committed those changes into their repository and now they want the owners to merge those changes, they can send a pull request. You can find the ‘new pull request’ option on top of your forked repository. Now after the code review, if the owner likes the changes, they will approve the pull request and will merge their repository.

--

--