DEV Community

Cover image for Git and GitHub for beginners
Ericawanja
Ericawanja

Posted on

Git and GitHub for beginners

Topics covered

  1. Introduction to version control systems
  2. Git and GitHub
  3. Branching and merging
  4. How to contribute to open source
  5. How to sync your Repo

Git and GitHub are must-learn tools for every software developer. These tools not only make it easy to track changes in your personal projects, but they also make it seamless to contribute and coordinate team projects. This guide is to help every beginner to harness their skills and have an easy time learning and using these tools. Let’s get into it.

What is a version control system?

Version control systems are tools that are used by software developers to track and manage changes in a project. These tools keep track of the change made, who made and the time it was made. Thus, making it easy to turn back to the previous versions of a document and identify errors if they occur.

Types of version control systems

There are three main types of VCS, namely;
• Local version control system
• Centralized version control system
• Distributed version control system

A local version control system is a type of VCs that uses the local database in your computer to store the changes made. It stores them as a patch where every patch contains the changes made to the file since the previous versions and not the entire file. Consequently, to see how the file appeared at any instance, you have to add all the relevant patches to come up with the document.
lvcs

A centralized version control system uses a central server to store the repository. Thus every developer must be connected to the central server to access the repository and make the changes. Even though it is quite easy to maintain a single repository in the server, in case the central server crashes, you may risk losing the data which is a major drawback. An example of centralized VCS is Apache Subversion which is abbreviated SVN.
cvcs

Distributed VCS

Unlike the centralized VCS, every contributor has their own local copy of the main repository. Thus they can change, update and commit to their local repository without interfering with the main repository. The contributors copy the main repository by cloning it. Also, they have to stage, commit and push the changes to make them available for other contributors to see.
dvcs

After knowing what a version control system let is us dive into GIT

What is git?

Git is an open-source distributed version control software that was created in 2005 by Linus Torvalds for the Linux kernel. It allows a team of developers to work on a project where each of them has a copy of their main repository which is located in the central server.

Features of Git

  1. Free and open-source- you don’t have to pay anything to use Git. Also, its source code is available openly, hence you can modify it to your preference.
  2. Distributed development- Git supports cloning where the contributors can store a local copy of the main repository
  3. Supports non-linear development – git supports independent lines of code known as branches, which can be staged, committed, and updated independently of the main codebase. Hence, branching gives a developer a safe space to implement and test something new without interfering with the codebase. The branches can later be merged into the codebase.
  4. Scalable- Git is quite scalable making it easy to handle an increase in the number of collaborators.
  5. Secure – this tool uses the secure hash function (SHA1) for naming and identifying objects in a repository. Hence every change is well monitored making it impossible to make changes without being noticed. ##What is GitHub? GitHub is a hosting company that was founded in 2008 that provides a platform for developers to host and share their projects. it provides access control and management features for every hosted project. Basically, it offers source code management among other features of git.

There are many alternatives to GitHub such as GitLab and bitbucket. Consequently, you do not necessarily need GitHub to use git, but you require git to use GitHub.

Getting started with GIT

You need to install git on your operating system. This guide here will help you.
Also, create a GitHub account, and let’s move on.

How git GitHub woks and its commands

github

Let's discuss the diagram briefly;
After the user makes changes on the working file, git takes notice of the recently modified files. the user can check the modified files using git status command.

The modified files are added to a staging area which acts as the temporary location for the files.

All the staged files are ready to be committed/moved to a local repository. Up to this point the changes are only visible on the developer’s local machine and cannot be accessed by other contributors online.
To make the file available online, you need to host on an online hosting service such as GitHub. So, you need to push the local repository to sync it with the remote repository.

Take, an instance that the other contributors have pushed changes to the remote repository. You must sync your local repository with the remote repository to be up-to-date with the changes done. To do that you use Git Pull command which will sync your local repository.
Let’s now look at the git commands

1. Git status

The git status command lists all the files that have been modified recently and has not been added to the local repository. In the below case the Two Sum.md file has not yet been added to the local repository

PS C:\Users\ERICA WANJA\Desktop\coding problems> git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        leetcode/Two Sum.md

nothing added to commit but untracked files present (use "git add" to track)
PS C:\Users\ERICA WANJA\Desktop\coding problems>
Enter fullscreen mode Exit fullscreen mode

2. Git add

git add command is used to move the files to the staging area for committing. To stage your files use the commands below;
git add filename- to add a specific file

git add -all which is shortened to git add -A stages all the files, that is the new, modified and deleted files. On the other hand the git add . command stages only the new and modified files excluding the deleted files.

3. Git commit

git commit moves the repository from the staging area to the local repository. In other words, it stores the snapshots of the changes made to the local repository instead of blindly copying the entire repository one more time.
Every time you are making a commit you must give a brief message explaining the changes made.
For example;

git commit –m “solved the two sum problem”
Enter fullscreen mode Exit fullscreen mode

4. Git pull

The pull command helps to keep your local repo up-to-date with the remote repository. It up streams any change made by another contributor in the remote repository to your local repository.
To pull the changes, you need to set the origin or parent remote repository
Command:

git remote add origin LinkToTheRemoteRepo
Enter fullscreen mode Exit fullscreen mode

After setting the origin remote repository, you can now pull the changes using the below command;

git pull origin master
Enter fullscreen mode Exit fullscreen mode

*Note: * it is a good practice that you pull the changes before the push command when working on a team’s project.

5. Git push

git push command transfers the commits changes from the local repository to the remote repository(GitHub). Thus, the changes you have made will be published on the central repository and made available online.
Commands
git push origin main pushes the commits to the main or the master branch
git push origin branchName pushes the commits to the named branch.(you will learn about branches shortly)

Branching and merging on GitHub

Branching simply means creating a different line of development (branch) where you can test and experiment new things before implementing them into the main codebase. Thus practicing branching will save you from messing up with production codebase.

After testing the new changes on the branch, you can later on integrate them with the main line of development. This act of integrating the branches to the main line of development is known as merging.

Git branch commands

git branch branchname command is used to create a new branch
git checkout branchname command is used to move to the specified branch. After checking out to the branch you can now commit and push the changes.
git checkout-b branchname command is the short form of the above two commands. It creates a new branch and moves (checks out) to it at the same time.

Creating remote branches

A locally created branch is only available on your device and not available to the other team members. You can push this local branch to make it available remotely using the below command.

git push u origin <name>
Enter fullscreen mode Exit fullscreen mode

Deleting branches

git branch –d branchname command is used to delete branches after merging the changes.
In some cases git may refuse to delete the branch if it has changes which have not been merged with the main branch which is a safety mechanism to prevent accidental loss of data.

However, if you are sure you want top delete the branch with uncommitted changes, you can use the command below;

git branch –D branchname
Enter fullscreen mode Exit fullscreen mode

Also, you use the following command to delete a remote repository;

git push origin --delete branchname
Enter fullscreen mode Exit fullscreen mode

I greatly appreciate that you stopped by. I hope the article has been of help. You can also this article on how to contribute on GitHub and how to sync your repository.

Discussion (7)

Collapse
bobbyiliev profile image
Bobby Iliev

Great post! Well done!

For anyone who wants to learn more, I could suggest this free eBook here:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the eBook use one of the following links:

📘 Chapters

Collapse
webstellar profile image
Peter Onyegbule

The ebooks aren't accessible anymore.

Collapse
bobbyiliev profile image
Bobby Iliev

Just tested it and it seems to be accessible.

Collapse
andrewbaisden profile image
Andrew Baisden

Great post for anyone new to GIT these commands should also work on other platforms like GitLab and Bitbucket.

Collapse
ericawanja profile image
Ericawanja Author

Thank Andrew. I greatly appreciate your comment

Collapse
webstellar profile image
Peter Onyegbule

Great post

Collapse
ericawanja profile image
Ericawanja Author

Thank so much Peter for your kind words