DEV Community

Ng'ang'a Njongo
Ng'ang'a Njongo

Posted on

Basics of Git and GitHub

What is Git?

Git is a tool that tracks changes to files that are specified in your folder. Every change made in the project folder is tracked and is saved using commits. Each commit saves the state of your files at that moment and records who made the change and why. Each commit corresponds to a new version of the project folder.

GitHub

This is the web interface that safely stores all the versions that have been made and pushed from Git.

Importance of Version Control

From the Git definition earlier, one benefit is that it allows you to track the history of changes to a project folder (who, when, why). Others are listed below:

  • You can experiment freely and safely as you can always revert back to a previous version without the fear of breaking things

  • It provides clear and documented history of changes. You are not caught in the chaos of saving files as "final_project_v7.zip"

  • Allows members / developers to collaborate on a project simultaneously without overwriting each others work

  • Avoids a situation where you have a single point of failure as each developer has a copy of the entire project

1. Pushing Code to GitHub

Create an empty folder
We'll begin by creating an empty folder on your local machine using Git and navigate to it as shown below:

Creating a folder

Initialize Git
In the current directory, we then need to initialize Git. This is done using the "git init" command: git init

Initialize Git

Create a repository on GitHub
Before we push code to GitHub, we'll need to create a repository that will store our pushed code from Git. You can do this by navigating to GitHub and selecting "New Repository" as shown below:

Creating a Repository

Connecting Git to GitHub repository
Once the repository is created, we can then connect Git with the specific repository we created using the code below:
git remote add origin https://github.com/Nganga7/LuxDev_Assignments.git

Creating a new file
In the directory "C:\Nganga\LuxDevHQ\Assignment1\", we created an empty text file "Doc_one.txt" and added the line "First Doc" as shown below:

Text File Creation

Writing to the new file
Text File Contents

Track Git changes
Since we've made changes to our project folder (creating a new file & writing to the file), Git will track the changes. Initially there was nothing to be tracked. See below:

Before creating the file
We use git status to track changes made in our project folder

Before file creation

After creating the file

After file creation

Staging and committing changes
After making the changes, we notice that there are untracked. We therefore need to add the files to the staging area and commit these changes before we push our code. We do this by using git add Doc_one.txt and commit (with comments) by git commit -m "First Commit" as shown below:

Git add & commit

We can view what has been committed using git log and as shown below, we see the: Commit ID, author (who), date & time of commit and comments made during the commit:

git log

Pushing to GitHub repository
All good to push our file with it's contents to GitHub. We do this using the command git push -u origin master

Pushed from Git

File available on GitHub
The file has now been pushed to GitHub and is now available to multiple collaborators who may need to pull it:

Pushed to Git

2. Pulling Code from GitHub

To pull a file from GitHub, we'll create a new file in our repository called "Doc_two.txt" and we'll write to it "Second Doc" as shown below:

Adding a file

Adding a file

Creating the file

Creating the file

We then need to commit these changes and add a comment to it. Similar to what we did when we pushed the "Doc_one.txt" to GitHub using the command git commit -m Doc_one.txt

Second Commit

Our GitHub repository now has both files "Doc_one.txt" and "Doc_two.txt" but our local just has the "Doc_one.txt" file

GitHub repository

GitHub Repo

Local Git repository

Local Repo

To finally pull the new file to our local we use the git pull command

Git Pull

As shown above, we have successfully pulled the "Doc_two.txt" file from GitHub to our local Git repository.

Conclusion

In this article, we were able to cover the following:

  • What Git is and why version control is important

  • How to push code to GitHub

  • How to pull code from GitHub

  • How to track changes using Git

Top comments (0)