"Mastering Git: A Step-by-Step Guide to Creating and Pushing a GitHub Repository via the Terminal"

Photo by Joan Gamell on Unsplash

"Mastering Git: A Step-by-Step Guide to Creating and Pushing a GitHub Repository via the Terminal"

"Embrace the Terminal: A Comprehensive Guide to Creating and Pushing GitHub Repositories Without Ever Leaving the Command Line"

In my dream of owning an estate, I will name it "The Terminal". It would encompass everything a city needs to survive and flourish and I will never have to leave it.

~Patrick Okafor

Who Should Read this Article?

This article is intended for individuals who are comfortable working with the command line, proficient in remote repository operations, familiar with shell navigation, and capable of managing files and directories via the terminal.

By the end of this article, you will acquire the skills to create, manipulate, and push files to GitHub via the terminal. This knowledge will empower you to efficiently handle GitHub operations directly through the command-line interface, enhancing your productivity and versatility in managing repositories.

Introduction

Although graphical file managers or Graphic User Interfaces (GUIs) can handle common file manipulations, utilizing command-line (terminal) tools offers increased power and flexibility. Before delving into repository creation and pushing, it is crucial to review the fundamental concepts of:

  1. The terminal

  2. Git

  3. GitHub and GitHub CLI

Understanding these elements is vital, as they synergize to enable efficient repository pushing via the terminal. By familiarizing ourselves with these critical components, we can harness their capabilities to enhance our workflow and productivity in repository management.

The Terminal

The terminal serves as an interface for receiving commands from the keyboard and transmitting them to the operating system for processing. A wide array of commands can be issued to the system for execution, encompassing tasks such as:

  1. cd: Allows the user to change the current working directory.

  2. ls: Provides a listing of files and directories present in the current directory.

  3. mkdir: Enables the creation of a new directory within the file system.

These commands represent a subset of the many available options users can utilize to interact with the operating system through the terminal.

GIT

Git is a version control system that is used to record every change in source code during software development. Git is a tool that helps developers work together on projects. It keeps a record of all the changes made to the code, making it easy for teams to work on the same code at the same time without any issues. It ensures that everyone is on the same page and can collaborate effectively. Some key features of git based on this article include

  • Distribution: Git is a distributed version control system means that every developer has a full copy of the project's history and can work on it even when not connected to the internet. This decentralization makes it more flexible and provides extra backup options.

  • Commit and History: Developers use Git to make commits, which are snapshots of the project at a specific point in time. These commits create a detailed history of changes made to the codebase, making it easy to track the development progress and understand why specific changes were made.

Some common Git commands include:

  1. git init: Initializes a new Git repository in a directory.

  2. git add: Adds changes or new files to the staging area.

  3. git commit: Records the changes in the repository's history with a descriptive message.

  4. git push: Sends local commits to a remote repository.

  5. git pull: Fetches and merges change from a remote repository into the current branch.

  6. git clone: Creates a copy of a remote repository on the local machine.

  7. git branch: Lists, creates or deletes branches.

  8. git merge: Integrates changes from one branch into another.

  9. git status: Shows the current status of the repository.

These commands are utilized in an alternating manner to ensure that, upon completion of development, the files are appropriately configured and prepared for remote deployment. By employing these commands as part of the development process, the files become well-organized and suitable for remote deployment when the project reaches its final stages.

GitHub CLI

GitHub CLI (Command Line Interface) is a powerful tool offered by GitHub, enabling developers to seamlessly interact with GitHub repositories and execute a diverse range of Git and GitHub-related actions directly from the terminal or command prompt. Through this interface, developers can efficiently manage their GitHub workflows, leveraging the convenience and flexibility of the command-line environment for their repository operations.

Github CLI combines the features of Git and GitHub. It aims to streamline the workflow for developers who prefer to work from the command line, providing them with a more efficient and seamless experience when interacting with GitHub repositories.

GETTING STARTED

  • Step one: Download GitHub CLI: Visit the web page of the official GitHub CLI and download the latest release. Here is a link to download the latest version of GitHub CLI.

  • Step two: Installation:

    • Windows:

      • After downloading the Windows executable, Open the file and run the installer and follow the on-screen instructions.
    • macOS:

      • If you downloaded the macOS binary, move it to the desired installation location (e.g., /usr/local/bin/) and make it executable. You can use the Terminal to do this:

          codemv ~/Downloads/gh_* /usr/local/bin/gh
          chmod +x /usr/local/bin/gh
        
    • Linux:

      • For Linux (Debian/Ubuntu-based distributions:), you can use either a .deb or .rpm package, depending on your distribution. Download the appropriate package and install it using your package manager.

          sudo dpkg -i /path/to/gh_*_linux_amd64.deb
        

Step 3: Authentication: After installation, you need to authenticate with your GitHub account. Open the terminal and run the following command:

    gh auth login

This will guide you through the authentication process using your web browser. Follow the prompts to log in to your GitHub account and grant GitHub CLI the necessary permissions.

Step 4: Verify Installation: To ensure that GitHub CLI is installed and working correctly, run the following command:

    gh --version

This should display the version number of the installed GitHub CLI.

Start Using GitHub CLI to create a repository and push it to GitHub from the command line.

With GitHub CLI installed and authenticated, you can start using it to interact with GitHub repositories and perform various actions from the command line.

Assuming the completion of work on an E-commerce website, we can proceed with creating the repository from the command line using the following command:

    gh repo create <repo-name> --public

For instance, if the desired repository name is "E-commerce," the command will be:

    gh repo create E-commerce --public

The addition of the --public flag while creating the repository instructs Git to set its visibility as public. As a result, the repository becomes accessible to a broader community of developers and users. This streamlined command expedites the process of repository creation, ensuring that the E-commerce project is promptly made available for collaborative development and public viewing. This accessibility fosters collaboration, enables open-source contributions, and allows developers worldwide to engage with the E-commerce project, promoting growth and innovation within the community.

If you want it private you must use:

    gh repo create E-commerce --private
  1. Initialize the Git repository:

    • Use the command git init to initialize a new Git repository. This sets up the repository and enables version control for the project.
  2. Add the repository to the Staging area:

    • With the command git add, we add all the files in the current directory to the repository's Staging area. These files are now ready to be included in the next commit.
  3. Commit the repository:

    • The command git commit -m "Initial commit" is used to commit the staged changes to the repository with a descriptive message. It is advisable to use a meaningful and structured commit message, such as "Initial commit," as it proves beneficial during code reviews, collaborations, backtracking, and debugging. A well-crafted commit message provides valuable insights into the changes made and helps maintain a clear and organized history of the project's development.
  • Establish the connection between the remote Repository and the Local repository:

    The command git remote add origin https://github.com/userName/E-commerce.git is used to add the remote repository to your local Git repository. Here's the meaning of each part of the command:

    • git: This is the command to invoke Git, the version control system.

    • remote: This is a subcommand in Git used to manage remote repositories (repositories hosted on remote servers like GitHub, GitLab, or Bitbucket).

    • add: This is an option of the remote subcommand that specifies that we want to add a new remote repository to the local repository.

    • origin: This is the name given to the remote repository. "Origin" is a standard convention for the primary remote repository, but you can choose a different name if you prefer.

    • https://github.com/username/E-commerce.git: This is the URL of the remote repository you want to add. Replace "username" with your actual GitHub username and "E-commerce. git" with the name of the GitHub repository you want to connect to.

💡
Note: The name "origin" is used to refer to this remote repository, so you can later use "origin" as a shorthand to reference this remote repository in various Git commands, like pushing and pulling changes.

Upload the local branch to the remote repository:
The command git push -u origin main is typically used when you are starting a new project or setting up version control for the first time. By pushing the "main" branch with the -u option, you establish the upstream tracking relationship, making it more convenient to push and pull changes between your local and remote repositories in the future.

When you run git push -u origin main, Git will transfer your local branch "main" and its corresponding commits, if any, to the "main" branch in the remote repository named "origin." The -u option sets up the tracking relationship between your local "main" branch and the remote "main" branch, so in the future, when you run git push without any additional arguments, Git will know to push to the "origin/main" branch by default.

Summary

  • Although, these can be done with Graphic Interface. The command line provides additional power and flexibility.

  • The GitHub CLI is an OpenSource project and new features are being added and older ones might be gotten rid of. To keep in touch with bug fixes and updates, Always check their official documentation.

  • GitHub CLI website documentation

  • Learn More about the Terminal

  • Learn about Git

To create a repository and push from the command line.

1. Create a new repository on GitHub: gh repo create <repo-name> --public
 (replace <repo-name> with your desired repository name)

2. git init: Initializes a new Git repository
3. git add : Adds all the files in the current directory to the repository
4. git commit -m "Initial commit": Commits the changes with a message

5. git branch -M main(optional)
6. git remote add origin https://github.com/Patoski-patoski/Dynamic-C-calculator.git
7. git push -u origin main