Git and GitHub for Beginners.

Nuwanthi Rajapaksha
Geek Culture
Published in
7 min readAug 16, 2021

This article is for beginners who are going to start with Git and GitHub. Through this article, first I’m going to discuss what is Git and GitHub and then I’m moving towards the basic commands you have to know while using Git.

Git is a free, open-source distributed version control system that means a software tool that helps the software teams to work collaboratively on big software projects by managing the source code over time. Version control systems keep track of the source code in a special database called a repository in a structured way. Without a version control system, it is difficult to keep the track of the changes made to the project and to know who made those changes. You have to keep separate files as final, latest and in the end, you have to deal with a new final version. For software teams, the version control system makes it easier to keep of track the changes made by the fellow team members without emailing the updates and collaborate effectively on the project.

You can use a command-line, a graphical user interface, code editor, or an IDE to make git work. Throughout this article, we are going to discuss Git using a command line.

What is GitHub?

GitHub is a hosting service that can be used to integrate with Git. There are many other solutions for GitHub. The most popular alternatives are GitLab and Bitbucket. So you do not need GitHub to use Git, but you cannot use GitHub without Git. GitHub is helpful when more than one person is working on a project letting them to built a centralized repository where everyone can manage, edit and upload code files avoiding all the confusion in an organized, easy manner.

Getting started with git and GitHub:

Install Git and create a GitHub account

First, you’ll have to install Git and create a GitHub account. You can click here to install Git. Once you’ve done with that, create a GitHub account here.

Configuring git

Then you have to configure your username and email address in your Git Bash terminal. You can use the following command to configure your username.

git config — — global user.name “Username”

You can configure the email address by the following command.

git config — — global user .email “ Your email ”

Create a local git repository (project)

First, you have to create a folder where you want to store all your project files and open it with Git Bash.

Initializing git repository

Next, you have to initialize the git repository. For that, you can use the git init command.

Add new files to the repository

You can use the touch command to add new files to the repository. Here I have added two files as file1.txt and file2.txt ( or else you can create these files manually )

Then you can use the git status command to find out which files’ existence is known by git. As these files are not added to the index (staging area) yet. Here we can see two untracked files. Without instructing git to track them git won’t track those files.

Add files to the staging environment

You can use the git add command to add files to the staging area.

git add -A command will add all existing files to the staging area. You can use git add “<filename> ” to add files separately to the staging area.

After adding the files to the staging area you can check the git status again. Then you can see all the existing files are added to the index and ready to be committed.

Create Commit

Commit means recording snapshots of a repository at a given time. You can use the following commands to do a commit.

git commit

This will launch the default text editor to commit the message and committed snapshots will get staged.

git commit –m “<message>”

With this command, you can directly commit the changes with the commit message. You can see the git commit command has committed changes to the local repository. The commit message should be a clear explanation of your changes.

Create a branch

If you need to add a new feature to the main project you can use a branch to develop the new feature without making changes to the main project. Every Git repository’s first branch is master by default.

You can use the following command to create a branch.

git branch <branch>

And to move inside to the newly created branch you can use the git checkout command.

git checkout <branch>

You can use the git checkout –b <branch> command to create a branch and check out the new branch at the same time.

I have created a file3.txt file and commit it to the newly created branch.

Merge branch

Merging is used to combine the work of different branches.

You can use the merge command to merge the newly created branches to the master branch. When merging make sure to check out in the destination branch.

My destination branch is the master branch. So first I have to checkout into the master branch and then merge the new branch with the merge command.

git merge <branch>

Now the changes you made for the new branch are merged with the master branch.

Pull the changes from GitHub to the local repository

Pulling is done from the remote GitHub repository to the local repository.

After creating the remote repository then you have to copy the URL of the remote repository and add it with the git remote add origin <URL>

In this command, you are adding the remote repository, with the URL as an origin to your local git repository.

Always pull the changes before the push. This will ensure that your local copy is in sync with the remote repository. As other members of the team are pushing to the remote copy, and if you push before syncing up, you could end up with merge conflicts when you push.

To get the changes made on the central repository of GitHub to the local repository you can use the git pull origin master command.

Push Local Repository to GitHub

This command is used to transfer commits made from your local repository to your remote repository.

As I have already merged changes to the master branch now I can use the git push origin master to add my files in the master branch to the central repository.

You can also merge the branch you are working on to the master branch after pushing the branch to GitHub. Let’s assume I’m working on a branch called newProject2

I have used the command git push origin newProject2

When you refresh the GitHub page you can see a notification saying that newProject2 had recent pushes. Then you have to create a pull request to alert the repository owners to merge the changes in your branch to the master branch.

Then you will see a button at the bottom that says ‘Merge pull request’. By clicking that you can merge your changes into the master branch.

After the merge is completed it will give you a message “Pull request successfully merged.

After merging the branch it is better to delete the branch as too many branches will create a mess.

These are the most common, basic commands you will come across when you are dealing with Git and GitHub projects. I hope this article will be helpful for beginners to get a basic idea about Git commands.

for more Git commands!

If you have any suggestions for improvements, let me know!

--

--

Nuwanthi Rajapaksha
Geek Culture

Undergraduate at Sabaragamuwa University. Majoring in Computer Science