Git is a popular version control system in the DevOps world, and GitHub is an online platform where developers can collaborate with their Git repositories. In this Git/GitHub beginner's tutorial, you'll learn how to get started without requiring any new knowledge about version control systems.
This entry is part 1 of 7 in the series A Git/GitHub beginner's guide

A Git/GitHub beginner's guide

A Git/GitHub beginner tutorial

A Git branch example

Git reset vs. git revert

Merge branches on GitHub

A git merge example

Resolve content (line) merge conflicts in GitHub

Resolve modify/delete merge conflicts in Git

Edem Afenyo

Git and GitHub

Version control systems (VCS) are software tools that help you keep a history of the changes made to files over time so that you can access previous versions on demand.

Git is one of many version control systems, along with others like Subversion and Mercurial. Git is a distributed VCS, which means that instead of having a central server to which all clients maintain a live connection, every user has their own copy of the full history of changes. This enables clients to work independently.

A central repository may be set up to collect the changes in each client's local repository and give each collaborator a full view of the current state of the project. This is useful but not mandatory, and allows Git to be more tolerant of server faults while enabling more flexible collaboration than centralized VCSs like Subversion.

On the other hand, GitHub is an online platform where developers can collaborate on software projects, manage their Git repositories, review code, track bugs and features, and secure code. Read this article to learn more about the interplay between Git and GitHub.

Installing Git

On most Linux distributions, you can install Git with the default package manager if it does not come preinstalled. For example, on Ubuntu, Git can be installed via its package manager, apt, as follows:

sudo apt install git

For Windows, visit the Git website and download the installer. On macOS, I recommend first installing the popular packet manager Homebrew and then installing Git with this command:

brew install git

Create a new repository

To create a new Git repository for your project, run git init at a command prompt, as shown in the snippet below. A Git repository is a tree of files and folders, usually named .git, for storing the metadata and history of tracked files. The name of the project in this tutorial is myproj. You can use any other name.

git init myproj

This is the default way to initialize a repository and results in the .git directory being created as a subdirectory of your project folder. If the project folder doesn't exist, a new directory will be created and initialized as a Git project.

Initializing a repository with Git Init

Initializing a repository with Git Init

On Linux and Windows, you can see some of the contents of a new Git project with the tree command, shown below. The switch -a allows you to see hidden folders like the .git directory.

tree -a myproj
Displaying the contents of a repository

Displaying the contents of a repository

Track changes with Git

After setting up a repository with git init, you need to select files in the project directory for tracking. In this section, you will create an example project file and select it for versioning.

Create a new Python script named os-version.py in your project directory, and save the following snippet in it.

import platform
print(platform.system())

Your file should resemble the file in the screenshot below.

Tell Git to track all the files in your project directory, including the file you just created, with the git add command, as follows. This is also known as staging files.

git add .

Instead of a period (.) for the current working directory, you can also track a specific file by typing in its filename.

Run git status to get a report on the state of the files Git is tracking.

git status

You should receive feedback, as in the screenshot below, confirming that the new file has been staged.

Confirming the status of a repository

Confirming the status of a repository

Commit changes to a Git repository

Great. You now have changes to be committed. However, before you commit any changes, you need to configure the repository with a username and an email address. This helps to identify the source of commits and is useful in a project with many collaborators to show the authors of certain changes.

Run the git config command to set your username and email, as shown below. This tutorial uses weebo and [email protected] for the username and email address, respectively.

git config user.name "weebo"
git config user.email "[email protected]"

Type git commit, as below, to commit all tracked changes to the repository with a message (-m). The message should be short but descriptive, allowing you or your team members to know what changes are covered in a particular commit. Commits also serve as reference points that allow collaborators to navigate the history of a project.

git commit -m "added script to determine platform"

As shown below, a successful commit operation confirms how many files were changed and the nature of the changes made.

Committing changes to the repository

Committing changes to the repository

Viewing the change history of a repository

It is often useful for a collaborator to walk through the history of a project to see the changes that have been made. Since Git is a distributed VCS, it has built-in support for independently investigating the timeline of changes in a repository.

If you are a second developer, you can execute the git log command with the oneline switch, as below, to see a list of recent changes. (A few more optional commits have been added for this demonstration.)

git log –oneline
Listing previous commits

Listing previous commits

You can go further to see the details of a particular commit with the git show command. For example, you can see the changes in the earliest commit using its commit hash, as follows:

git show 

You can see what files were added along with the lines that were modified, as shown in the screenshot below.

Viewing details of a previous commit

Viewing details of a previous commit

You can also use git log with the -p switch to view the log with corresponding change details. This is often so verbose that a pager is needed to help you navigate the log in the terminal. Tap the up and down keys to move by line or the space bar to move by page. Quit the Git log with the q key.

Listing historical commits with change details

Listing historical commits with change details

This tutorial covers a newly created repository with a single branch, main. However, it is also possible to use the Git log to view the history of another branch, as shown in the command snippet below:

git log <branch name>

Connecting your repository to GitHub

To make your project more accessible to other collaborators, you can create a more central remote repository for your project on an online platform, such as Github. This is a common follow-up step, as remote repositories allow all collaborators to keep abreast of ongoing development.

First, add a remote repository named origin to the configuration of your project. If you don't already have a GitHub account, you should create one now here. The remote address should be the web URL of an empty GitHub repository. You can create a new repository and copy its web URL to follow along for this section.

Creating a GitHub URL

Creating a GitHub URL

Supply the web URL to the git remote command as follows to add the new remote:

git remote add origin <Your GitHub URL> 

Your repository is associated with a remote, so you can push your commits upstream. However, typing git push at the command line will require authenticating to the GitHub API, and that cannot be done with your regular password. Instead, you should generate a Personal Access Token in the browser.

Creating a Personal Access Token

Creating a Personal Access Token

With the token in hand, execute git push in your terminal as shown below. Turn on the set-upstream-to (-u) switch to set the main branch of the remote as the default remote branch for your local main branch. After this step, a simple git push or git pull will be enough to keep your repository current.

git push -u origin main

After entering your username and access token, you should receive confirmation of a successful push as follows

Pushing your first commit to a remote origin

Pushing your first commit to a remote origin

Now, you and your collaborators can push and pull changes to and from GitHub.

Uninitializing a Git repository

The easiest way to remove versioning from a project is to remove the .git subdirectory. On Linux, you can use the rm command within the project folder to remove .git and its constituents (-r) forcibly (-f), as follows:

rm -rf .git

Please use this with caution, as all the history of the project will be lost.

Conclusion

In this beginner tutorial for Git and GitHub, you learned how to create a Git repository with git init, view the changes made to the repository, and then share it with other developers via GitHub. If you have questions, please leave a comment below.

Subscribe to 4sysops newsletter!

In the next part of the Git/GitHub beginner's series, I will introduce Git branches.

A Git/GitHub beginner's guide

A Git branch example
3 Comments
  1. info
    Stellispro 3 years ago

    Nice and informative article

  2. mcs3ss2
    mcs3ss2 2 years ago

    oh man
    i was looking for something to get me of the ground for github
    i will be reading this series and testing / experimenting. may throw a question at you here and there lol

    • Edem Afenyo Author
      Edem Afenyo 2 years ago

      That’s nice to hear. Happy to help out if you need any thing.

Leave a reply

Please enclose code in pre tags: <pre></pre>

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2026
WindowsUpdatePreventer

Log in with your credentials

Forgot your details?