Skip to main content

Uploading to GitHub and Gitea using Terminal

This guide explains how to upload files to GitHub and Gitea repositories using the terminal, step by step. We'll also discuss the purpose of each command to help you understand what’s happening at every stage. Since GitHub no longer supports password authentication, we'll use a Personal Access Token (PAT) for GitHub uploads.


Step-by-Step Instructions

1. Navigate to the Project Directory

Use the cd command to go to the directory where your project files are stored.

┌──(ryan㉿opslinuxsec)-[~]
└─$ cd ~/for_tutor
  • Purpose:
    This command moves you into the directory where your project files are located.

2. Initialize a Git Repository

Run the git init command to create a new Git repository in the current directory.

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git init
  • Purpose:
    Initializes an empty Git repository in your project folder. This allows Git to start tracking changes in your files.

3. Create and Switch to a New Branch

Create a new branch called main using git checkout -b and switch to it.

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git checkout -b main
  • Purpose:
    This command creates a new branch named main and switches to it immediately. By default, Git creates a branch named master, but it's now a common practice to use main instead.

4. Add Files to the Staging Area

Use git add . to add all files in the current directory to the staging area.

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git add .
  • Purpose:
    This command stages all files in the current directory, preparing them for a commit. The dot (.) means "add everything in this directory."

5. Commit the Changes

Commit the staged changes with a descriptive message using git commit.

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git commit -m "first commit"
  • Purpose:
    This command records the changes you’ve staged with a message. In this case, the message is "first commit". The commit serves as a "snapshot" of your project at that point in time.

6. Add a Remote Repository

For Gitea:

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git remote add origin https://git-v2.local.opslinuxsec.com/ryan/tester.git

For GitHub:

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git remote add origin https://github.com/your-username/your-repo-name.git
  • Purpose:
    The git remote add origin command links your local Git repository to a remote repository (on GitHub or Gitea).
    • origin is the default name for the remote repository.
    • The URL specifies the location of the remote repository.

7. Push Changes to the Remote Repository

For Gitea (Username & Password Authentication):

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git push -u origin main

When prompted, enter your username and password.

For GitHub (Token Authentication):

Since GitHub no longer supports password authentication, use a Personal Access Token (PAT) instead of a password. Here’s how to push:

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git push -u origin main

When prompted for the password, enter your Personal Access Token (PAT).

  • Purpose:
    The git push -u origin main command uploads the local branch main to the remote repository named origin.
    • The -u flag sets origin/main as the upstream branch, meaning future pushes can be done with just git push.

Summary of Commands and Their Purposes

CommandPurpose
git initInitializes an empty Git repository in the current directory.
git checkout -b mainCreates and switches to a new branch named main.
git add .Stages all files in the current directory for the next commit.
git commit -m "message"Records the staged changes with a descriptive message.
git remote add origin <URL>Links the local repository to a remote repository.
git push -u origin mainUploads the local main branch to the remote repository and sets it as upstream.

Important Notes

  1. GitHub Authentication:
    Since GitHub no longer allows password authentication, you must use a Personal Access Token (PAT). You can generate a PAT from GitHub Developer Settings. Ensure that the token has the required scopes (e.g., repo for full access to private and public repositories).

  2. Gitea Authentication:
    Gitea still supports authentication using a username and password, so you can use your regular credentials when prompted.


Example Workflow Output

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ cd ~/for_tutor

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git init
Initialized empty Git repository in /home/ryan/for_tutor/.git/

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git checkout -b main
Switched to a new branch 'main'

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git add .

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git commit -m "first commit"
[main (root-commit) 148efbc] first commit
1 file changed, 1 insertion(+)
create mode 100644 1.txt

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git remote add origin https://github.com/your-username/your-repo-name.git

┌──(ryan㉿opslinuxsec)-[~/for_tutor]
└─$ git push -u origin main
Username for 'https://github.com': your-username
Password for 'https://[email protected]': <Your PAT>
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 223 bytes | 223.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/your-username/your-repo-name.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

Conclusion

This guide covers the process of uploading files to GitHub and Gitea repositories using the terminal. Make sure to use a Personal Access Token (PAT) for GitHub, while Gitea still supports username and password authentication.