Computer scienceSystem administration and DevOpsGitHub

Publishing project to github via Git console

2 minutes read

GitHub is a vital platform for software development, offering tools for version control and collaboration. It helps developers efficiently manage and share their code. The Git console, a command-line tool, plays a key role in these tasks. It lets developers execute commands to manage changes, add files, commit updates, and push them to online repositories. This ensures projects are up-to-date and securely stored online.

In this topic, we'll walk you through the process of using the Git console application to get your projects up and running on GitHub. Starting with how to add files to your local repository, we'll guide you through to the final step of sharing your repository with the wider community.

Adding files via Git console application

Adding files via the Git console application is a complicated method compared to the previous one. However, it may fit well into someone's workflow. First things first, you have to go back to the directory with the code file and open it in the terminal using cd <directory_path> or just select "Open In" ⇾ "Terminal" instead of "Explorer" in the IDE.

Then, you need to initialize the GitHub repository in this directory using the following command:

> git init
Initialized empty Git repository in <directory_of_your_project>

In the next step, you need to add files to the commit:

> git add app.py

In this case, it is just a single file — app.py. But in most cases, you will come across programs consisting of several files. For multiple files, you should commit them all by listing each file separated by spaces. Or you can add the entire directory containing the files with the code at once. It will most certainly be easy to add the entire directory at once.

> git add <file1> <file2> ... <fileN>

Or:

> git add <directory>/

You can check if the correct file is added to the commit using git status command:

> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   app.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test/
        tests.py

As you can see, only the app.py file is subsequently added to the repository. The test files are left behind.

Now it's time to add changes to the commit using git commit command and specify the name of the commit after the -m flag:

> git commit -m "init: app.py add"
[master (root-commit) 5ad0d23] init: app.py add
 1 file changed, 109 insertions(+)
 create mode 100644 app.py

When creating a local repository as of 2020, GitHub changed the name of the main branch to main but the master branch is still the main branch. So you should change the main branch to main before you connect the local repository to a remote repository. In the penultimate step, you need to change the main branch and connect the remote repository using these commands:

> git branch -M main
> git remote add origin [email protected]:<username>/<repository>.git

Do not forget to put your GitHub username and repository name within this command.

And the final step is to push your file to the remote repository.

> git push -u origin main

GitHub will successfully add the file to the repository, and you can see it directly on GitHub.

Share the repository

You surely have a great project that you are proud of and probably want to share with your friends.

The easiest way to share is to copy the repository link from your browser and send it to a friend. Your friends can use git clone to download all the files from your repository to their computers:

> git clone <repository_link>

Your friend may ask how they can run this program. In this case, it is important to remember, however, that a repository only stores the source code of a program and not the executable, which you can simply take and run. To run the program, your friend will need to install the necessary compilers for the programming language your program is written in. In some cases, they might have to install additional libraries. Or you can create an executable file by yourself and send it to your friend instead of the repository with the code.

Conclusion

To wrap up, this guide has shown you the essentials of using the Git console to add files and share your GitHub repository. While the steps may seem detailed at first, they provide a solid foundation for managing and distributing your projects. By following these instructions, you can efficiently handle your code on GitHub, making it easier for others to access and contribute to your work. This skill is invaluable for any developer aiming for effective project collaboration and version control.

4 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo