LanguagesGetting Started with Git and GitHub

Getting Started with Git and GitHub

Git is a free and open-source version control system. Being the most popular version control system, most programmers interact with Git on a daily basis. A version control system is a tool for computer scientists to keep track of the changes made to code being developed. Basically, a developer can save an initial version of their source code to Git, then when a user updates their code, you can save it again iteratively so that later a user can reference or review a previous version of that code in the future.

For this article, I will be using the command line interface and terminal to interact with git and commit files to Github. If later you feel the need for a more streamlined work process, I encourage you to search for the Git integration tools you will need for your preferred Integrated Development Environment applications. For myself, I am using the Visual Studio Code IDE application from Microsoft in my development process, which you can download for free here.

Read: Visual Studio Git Stash

What Are Common Git Terms a Developer Should Know?

Before getting started, let us briefly discuss a few terms. As a computer scientist, you may have come across words and phrases like directory and command line. Directory is a folder on the computer and the command line is a text editor application that works as an interface between the user and the computer. Many programs may require the use of this Command Line Interface (CLI) in order to interact with them.

While Git can be run using applications, in this article we will learn to use Git through its commands in the CLI. Some commands to familiarize yourself with include cd (standing for change directory), which allows a user to relocate to a different directory using that command, followed by the path of the desired directory.

Those that write code understand that one can write code anywhere using any text document; instead, there are specialized applications called code editors that allow a user to write and edit code with featured assistance. A repository is where a project is stored. It can be a directory on the computer or a hosted project on Github.

People sometimes get confused between Git and Github. Git is the tool developers use to review changes in their source code, whereas Github is the website developers can host their code on. There are several sites like GitHub that can be called remote repositories; other examples of remote repositories include BitBucket, SourceForge, and GitLab.

Read: 6 Best Python IDEs and Code Editors

Git Commands To Get Started

The following is a list of Git commands you need to get started creating, cloning, and working with repositories and codebases.

  • clone: allows a user to take a repository that is hosted on GitHub or a similar site and download it into a user specified folder on their local machine.
  • status: this command allows a user to see information about the repository and branch they are working in and to see the status of the files in that directory.
  • add: when a user makes changes to a project by updating, moving, creating, or deleting files, they can have Git track those changes using this command.
  • commit: allows a user to save their files in Git.
  • push: this command uploads Git commits to a remote repository.
  • pull: download an updated version of the same repository from a hosting site.

Now that we have gone over some of the basics, let us start setting up Git and our environment.

How to Install Git

If you are using a MacOS or Linux machine, chances are you already have Git installed. You can check this by running the command git –version in the terminal. If an error occurs, then you will need to install Git by running one of the following commands:

  • dnf install git-all for RPM-based linux distributions like Fedora
  • apt install git-all for Debian-based linux distributions such as Ubuntu
  • brew install git-all for MacOS

For Windows, you will need to download the Git for Windows installer.

How Do I Setup Git?

After installing Git, we will want to set it up. Users are able to set up configuration variables using the Git config tools provided. All global configurations are stored by Git in the .gitconfig file, found in your home directory. Using the command option –global, set configuration values as global; omitting this option then only configures the current Git repository. You may also want to customize system wide configurations. Git manages the configuration of each user and Git repository, storing them as values in the /etc/gitconfig file. To set or manage these values, you will need root rights before using the –system option.

Now, you will want to set a username and email into Git after installing it. This is important, as Git uses this information to keep track of the commits being made.

To set the username, use the command git config –global user.name “Your Name”

To set the email id, use the command git config –global user.email “y.name@emaildomain.com”.

You may also choose to configure the default text editor that will be used whenever Git requests you to write a message. Otherwise, Git will use the default editor of your system.

To set your own preferred editor, you may need to find the command options specific to your preference. Some examples include:

$ git config --global core.editor “nano -w”
$ git config --global core.editor emacs
$ git config --global core.editor “atom --wait”
$ git config –global core.editor “'C:\Program Files\Sublime Text 3\sublime_text.exe' -w”

For more examples, check out the documentation at Git-scm.com.

Since these commands use the –global option, this information will be used for every Git repository in the system. To use a different association for specific projects, run these commands without the –global option.

Continue to explore your options and make Git your own stylish and efficient tool as you continue to rely on it, along with the rest of your developer toolset.

You can view your settings for Git using the following command:

$ git config --list or git config -l

You can also check specific configuration elements by using git config .
For example:

 
$ git config user.name
Your name

Start using Git in your projects by executing the git init command in the directory you want to start keeping track of in Git. We will go over this more a little later.

Read: The Top Java IDEs

How to Setup and Use GitHub

We will want to have an account with an online code repository host. For this article, we will go through setting up the Git environment with GitHub. Use the green “New” button to create a new repository with “Demo” as the title and hit the “Create repository” button.

Setting Up Git

Proceed to use the “create new file” button to create what is called a “read me” file. This is a file usually written in the markdown (.md) format that gives you the opportunity to divulge a more detailed description about the project you are working on.

Git Tutorial

Writing the name README.md for its name, write a description within said file, then proceed to commit this new file using the green button shown in the image below. This will save your changes in the new repository and you will see it presented as in the following screenshot:

Setting up GitHub

Using the “Edit this File” button (the one that looks like a pen), you can freely edit the file you just made. After editing your README.md file, hit commit again.

Using Git and GitHub

Going to your Demo repo’s main page, you can see your most recent commit, along with its message (if you wrote one at the time of committing). In the corner of the repo’s headliner, you can see a link either labeled “history” or the number of commits you have made in this repo.

If you click this, you can see a timeline of commits with a unique identifier for each, along with the titles and the messages given to them. Clicking on one will show you the changes made to the file those commits are related to.

Git

Git and GitHub Setup

Installing Git

Git and GitHub

So far, we have started using GitHub to create a repository, but we have also prepared Git on our own computer to track code made and edited there. Now let’s see how we can use both in tandem. The way we can work on online code locally is by cloning the repository onto our local machine and tracking it there too. On your GitHub page, go to your repository and find the cloning link under the “Code” button. Copy this HTTPS link, as it will be used on your local machine to download the repository.

Setting up GitHub

On the terminal of your local system, use the command git clone, along with the copied HTTPS link – ending in .git – to download the repository. This creates the Demo directory where you executed the command. Navigate into that repository folder. You can see that this repository is already being tracked by Git, as there is a hidden file within that directory with the .git name. This file is the same as when you have created a Git repository locally by executing the command git init.

Git Repository

Now, use your preferred text editor to edit the ReadMe file in the repository.

Git Repository Settings

Maybe you would like to add another file to the repository. Use your text editor to create an HTML formatted file. Executing the command git status shows you the status of files in your repository. As you can see, the README.md file, which is already tracked by Git, is shown as modified, while the new index.html file is shown as untracked.

Git Congiguration

You can add new and modified files to Git using the git add command option. Adding a period to that command signifies adding all the files in the repository. Using the status command again, you can see that the files are now staged and ready to be committed.

Git Tutorial

Committing your staged files enters them into the .git file as an iteration of your workflow. Use the git commit command and enter a comment using the -m option. This option actually populates the title of the comments for this commit; use a second -m to write more comments for those commits.

How to Use Git

Now that we have prepared everything for our code to be tracked locally, our next step is to push it for the repository to be updated online as well. Before we can do this, we will need a token from GitHub to securely upload our code. For that, we should go back to GitHub. Under settings, go to developer settings, hit Personal access tokens, and generate a new token.

Git Tokens

You will be taken to a menu where you will choose what access this token allows for. Pick the repo options as those will allow us to push and pull repository files.

Git Repository Token

You will see a set of characters generated for you – this is your token. Copy this for use in your local machine. This will allow us to upload the code made locally into GitHub over a HTTPS connection. Later, you may also choose to use SSH keys to connect to your repository. On your command line interface, use the command git push to upload your changes to the repository. Be sure to use the options origin and main, as you should be working with that copy of the files you are working with and the branch you are working in. You will be prompted for the username and password of your GitHub account; use the same username, but use your generated token instead of the account password.

Git Commands

Go back to your GitHub and you will see that the repository you have created has been updated with the changes you made on your local system.

Create a Git Repository

Guide to Git and GitHub

If you decide to update this same repository on GitHub, you can use the git pull command on your local machine to update your locally linked repository.

Conclusion to Getting Start with Git Tutorial

Now, you have set up and used both Git and GitHub for a project, and you are ready to use these tools in more projects. Sincerely, these will be effective tools for your development process, and I encourage you to explore more of both Git and GitHub.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories