Must know Git Basics — For absolute beginners

Sandali Tharuki
Nerd For Tech
Published in
8 min readMar 29, 2021

--

Courtesy: https://unsplash.com

It is a good practice to use a version control system as a software developer. GitHub is a platform for version control where developers can keep a copy of a project and collaborate with co-workers or other developers. Of course, GitHub makes the lives of software developers stress-free.

In this tutorial, I discuss GitHub basics including Create a repo, Branching, Commit changes, Push, PR, Pull, Merging.

Prerequisite: Get installed Git on your computer

Let’s start your Git journey.

Create a GitHub repository

A local project can be stored in a repository which is called a repo. Repo is literally a location where all the folders, files, videos of a project can be stored. The local repo is the location where the particular project is stored in the computer. The remote repo or Git repo is the location where the particular project is stored in the GitHub server. Each project has its own unique GitHub repository.

Under this step, you will be able to learn how to create a GitHub repo, First commit, and then push the first commit to the remote repo.
To create a remote repo you can use either a GitHub platform or Git Bash. In this tutorial, I show you how to create a remote repo using both methods.

  1. Using GitHub

First in first, click the PLUS (+) icon on the top right corner which will direct you to “New repository”. Then fill input fields with relevant details. You can select either “Private” or “Public” according to the purpose of the project. After some more steps, you can create the repo.

Create a new Git repository

Congratulations!!! You just created your first Git repository. Now you can start either by simply clicking “ creating a new file “ or “ uploading an existing file “ or using Git bash.

1.Git commands

To start with Git bash, open the Git bash in the directory where the local repo is stored and follow the below commands.

  • First, you should initialize the Git repo.
git init
  • Then create a new text file or README file in the directory. This is the very first file you are going to add to your repo. ( You can use “ echo “# test” >> README.md “ to create the README.md file )

After creating the first file, you should add it to the local repo.

git add filename.txt OR git add README.md
  • Here, you did the first change to your local repo and you should save that change as a commit.
git commit -m “write a commit message”

Always try to write some meaningful message which can easily recognize the changes you made to the project files when it is necessary.

  • Then, you should create the “ Main “ branch
git branch -M main
  • You are about to end this process. Now all you have to do is add the local repo to your remote repo.
git remote add origin https://github.com/GitHub user name/repo name.git
  • Finally, push the changes made to the local repo to the remote repo.
git push -u origin main
Open the Git bash in local directory and use the above commands

2.GitHub

Add files using GitHub

Branching

Branching is one of the notable plus points that motivates us to use GitHub. This is pretty handy in team play. Each member can have their own branch and the changes (commits) they make to the project (local repo) they can save (push) in their branches. Only the correct and final version can push into the Master branch. So that, you can keep a clean project repo without having any mess.

Either Git commands or GitHub can be used to create new branches.

1.Git commands

Either Git bash, IDE terminal, or any other preferred terminal can be used. In this tutorial, I use Git bash.

git branch branch_name  -->  TO CREATE A NEW BRANCHgit checkout branch_name  -->  TO SWITCH FROM CURRENT BRANCH TO NEW BRANCH
Create a new branch and switch to it using Git commands

2.GitHub

You can easily create new branches using GitHub. All you have to do is visit the remote repo in GitHub and click the left upper button with the Branch icon on it. Give the name in the input field and hit “Create branch”. Here you get your new branch. Simply click on the branch and you can switch to it.

Create branch using GitHub

Commit Changes

Local changes can be saved as commits in the remote repo. The changes made to the project files can be easily traced when it is necessary. The best thing I love is if somehow you lose your project codes you can backup them again and continue the project without worry.

Here also either Git commands or GitHub can use to make commits.

1.Git commands

git commit -m “write a commit message”

Always try to write some meaningful message which can easily recognize the changes you made to the project files when it is necessary.

2.GitHub

This method is pretty handy when you add new files to your repo. You can directly add new files with a simple click on “ Add file “ which is on the upper right corner. It directs you to the next step where the change can save as a commit.

Commits using GitHub

Push

When we need to upload the changes we made from the local repo to a remote repo, we can use Git push. As a practice, Git push performs after Git commits. The common Git push commands are,

  • Push the content of the local repo to a branch of the remote repo.
git push origin <branch name>
  • Push the content of the local repo to the master branch of the remote repo.
git push origin master

You can also use your preferred IDE to execute Git push.

Fetch

Git fetch is used to get newly pushed commits in a remote repo that are not available in your repo. Before proceeding with Git pull and merge I advise you to use Git fetch command. It avoids conflicts that can be invoked in merging. You can proceed with fetch commands as per your intention. Available cases are mentioned below.

  • Fetch a remote repo

An entire Git repo can fetch with the aid of the below Git command.

git fetch <repo URL>
Fetch a remote repo
  • Fetch a branch

A specific branch of a remote repo can fetch.

git fetch <branch URL> <branch name>
Fetch a branch
  • Fetch all the branches

All the branches created in a specific repo can fetch using the below Git command.

git fetch — all
Fetch all the branches
  • Synchronize the local repo

To synchronize the local repo with the remote repo.

git fetch origin
Synchronize the local repo

You can also use your preferred IDE to execute Git fetch.

Pull

Git pull is used to update your local repo with the changes of the remote repo. Here the content of the remote repo over-writes on your local repo. Merge conflicts can be invoked when proceed with Git pull. The common using Git pull commands are,

  • Default command
git pull
  • Pull from a remote branch
git pull <branch URL> OR git pull origin <branch name>
  • Pull from the master branch
git pull origin master

PR

PR or Pull Request is another plus point that motivates developers to use GitHub which is pretty handy in team play. Opening a PR you can request your colleagues to review your changes to project files and merge them either to their branches or to the Master branch. In this step, the differences among the codes can be traced. Though you can open a PR and accept it on your own, I advise you to discuss it with your teammates before merging it.

You can use Git commands, IDE shortcuts or GitHub to open a PR.

git request-pull [-p] <start> <url> [<end>]

1.Git commands

  • [-p] → Include a suitable text. (Ex:- Which version)
  • <url> → Include the repo URL that to be pulled from.
  • <start> → Include the start commit.
  • <end> → Include the end commit.

2.GitHub

To open a PR using GitHub, click the “ Pull request “ on the top navigation bar which directs you to the “ New pull request “ window. Simply click on it. Now you have to pick the correct “ base branch “ and “ compare branch “ over other branches.

Open a PR using GitHub

Merge

Merge is the next step of PR. Here occurs accepting the changes made. Before accepting code review should be done and it is much better if you could review the changes you made with your teammates to avoid unnecessary disputes.
As well as other Git basics, merging also can be processed using Git commands and GitHub.

1.Git commands

First of all switch to the branch, you need to merge into.

git checkout branch_name

Then type,

git merge branch_name

The branch_name is the branch that you need to merge from.

2.GitHub

After compare and open a PR you can simply click on “ Able to merge “ and proceed with the merge. Merging is only allowed if the branch has no conflicts. If the path is clear you can just go and click “ Merge pull request “. Then you will get to the confirmation window where you can confirm the merge.

Firs step
Second step
Confirm merge

Conflicts can be invoked when proceed with Git merge. I suppose to discuss it in a future article in my Git tutorial series.

Excellent. You just now learned all the Git basics you should know as a newbie. Give yourself a pat on the back.

This is the end of the first tutorial on Git. I hope this would be helpful to you.

Thank you!!!

--

--

Sandali Tharuki
Nerd For Tech

Undergraduate-Faculty of Information Technology | University of Moratuwa | Sri Lanka