Git and Github for Beginners

Martins charles
10 min readOct 1, 2023

--

By Charles Martins

Git and GitHub

Being a computer programmer means developing software, often with others.

So even if we have established what dotnet core is, written our first hello world code in C#, and understood key concepts in C#, we must learn how to use collaborative tools that help us work on projects.

As a programmer, your role doesn’t just end with writing code. A lot of times, you will be writing code that others have to contribute to. It would help if you also learned to collaborate with others using tools like Git, Github, Jira, etc.

Today, I’ll show you how to use Git and GitHub in software development. This article will address the following:

What Git and GitHub is

How to set up Git and GitHub

Making a commit in Git

Branching and Merging in Git

Git and GitHub

What is Git?

Git is an open-source software developed in 2005 by Linus Torvalds. It allows you to track changes in a distributed version control system.

A distributed version control system enables each software developer to have their own copy of the source code. This way, they can make changes to the source code, review the code, and even update it.

Git allows you access to the entire codebase and its history (revisions) on your local machine. The beauty of Git is its ability to branch and merge. This means you can create changes without messing up the main code base, and when you are done with those changes, you can merge them into the main branch.

Git is just one of the distributed source code management systems; there are others like Mercurial and Bazaar.

Git as a distributed version control system helps software development teams achieve the following:

Clone repositories and create backup files.

A repository contains all your project files and their revision history. Using Git, every member of the team can have their own copy of the codebase to work with. You can also clone the files and put them on a hard drive as a backup in the event that a catastrophe happens.

Faster builds with flexible branching and fast merging.

You can accelerate the delivery of projects with branching and merging. It is a distributed workflow where members of the team can work on different parts of the project and merge them without having to interrupt the workflow of others.

Code review and swift feedback.

Code can be reviewed and changes can be made quickly with a distributed version control system. Since developers can get a local copy, they can effect changes, track changes, revert to previous changes, and debug easily.

What is GitHub?

GitHub is a platform where developers who use Git can collaborate on open-source projects. GitHub allows members to share files, host their projects, and work on projects together. It uses Git under the hood but has all these other features that provide benefits to its users.

While Git simply helps you track changes, GitHub provides you with much more features, such as:

  1. Project management: GitHub has added features that can help project managers supervise software development on the platform. With GitHub Issues, project managers can create tasks (issues), assign tasks, track progress and relationships, leave notes, and make reports promptly.
  2. Safely handling packages: You can safely publish and consume software packages (e.g., APIs), which may be private or unique to your team or open-source to everyone, using GitHub Packages.
  3. Code safety and improved code writing: GitHub has tools to identify and analyze vulnerabilities in your code that you may have missed. Then you can review the code and make appropriate changes easily.
  4. Code hosting: GitHub serves as a platform where millions of repositories are stored to be retrieved at a later time.

Git vs GitHub

Although the purpose of Git and GitHub is to collaborate in software development, here are the key differences you have to note between the two:

  • Git is a version control system that is installed and maintained on your local computer, while GitHub is a Git repository hosting service that is exclusively cloud-based.
  • Git is designed to work well with text files, while GitHub expands on Git’s functionality and provides a graphical user interface.
  • Git is completely open-source. Github, on the other hand, is a profit-for-service business; although some of its functionalities are free, it is still a paid service.

Setting up Git and GitHub on your windows

Setting up Git

Download Git: The first step is to download git on your system. Visit this site to download the latest git version for your PC

Use the command prompt to access Git: Once you have installed Git, you can check what version is installed.

Go to the command line on your PC and type in the command below:

git --version 

The command line will display what version of git is installed. There are other git commands that you can use to see them type this into the command line:

git --help 

You will get a display like this:

Setup user details: Now it is time to set up your details for your GIT such as username and email.

Using the command line, you can set a user name using the following code:

git config --global user.name charlesmartins 

This will set the global username as charlemartins so that when you track changes and update code using git, you will be seen as charlesmartins

You can set up a user email by typing the following into the command line:

git config --global user.email martinscharles561@gmail.com 

This will set my global user email as martinscharles561@gmail.com.

Create a directory and initialize it: Now let us create a directory from the command line, initialize the directory, and commit changes to it.

First, you create a directory using the command mkdir. Let’s name this directory BashExercises

So we enter the following into the command line:

mkdir BashExercises

You enter this directory using the command cd.

cd BashExercises
creating directory

While inside the directory, we initialize it using:

git init

Initializing a directory turns it into a git repository. If you have done this successfully, you should see the folder BashExercises on your laptop.

Add files to Git repository: Now this is a git repository that you can work on, whether it is a adding a new file or updating the contents of the repository.

For instance, let’s add a text file called demo-test.

Create a text file called demo-test within the Git repo: BashExercises. like this:

The text document should contain some text like, “Hello world”.

Once you have created this document, it is important for you to append the text file to the directory; otherwise, Git will be unable to track the file. You can do this using the command git add. This is called adding a file to the staging environment.

git add demo-test.TXT

This will make sure the file is tracked. To make sure that all your files in your directory are tracked, you check the status of your Git using git status

git status

Check Git status: Your Git status should indicate that you are successfully tracking changes. Next, you can commit the file. A commit is a record of all the changes that you have made. After you have used git add to add all the files you wish to commit, you can then tell git to package them into a commit using the code:

git commit -m "committing a text file"

The message in quotes is important because it helps other programmers who may use your code understand why you have made that commit and what the commit does.

As I stated earlier, the best thing about git is the ability to branch and merge. So let’s branch and merge.

Creating a new branch: If you wish to make a new feature and develop it without affecting the main code, that is where a branch comes in.

git checkout -b new-world

The code above creates a new branch called new-world and then moves to that branch. Now whatever changes you make will be made on that branch.

You can check what branch you are on by typing the code below into the command line:

git branch 

The first branch is called master or main, and is known as the primary branch; any other branch is often named like new-world.

After working on your branch, if you wish to merge it, simply type:

git merge new-world

And just like that, we created a repo, made a commit, created a branch, and merged it with the main branch.

If you wish to exit the branch that you are currently in and return to the main branch, use:

git checkout master

If you want to keep track of your files locally, then Git helps you do that, but if you wish to collaborate with a team then use GitHub. Now that we have set up Git, it is important to set up GitHub to make sure that your repositories can be shared and hosted on the cloud. This way other members of your team can track changes on a public platform.

Setting up GitHub

Sign up: Go to the GitHub website. Click on sign up and follow the prompt.

Once you have signed up you can sign in to your account and get started.

Create a new repository: As I mentioned, GitHub is very much like Git, we can also create a repository on our GitHub. The difference is you are interacting with a user interface to achieve this.

Once you are logged in to your GitHub account, you will see something similar to the image displayed below. Click on the icon on the left depicting new to create a new repository.

Once you click on new, you will get a repository template that will help you specify the kind of repository you wish to build. There will be details like, if the repository will be public or private, if there are licenses to add, and what files the repository will take.

Click on Create Repository, and there you have it.

Now remember that we can create a repository from the command line using Git. And we have now learned how to create a repo on GitHub.

Push repo from Git to Github: You can also push a local repository from the command line to GitHub.

I have created a repository called C-test. To push the repo that I have created in Git called new-world to the GitHub repository I have created, all I have to do is, copy the link of the repository I posted and do the following in the command line:

add the GitHub link

git remote add origin https://github.com/MartinsCharles/C-test

push the git repo to GitHub

git push -u origin bad

You will see a prompt to authenticate this action and give git access to your GitHub.

And just like that you pushed your repo from Git to GitHub using the command line.

If you wish to create a branch of the repo that you have just pushed, you simply click the branch option on the repository.

You name your branch and create it. Let’s say you create a branch called good. Once your branch is created, you can then add files to it.

I am adding a file called go to the branch good in the image above.

It is so much easier to use GitHub to create repositories, add branches, commit, and merge because of the graphical user interface. All you have to do is click on the icons on the screen compared to typing in commands like in Git.

So far, we have seen how Git and GitHub work. For the sake of the length of this article, I will not explain GitHub with VSCode but you can watch this video to get a hang of it:

With all the knowledge we have shared here and those in my previous articles, you are ready to write your first C# project and host it on GitHub.

Next time we meet, we will be writing our first C# project and hosting it on GitHub!

REFERENCES

Everhour Blog. (2023). What Is Github: Main Features, Benefits, Pricing [2023]. https://everhour.com/blog/what-is-github/.

Git vs Mercurial vs Bazaar W3schools. https://www.w3schools.blog/git-vs-mercurial-vs-bazaar

Git Installation on Windows: A Step-by-Step Guide [2022 Edition]. https://www.simplilearn.com/tutorials/git-tutorial/git-installation-on-windows.

Git Push Local Branch to Remote — How to Publish a New Branch in Git. https://www.freecodecamp.org/news/git-push-local-branch-to-remote-how-to-publish-a-new-branch-in-git/ [Accessed 28 Sep. 2023].

‌‌How to Use Git and GitHub—Introduction for Beginners https://www.freecodecamp.org/news/introduction-to-git-and-github/.

The benefits of a distributed version control system. https://about.gitlab.com/topics/version-control/benefits-distributed-version-control-system/.

Nelson, M. (2015). An Intro to Git and GitHub for Beginners (Tutorial). Hubspot.com. https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners.

What is GitHub And How To Use It? Available at: https://www.simplilearn.com/tutorials/git-tutorial/what-is-github.

‌‌‌

--

--

Martins charles

Hi I'm Charles. A life long learner, welcome to my thought box, if you stay long enough I have exciting things to share!