How To Create A Repository In Github
The very next step after creating an account on GitHub is to create a GitHub repository. Once you do, you can kickstart your GitHub journey, regardless of whether you want to start a new software project or take a “Docs as code” approach with your documentation on the platform.
This Nira guide will cover the basics of GitHub before giving you a step-by-step breakdown of how to create a GitHub repository and update it.
Let’s get started.
What Is GitHub?
GitHub is a free cloud-based platform that helps developers manage their code. It serves as a repository to store code and lets users monitor and control changes to all or some of their code (known as version control).
But we cannot talk about GitHub without talking about Git.
Git is a version control software responsible for managing changes to a project without overwriting any part of it and is a critical component of GitHub.
Suppose two co-workers are simultaneously updating pages on a website. What would happen if both co-workers unknowingly start working on the same page at the same time? Naturally, one of them will have all their work overwritten and erased.
Git prevents this from happening.
The co-workers can upload their respective revisions to the same page, after which Git will save them as two separate copies, thanks to its version control abilities. You can then merge these changes later to prevent overwriting any work.
Reverting to an earlier version, regardless of the time frame, is also possible. This is because Git keeps a snapshot of each and every change ever made to a project.
Why Create a GitHub Repository?
One of the biggest advantages of an open-source distributed revision control system like Git is it lets you easily track down the source of the problem and revert it to a working condition before the problematic code was pushed to salvage the project. In addition, it allows you to track who contributed to what and when which can come in handy when multiple people from different time zones are contributing to a specific project at the same time.
When you create a GitHub repo, you can bring all these benefits to your project. What’s more, you can make your repo open to the public so that other developers can contribute by either helping fix broken code or making corrections to typos.
The fact that you can release a working beta version ahead of schedule is another significant benefit.
2 Steps to Create a Repository in GitHub
Let’s look at the various steps you need to carry out before creating your very own repository on GitHub.
Step 1: Create a Git Account
Open the GitHub homepage and fill in the registration form to become a GitHub user.
Follow this up by installing Git on your computer. You’ll find different Git software, but we recommend installing the basic one if this is your first rendezvous with the platform. You can create a repository in GitHub using git bash (or command lines). Once you’re comfortable with a command line, download GitHub software with user interfaces like Fork, GitHub Desktop, and SourceTree.
Here’s how you install Git for different OS:
- Download the latest Git for Mac installer, and follow the prompts displayed on your screen
- Open the terminal and verify that Git is installed correctly:
$ git –version
- Download the latest Git for Windows installer, and follow the prompts on your screen. You can leave the default options if they meet your preferences.
- Open a terminal (example: git bash) and verify that Git is installed correctly:
$ git –version
- Update your packages:
$ sudo apt update
- Install Git with apt-get:
$ sudo apt-get install git
- Verify that Git is installed correctly:
$ git –version
Next, you’ll set up Git config after completing the Git installation.
To do this, change your username and email. This will be helpful when you save your work. We’ll use the git config command for this.
$ git config –global user.name “John Doe”
$ git config –global user.email “firstname.lastname@example.org”
Step 2: Create a New Repository on GitHub
After installing Git, return to the main GitHub page and click on the + icon in the navigation bar. A dropdown menu will appear on your screen.
From the dropdown menu, click on New Repository. In the Create a new repository page, type in the name for your first repository, followed by adding a description.
Next, select a repository visibility. Your options include Public (anyone can see your repository, but you’ll choose who can commit), Internal (only enterprise members can see your repository, but you’ll choose who can commit), or Private (you choose who can see and commit to your repository).
Next, click on the Initialize this repository with a README checkbox.
Once you’re done, click on Create Repository.
And that’s it! You’ve created your very first GitHub repository. If you want to see all your repositories, click on your profile picture in the menu bar > Your repositories.
Other Useful Things to Know About Your GitHub Repository
In this section, we’ll take a look at a few other things you should know about your GitHub repository.
Clone Repository in GitHub
After creating a repository in GitHub, next you’ll add your first file. But before you can do that, you’ll need a copy of the repository on your computer.
For this, you’ll have to clone the repository on GitHub. Then, on the repository page, copy the HTTPS address.
Return to the terminal after copying the URL of the repository. Move to the folder of your choice, after which you can git clone (create a local copy of) the GitHub repository.
$ git clone [HTTPS ADDRESS]
This command will make a local copy of the repository hosted at the specified address.
At this stage, your repository is finally on your computer. Move it by typing:
$ cd [NAME OF REPOSITORY]
Write the Cover of Your Project
Next, you need to create a README.md in the folder you cloned above.
Use your terminal or your favorite text editor. In this file, copy and paste the following template and edit it:
# My first Git repository
Hello, my name is [Your Name], and I’m learning Git. Here is my first GitHub repository.
[Insert a catchy sentence about you]
Thanks for reading!
You can also enter some text between the square brackets.
Remember, README.md is a file that Git will use as a cover for your repository. This means that the platform will understand the format (markdown) and display it on the repository homepage.
Update GitHub Repository
Lastly, you can add files to the repository with the command line. The command is the same to update a GitHub repository.
Return to the terminal. In case you have closed it, you can come back to your project folder.
Before you can save your work, you have to carry out four steps: “status“, “add“, “commit“, and “push“.
Keeping this in mind, you’ll have to perform a standard procedure to save your work:
- Git status command helps you check your project status when you complete your work. Add the following command to make the list of changes appear:
$ git status
- Git add command lets you select the files you want to save. Use the git status command output to learn more about the modified files. Next, select the files you want to upload with the following command:
$ git add [filename] [filename] […]
Note: As we want to add and save the README.md file, the command will be $ git add README.md.
- Git commit command allows you to add a message to explain the changes/modifications you made. This message will be helpful if you want to check the change history (git log command) at any time in the future.
$ git commit -m “Add README.md to describe the project folder”
- Git push command lets you send your work to the GitHub platform. Use the following command to persist your commit to Git:
$ git push origin master
Here, the word master means the main branch of the repository (this is also the default one).
3 Most Common Git Mistakes and Their Solutions
Let’s review the three most common Git errors and their solutions.
1. Discarding Changes to Local Files
Errors can happen unexpectedly. In our haste to solve them, we often make changes to our code that aren’t always optimal. That’s why knowing how to quickly undo any hasty changes you made is useful to avoid more serious mistakes.
Use the command “git checkout“ to reset the files to their original state:
# Reset directory “myCode”
git checkout – myCode
2. Removing Project File From Git Without Deleting It Completely
Adding a file to the staging area (git add) that doesn’t belong there is another common mistake.
While you can use the command “git rm” here, it will also remove the file from your file system. If you want to keep the file in the file system, you’ll have to remove the file from the staging area with “git reset <file name>“. Follow this up by adding the file to the .gitignore to avoid packing it back into the staging index mistakenly down the line. Here’s how to go about this:
git reset Dateiname
echo Dateiname >> .gitignore
3. Undoing Local Commits
It’s easy to create a commit containing errors, especially when you’re working on building many of them. Luckily, there is an easy solution here.
To undo one or more commits, use the “git reset”command. The command knows three different modes (soft, hard, mixed), and here’s how it’ll look:
# Undo the last four commits, keep changes
git reset HEAD ~ 4
# Undo the last four commits, discard changes
git reset –hard HEAD ~ 4
We also want to point out that you should use the “hard“ mode cautiously. As soon as you execute “git reset — hard“, the working tree and the index will reset automatically, meaning all changes will be lost forever.