After a short introduction to the advantages of Git, you will learn how to use it. In the end, you will be able to create a GitHub repository and update it.
This tool is inescapable for worldwide developers. Here are a few advantages of using Git:
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?
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:
Congrats! 🎉 You are officially a new member of GitHub!
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:
$ git --version
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:
$ git --version
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:
$ git --version
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 protected]"
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.
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".
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]
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.
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
$ 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 -m "Add README.md to describe the project folder"
$ 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!
Here is a list of commands to go a bit further in this step-to-step guide.
Display the history of commits (all modifications made on the project):
$ git log
Revert all your changes since the last commit:
$ git checkout .
Revert all changes on a specific file since the last commit:
$ git checkout [FILENAME]
Display the last changes on a file since the previous commit:
$ git diff [FILENAME]
Remove all unexpected files in your project (not committed):
$ git clean -dfx
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]
➡️ If you want more content like this, you can follow me on Twitter, where I tweet about web development, self-improvement, and my journey as a fullstack developer!
Subscribe to the newsletter.
Receive the latest articles, tips, tutorials, and exclusive content (1.700+ learners are already part of the community).