If you have never used Git or GitHub before, you need to understand one of the most important tasks you will use with the service: How to push new projects to remote repositories.
GitHub is one of the most widely used software repositories for the Git Version Control system. With GitHub, you can create new repositories, share these repositories, and collaborate with teams on projects.
For each developer, one of the most important things you can do is interact with GitHub from the command line. By doing this, you can create an empty repository on GitHub and then connect to the local repository to that remote. With this setting, you can do everything you need from the local command line.
How do you do this? I’m glad you asked. What I’m going to do is guide you through the process of creating a new remote repository on GitHub and then creating a local repository on your desktop machine and connecting the two. This is a very basic Git / GitHub thing, so if you’ve accelerated how this works because there’s nothing for you here.
However, if you are new to Git / GitHub, keep reading, because this is something that you will use quite a lot in the future as a developer.
What do you need
The first thing you need is a GitHub account, so please register and register. You also need to install git on the platform of your choice. I will show with Ubuntu Server 18.04, but the operating system does not matter, as long as it supports Git.
How to make a new repository on GitHub
The first thing to do is create a new repository on GitHub. Log in to your GitHub account and go to the Dashboard. From that page, click the Repository tab. In this new window, click New (Figure A).
In the resulting window, provide the name of the project and an optional description (Figure B). Depending on what the project is used for, you might make it private.
Creating a new repository on GitHub.
Click Create Repository and GitHub will do that. The new repository address is:
Where USER is your GitHub username and PROJECTNAME is the name you give to your new project. You will need that little address.
How to connect your local project
Now the fun begins. On your local machine, create a new project folder with the command:
Change to the newly created directory with the command:
Initialize the repository with the command:
Now let’s create a readme file with the command:
Add a new file to the staging area with the command:
git add .
Now we will make our first commit. If you’re not sure what a commit is, it’s simple: Commit adds the latest changes to the source code to the newly created repository. This change will then become part of the repository head revision.
To make a commit, run the command:
git commit -m "added readme"
You can change the text in quotes to whatever you want, such as “my first commit.” Make sure the text in quotation marks describes what has been done to commit.
The next step will be to use the GitHub repository address. What we need to do is add a local repository to the origin (name of the remote repository where you want to publish your commit) from the remote repository. This is done with the command:
git remote add origin https://github.com/USER/PROJECTNAME.git
Where USER is your GitHub username and PROJECTNAME is the name you give to your new project.
At this point you can then push your work to the remote with the command:
git push -u origin master
When you run this command, you will be asked for your GitHub username, followed by your GitHub user password. After successful authentication, your local repository will be connected to the remote GitHub repository and the readme.txt file is pushed to the remote.
Let’s add another file and push it to the remote repository. Issue an order:
Add files to the staging area with the command:
git add .
Give a new commit with the command:
git commit -m "added license file"Push changes to the master branch of the GitHub repository with the command:
Push changes to the master branch of the GitHub repository with the command:
git push -u origin master
Now, if you look at the repository on GitHub, you will see the readme.txt and LICENSE files there (Figure C).
We have successfully pushed our file to a remote GitHub repository.
Congratulations, you just pushed a new project to GitHub from the command line. Yes, this is a very basic thing, but this is a task that you must understand when starting a career as a developer. Make sure you are experienced with Git and GitHub, so that your path to Nirvana developers is as clear as possible.