Basic Github Usage
Introduction
If you are into software development, you must have heard of Git and GitHub. Git is a version control system that allows you to track changes in your codebase. GitHub is a web-based platform that provides hosting for Git repositories. In this article, we will discuss the basics of Git and GitHub.
What is Git?
Git is a distributed version control system that allows you to track changes in your codebase. It was created by Linus Torvalds in 2005 to manage the Linux kernel development. Git is a powerful tool that allows you to collaborate with other developers, track changes in your codebase, and revert to previous versions of your code. It may be a handy tool for managing your codebase. reverting faulty code, and maintaining a collaborative project.
What is GitHub?
As the name suggests, GitHub is a web-based platform that provides hosting for Git repositories. It was created in 2008 by Tom Preston-Werner, Chris Wanstrath, and PJ Hyett. GitHub allows you to store your code in the cloud, collaborate with other developers, and track changes in your codebase. It is a powerful tool that allows you to manage your codebase, collaborate with other developers, and contribute to open-source projects. (Now, I believe, Microsoft owns GitHub.)
Git Basics
We will start with some concepts from git to understand most of the opportunities GitHub provides also. Here are some basic concepts for git:
What is .gitignore
and README.md
?
Well,
.gitignore
is a file that allows you to ignore some files or folders in your repository. For example, you may want to ignore some temporary files, or some files that are generated by your code. You can add these files to the.gitignore
file, and git will ignore them when you commit changes. It can be also useful for hiding sensitive information like tokens.README.md
is a file that provides information about your repository. It is usually the first file that people see when they visit your repository on GitHub. You can add information about your project, how to use it, and how to contribute to it in theREADME.md
file. In some rare cases, using only an introduction and redirecting to the repository’s wiki is also a good idea.
Clonning an Existing Repository
The most basic thing you should learn is how to clone an existing repository which may help you in every situation.
|
|
<repository-url>
is the URL of the repository you want to clone. In this way, you can download a repository to your local machine. Even more, you can make changes and upload them to the repository, or maybe update it more easily. We will learn how to use these concepts later on.
Creating a New Repository
Well since you have learned how to clone a repository, you may want to create your own repository to publish, or maybe maintain. Here is how you can do it:
|
|
In this way, you can initialize a new git repository on your current working directory. After that you can add files, check for differences, commit changes, and push them to your repository online. Let’s learn how to connect some repository from GitHub to this local repository.
Connecting Local Repository to GitHub
Now, you can initiliaze a new repository on your local machine, and you may want to connect it to a repository on GitHub. Here is how you can do it:
|
|
<repository-url>
is the URL of the repository you want to connect. Remember that, you have to connect your GitHub account for this to work. After this command, you can push your changes to the repository on GitHub. Let’s learn how to push changes to the repository on GitHub.
Now since you have created a repository and then connected it to GitHub, let’s learn how to add your changes. There is some sort of process to do that. Let’s learn how to do it.
Adding Changes to Cache
As a first move, you must add necessary changes per change topic. Like you may have changed “README.md” file, and you want to add it to the cache. Here is how you can do it:
|
|
Or you can just add all files using below command:
|
|
After you add the files you want to upload (I refer commit as upload until I mention it explicitly) you will need to commit these changes. Let’s learn how to do it.
Committing Changes
Committing changes is basically uploading diff files that indicates differences on changed files. You can just upload some sort of initial commit (creating new repository for example) and then upload diff files which will allow git to find the last possible structure of the files. Here is how you can commit changes:
|
|
<commit-message>
is the message you want to add to the commit. It is usually a short description of the changes you made, there are different conventions but if not specified, please use Conventional Commits. After you commit changes, you can push them to the repository on GitHub. Let’s learn how to do it.
After all your adds for specific changes, you can commit them to the repository. This will allow you to track changes in the future. After committing changes, you can push them to the repository on GitHub. Let’s learn how to do it.
Pushing Changes to GitHub
It is quite trivial to push changes to the repository on GitHub. Here is how you can do it:
|
|
<branch-name>
is the name of the branch you want to push changes to. It is usually main
or master
branch. After you push changes to the repository on GitHub, you can see the changes on the repository page. This is how you can manage your codebase using Git and GitHub.
Well, now you know some basic usage of the git and GitHub. I will add two example usage to it, and then we can finish the article.
Example Usages
Example 1: Cloning a Report Template from GitHub
In this situation, you most likely to want clone some repo, and then arrange necessary files for usage. For this example, I will clone my own report template for PHYS107&108 courses, you can access it with link. Now, we will clone it to our ~/Downloads/gitfiles/ folder, so that it can be easily accessible all the times. Let’s do it:
|
|
Well, after this, all things are trivial to your needs. In this repository, I recommend copying the main.tex
file to another folder, possibly called EXP8
under PHYS108
, and then changing the stuff as you wish.
Example 2: Creating a Basic Script and Uploading it to a GitHub Repository
Well, in this situation, I will explain how I made rofi-search-script. Let’s gooo:
|
|
Example 3: Creating a Website with Hugo and Deploying it with GitHub Actions
Well, in this situation, I will explain how I made my website. However, this uses some nonbasic concepts, so its time will come AS an individual article. So, let’s skip this for now.