Step-by-step: How to Start with Git and Create a Repository in GitHub
What are the advantages of Git
This tool is inescapable for worldwide developers. Here are a few advantages of using Git:
- No more copies: when you finish your work on your application, you can use the git push command to save your project online.
- Don’t break your code anymore: if it happens, you will return to the previous version by typing one command. It’s a massive time saver!
- Working with your collaborators is more straightforward: imagine a world where sharing code is simple… welcome to Git!
- You can afford to forget what you did: One command only is mandatory to check your changes.
Suppose you don’t use Git at the moment. Believe me, this tool will soon be one of your main. One more example; Git allows you to configure services to deploy your code automatically.
Ready to dive in?
Learn how to create Git account
Now that I introduced the advantages of Git to you, it’s practice time!
After this exercise, you will be able to create and manage your projects with Git and GitHub.
Note: I chose GitHub as a hosting service for Git because it's the most used in the world. Don't be afraid; the procedure is almost the same on other services.
To create your account, you need to:
- connect on GitHub homepage
- fill in the registration form
Congrats! 🎉 You are officially a new member of GitHub!
Install Git on your computer
The next step is to install Git on your computer. There are different Git software, but it’s better to install the basic one to start. In this tutorial, you will learn how to create repository in GitHub using git bash (or command lines).
Once you are comfortable with the command line, you can download a Git software with a user interface (like GitHub desktop, Fork, SourceTree, etc.).
I will talk briefly about the installation because it’s specific to your platform. Check here for further details on how to install Git on your computer.
First, update your packages:
sudo apt update
Next, install Git with apt-get:
sudo apt-get install git
Finally, verify that Git is installed correctly:
First, download the latest Git for Mac installer.
Next, follow instructions on your screen.
Finally, open a terminal and verify that Git is installed correctly:
First, download the latest Git for Windows installer.
Next, follow instructions on your screen (you can leave the default options).
Finally, open a terminal (example: powershell or git bash) and verify that Git is installed correctly:
Setup Git config
Once you finish the Git installation, you need to change your username and email. It will be helpful when you save your work. To do that, we will use the
git config command.
git config --global user.name "Gaël Thomas" git config --global user.email "email@example.com"
How to create a GitHub repository
Depending on the platform, the installation of Git isn’t simple.
Be proud, you’ve done the hard part! Now, we will create a file and save our code online.
Create a new repository in GitHub
You can return to the main GitHub page and click on the “+” icon in the navigation bar.
Once you click on this button, a sub-menu appears with a “New repository” button. You can click on it.
The create a new repository page will appear. Find a cool name for your first repository and add a description. Once you did it, you can click on “Create repository”.
Note: For this tutorial article, please don't tick "Initialize this repository with a README". We will do it ourselves! 😎
Well done! Your first GitHub repository is created. If you want to see all your repositories, you need to click on your profile picture in the menu bar then on “Your repositories”.
Clone repository in GitHub
It’s time to add our first file to the repository. But, before doing that, you will need to get a copy of the repository on your computer.
To do that, you need to clone the repository on GitHub.
On the repository page, you should copy the “HTTPS” address.
When you copied the URL of the repository, come back to your terminal.
Move to the folder of your choice then we will git clone the GitHub repository (create a local copy of the git repository).
git clone [HTTPS ADDRESS]
This command will make a local copy of the repository hosted at the given address.
Now, your repository is on your computer. You can move in it by typing:
cd [NAME OF REPOSITORY]
Write the cover of your project
We will create a “README.md” in the folder you cloned in the previous step. To do that, you can use your terminal or your favorite text editor.
In this file, you can copy and paste this template and edit it. If you want, you can replace the text between the square brackets.
# 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! ☺
Note: "README.md" is a file that Git uses as a repository cover. It will understand the format (markdown) and display it on the repository homepage.
How to update GitHub repository
Here we are! This last step will show you how to add files to the repository with the command line.
It’s the same command to update a GitHub repository.
To do this, get back to your terminal. If you have closed it, come back to your project folder.
When you want to save your work, four steps are mandatory. These steps are: “status”, “add”, “commit”, and “push”.
Below, you will find a standard procedure to perform when you want to save your work.
Note: You should perform all these steps within the repository.
- Git status command: help you check your project status when you finish your work. You can type the following command to make the list of changes appears:
- Git add command: allow you to select the files to save. You can use the git status command output to know modified files. Then, you have to choose the files you want to upload with the following command:
git add [filename] [filename] [...]
In our case, we will add the “README.md” because we want to save it.
git add README.md
Note: If you type the git status command again, the "README.md" will appear now in green. It means that we have added the file correctly.
- Git commit command: allow you to add a message to explain what you did. This message may be helpful if we want to check the change history (git log command).
git commit -m "Add README.md to describe the project folder"
- Git push command: send your work to the GitHub platform. With the following command, you can persist your commit to Git:
git push origin master
In the previous command, the word “master” means the main branch of the repository. This branch is the default one.
Git uses a branch system to help you divide your work better and work in a team.
The Git branch command is an advanced one, and we will not talk further about it in this tutorial. If you’re a beginner with Git, I recommend you take the time to be more comfortable before going further.
You did it; you update your GitHub repository! If you return to your repository page on GitHub, you will see the “README.md” in the file list.
Below, the “README.md” appears as a cover of your project!
Now that you know how to create a repository, you can learn how to delete repository in GitHub right here!
Useful Git Commands
Here is a list of commands to go a bit further in this step-to-step guide.
- Git log command
Display the history of commits (all modifications made on the project):
- Git checkout command
Revert all your changes since the last commit:
git checkout .
Revert all changes on a specific file since the last commit:
git checkout [FILENAME]
- Git diff command
Display the last changes on a file since the previous commit:
git diff [FILENAME]
- Git clean command
Remove all unexpected files in your project (not committed):
git clean -dfx
- Git commit command
Add a message to the files you added:
git commit -m [MESSAGE]
Add all files and make a commit at the same time:
git commit -am [MESSAGE]
Thanks for reading. Let’s connect!
➡️ I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more. 🚀🎒