How to Use Git and GitHub via Command Line?

Gevin Nanayakkara
5 min readMar 24, 2023

--

A Guide to Using Command Prompts for GitHub.

Are you new to Git and GitHub, and wondering how to use them via the command line? Look no further! In this article, we’ll explore the basic Git commands that you can use in your command prompt or terminal to interact with your GitHub repository. Whether you’re a beginner or an experienced developer, understanding these commands will enable you to clone, pull, push, and manage branches in your repository with ease. So, fire up your command prompt or terminal, and let’s dive into the world of Git and GitHub!

Login to GitHub

To log in to GitHub, follow these steps:

  1. Open your command prompt or terminal on your computer.

2. Type the following commands and press Enter:

git config — global user.name “Your Username”
(Replace “Your Username” with your GitHub username.)

git config — global user.email youremail@example.com
(Replace “youremail@example.com” with the email address associated with your GitHub account.)

git config — global credential.helper cache
(This command will save your credentials in the cache for a short period of time so that you don’t have to enter your username and password every time you push to or pull from GitHub.)

git config — global credential.helper ‘cache — timeout=3600’
(This command will cache your credentials for an hour, so you won’t have to enter them repeatedly during that time)

git clone https://github.com/username/repo.git
(Replace “username” with your GitHub username and “repo” with the name of the repository you want to clone.)

Type your GitHub username and password when prompted.

Checking if You’re Logged in to GitHub via Command Line

To check if you’re logged in to GitHub using the command line, simply use the following command:
git config user.name
(This command will display the username that is associated with the Git configuration on your machine. If you have previously configured your username for GitHub, it will be displayed in the output. If it is blank or displays an error message, it means that you are not logged in to GitHub from the command line.)

You can also use the following command to check if you have an active session with GitHub:
curl -i https://api.github.com/user
(This command will send a GET request to the GitHub API to retrieve information about your user account. If you are not logged in, you will receive an authentication error message. If you are logged in, you will receive a response with your user information.)

Upload the project to GitHub using the command line interface (CLI)

You can upload your project to GitHub using the command line interface (CLI) with the following steps:

1. Create a new repository on GitHub.com. Do not initialize it with a README file or .gitignore file.

2. Open your command prompt or terminal on your computer.

3. Change the current working directory to your project directory using the ‘cd’ command. For example, if your project is located in ‘C:\Users\YourName\ProjectFolder’, type the following command and press Enter:
cd C:\Users\YourName\ProjectFolder

4. Initialize a new Git repository in your project directory using the ‘git init’ command. For example:
git init

5. Add all the files in your project directory to the staging area using the ‘git add’ command. For example:
git add.
(This will add all files in your project directory to the staging area.)

6. Commit the changes to the local repository using the ‘git commit’ command. For example:
git commit -m “Initial commit”
(Replace “Initial commit” with a descriptive message for your commit.)

7. Add the remote repository URL using the ‘git remote add’ command. For example:
git remote add origin https://github.com/username/repo.git
(Replace “username” with your GitHub username and “repo” with the name of the repository you created in step 1.)

8. Push the changes to the remote repository using the ‘git push’ command. For example:
git push -u origin main
(This will push your local changes to the remote repository. If prompted, enter your GitHub username and password to authenticate the push.)

If you are working with Git branches in your project, you can upload your project to GitHub using the following steps:

1. Initialize a new Git repository:
git init

2. Add all the files in your project to the Git repository:
git add.

3. Commit the files to the Git repository:
git commit -m “initial commit”

4. Link your Git repository:
git remote add origin <GitHub repository URL>

5. Check the available branches in your local repository:
git branch

6. If the branch you want to switch to does not exist yet in your local repository, create a new branch:
git branch <branch name>

7. Switch to a new branch:
git checkout <branch name>

8. Push your project to GitHub:
git push -u origin <branch name>

Committing Changes to Your GitHub Branch via Command Line

To commit changes to your GitHub branch via the command line, follow these steps:

1. Check the current state of your repository:
git status

2. Stage your changes:
git add.

3. Commit your changes:
git commit -m “Commit message”

4. Push your changes:
git push origin <branch name>

5. If you want you can check the current state of your repository using:
git status

Cloning a Repository via Command Line

To clone a repository via the command line, follow these steps:

1. Open your command prompt or terminal on your computer.

2. Change the current working directory to where you want to store the cloned repository using the ‘cd’ command. For example, if you want to clone the repository into a new directory called ‘myproject’ in your ‘Documents’ folder, type the following command and press Enter:

cd Documents

mkdir myproject

cd myproject

(This will create a new directory called ‘myproject’ in your ‘Documents’ folder and navigate to it in your command prompt or terminal.)

3. Copy the URL of the repository you want to clone from GitHub. You can find the URL by navigating to the repository on GitHub and clicking the green “Code” button, then selecting “HTTPS” and copying the URL.

4. Clone the repository using the git clone command. For example, if the repository URL is ‘https://github.com/username/myrepository.git', type the following command and press Enter:

git clone https://github.com/username/myrepository.git
(This will clone the repository into a new directory called ‘myrepository’ in your current working directory.)

If the repository is private and requires authentication, you may be prompted to enter your GitHub username and password or a personal access token (PAT). Enter your credentials as prompted.

If you are working with Git branches, you can clone a Git repository using:
git clone -b <branch name> https://github.com/<username>/<repository name>.git

That’s it! With these basic steps, you can use Git and GitHub via the command line to manage your projects and collaborate with others.

--

--