How to Use GitHub on Ubuntu: Complete Beginner-to-Advanced Guide with Git, SSH, and VS Code

目次

1. Introduction

Why Connect Ubuntu with GitHub?

In recent years, the use of Git and GitHub has become standard practice in software development and system operations. For engineers and programmers working in Linux environments such as Ubuntu, integrating with GitHub is part of everyday workflow.

Git is a tool for managing source code version history and is essential for efficient collaboration among multiple developers. GitHub, on the other hand, is a hosting service that allows Git-managed repositories to be shared and published online, serving as a global hub for developers to exchange code.

Why Use GitHub on Ubuntu?

Ubuntu is a Linux distribution highly popular among developers and is known for its excellent compatibility with open-source development. It provides an environment where Git and other development tools can be easily installed, making GitHub integration smooth and efficient.

Typical use cases for combining Ubuntu and GitHub include:

  • Managing and sharing Python, C++, and other source code on GitHub
  • Contributing to open-source projects
  • Publishing your work as a portfolio

What You Will Learn in This Article

This guide explains everything from basic to advanced GitHub usage on Ubuntu through the following steps:

  • How to install Git on Ubuntu
  • Initial Git configuration and SSH setup
  • Creating and managing GitHub repositories
  • Efficient development using Visual Studio Code
  • Common troubleshooting tips and FAQs

Even first-time GitHub users can follow along safely, with real command examples and important notes included. If you want to make the most of GitHub on Ubuntu, read on until the end.

2. Installing Git on Ubuntu

What Is Git? A Quick Refresher

Git is a Version Control System (VCS) used in software development to manage change history of source code. It allows developers to restore previous states and collaborate simultaneously.

One major advantage of Linux environments like Ubuntu is how easily Git can be installed. Using the method below, installation can be completed within minutes via the terminal.

Installing Git via APT

The most common way to install Git on Ubuntu is by using APT (Advanced Package Tool). Follow these steps:

1. Update the Package List

sudo apt update

This command retrieves the latest package information. Always run it before installing new software.

2. Install Git

sudo apt install git

When prompted with “Y/n”, type y and press Enter to start the installation.

Verify the Git Installation

After installation, confirm that Git is installed correctly:

git --version

If you see output like the following, Git is installed successfully:

git version 2.34.1

The version number may vary depending on your Ubuntu release, but any version output confirms success.

Installing via Snap (Optional)

Although Git can also be installed using the snap command, APT is generally more stable and widely used. Unless you have a specific reason, APT is recommended.

3. Initial Git Configuration

Essential Setup Before Using Git

Once Git is installed, the next step is configuring user information and authentication. Proper configuration ensures smooth interaction with remote repositories and team workflows.

Setting Git Username and Email

Git records who made each commit. Therefore, you must configure a username and email address.

Configuration Commands

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

These values do not need to match your GitHub account exactly, but using the same email as GitHub helps associate commits correctly.

Verify Configuration

git config --list

This command displays your current Git configuration.

Generating and Registering SSH Keys

Although HTTPS is supported, SSH authentication eliminates repeated password prompts and provides a more secure and efficient workflow.

1. Generate an SSH Key

ssh-keygen -t ed25519 -C "you@example.com"

Press Enter several times to generate a private key (~/.ssh/id_ed25519) and a public key (~/.ssh/id_ed25519.pub).

2. Start the SSH Agent and Add the Key

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

The SSH key is now ready for use.

3. Add the Public Key to GitHub

Copy the public key:

cat ~/.ssh/id_ed25519.pub

Then register it on GitHub:

  1. Log in to GitHub
  2. Click your profile image → Settings
  3. Select “SSH and GPG keys”
  4. Click “New SSH key”, paste the key, and save

4. Test the SSH Connection

ssh -T git@github.com

Type yes when prompted to trust the host.

If successful, you will see:

Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.

Recommended Next Steps

With this setup complete, you can now clone, push, and pull GitHub repositories. The next section explains how to create and operate GitHub repositories in practice.

4. Working with GitHub Repositories

Creating a New Repository on GitHub

After logging in to GitHub, start by creating a new repository.

Steps (via Web Browser)

  1. Click the “+” button in the top-right corner of the GitHub homepage and select “New repository”.
  2. Enter the following information:
  • Repository name: Example: my-first-repo
  • Description (optional): A brief explanation
  • Public / Private: Choose repository visibility
  1. Click “Create repository”.

After creation, the repository URL will be displayed. You will use this URL to clone and manage the repository.

Cloning an Existing GitHub Repository

To copy a repository to your local Ubuntu environment, use the git clone command.

git clone git@github.com:your-username/my-first-repo.git

This example uses SSH. If you use HTTPS, the URL will be different.

After execution, a directory named my-first-repo will be created containing the repository files.

File Changes, Staging, and Commit Workflow

1. Add or Edit Files

For example, create a new file:

echo "# My First GitHub Project" > README.md

2. Stage the Changes

git add README.md

Staging selects which changes will be included in the next commit.

3. Commit the Changes

git commit -m "Initial commit: add README.md"

Your changes are now saved in the local repository history.

Pushing Changes to GitHub

To reflect local changes on the remote GitHub repository, use git push.

git push origin main

If your default branch is not main (for example, master), adjust the branch name accordingly.

Pulling Remote Changes to Local

If another developer has made changes, use the following command to update your local repository:

git pull origin main

This merges remote changes into your local branch.

Common Additional Operations

  • Check remote repositories:
git remote -v
  • Add another GitHub repository later:
git remote add origin git@github.com:your-username/another-repo.git
  • Editing README.md or .gitignore follows the same workflow.

5. Integrating with Visual Studio Code

Why VS Code Is Ideal for Git Operations

When using GitHub on Ubuntu, combining it with Visual Studio Code (VS Code) significantly improves productivity. VS Code is an open-source editor developed by Microsoft and is known for its excellent Git integration. It allows commits, pushes, and diff reviews via a GUI, making it beginner-friendly.

Installing VS Code on Ubuntu

You can easily install VS Code on Ubuntu using the following steps.

1. Add the Microsoft Repository

sudo apt update
sudo apt install wget gpg
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /usr/share/keyrings/

2. Register the Repository

sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

3. Install VS Code

sudo apt update
sudo apt install code

After installation, you can launch VS Code by typing code in the terminal.

Built-in Git Support

VS Code includes Git integration by default, so no additional plugins are required for basic Git operations. For advanced usage, the following extensions are recommended:

  • GitHub Pull Requests and Issues
    Enables GUI-based Pull Request handling and Issue management.
  • GitLens
    Visualizes line-by-line history, showing who changed what and when.

Basic Git Operations in VS Code

Opening a Repository

Navigate to your cloned repository and run:

code .

Reviewing Changes and Committing

  • Click the Source Control icon in the left sidebar
  • View the list of modified files
  • Select a file to see diffs
  • Enter a commit message and click the ✓ icon

Push and Pull Operations

  • Select “Push” or “Pull” from the “…” menu
  • Or use the status bar at the bottom right

Using the Integrated Terminal

The built-in terminal (Ctrl + `) allows seamless switching between GUI and CLI workflows. For example, you can commit via GUI and manage branches via the terminal.

Troubleshooting Tips

  • If SSH errors occur, verify that VS Code references the correct SSH key
  • You may need to reconfigure authentication or use a GitHub Personal Access Token (PAT)

6. Common Troubleshooting

SSH Error: “Permission denied (publickey).”

Possible Causes

  • SSH keys were not generated correctly
  • The public key is not registered on GitHub
  • The SSH agent is not loading the key

Solutions

  1. Check for an existing SSH key:
ls ~/.ssh/id_ed25519.pub

If it does not exist, generate one:

ssh-keygen -t ed25519 -C "your_email@example.com"
  1. Start the SSH agent and add the key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
  1. Add the public key to GitHub:
cat ~/.ssh/id_ed25519.pub

Add it under “SSH and GPG keys” on GitHub.

  1. Test the connection:
ssh -T git@github.com

Authentication Error with HTTPS

Cause

  • Password authentication has been deprecated; token-based authentication is required

Solution

  1. Create a Personal Access Token on GitHub
    https://github.com/settings/tokens
    Check repo scope when creating the token
  2. Use your GitHub username and the token string as the password
  3. Enable credential caching if desired:
git config --global credential.helper cache

Error: “fatal: not a git repository”

Cause

  • The current directory is not a Git repository

Solution

  • Move into an existing repository directory:
cd ~/your-project-directory
  • Or initialize a new repository:
git init

Handling Merge Conflicts

Cause

  • Conflicts occur when multiple users edit the same section of a file simultaneously

Solution

  1. Open the conflicting file after a git pull error
  2. Look for conflict markers:
<<<<<<< HEAD
Your changes
=======
Other changes
>>>>>>> origin/main
  1. Edit the content, remove the markers, and save
  2. Then run:
git add .
git commit

Error: “remote: Repository not found.”

Cause

  • Incorrect repository URL
  • No access permission to the repository

Solution

  • Check the remote URL:
git remote -v
  • Reset the URL if necessary:
git remote set-url origin git@github.com:your-username/your-repo.git

These issues commonly occur for beginners. However, with clear causes and solutions, they can be resolved calmly. Treat errors as learning opportunities and gradually build confidence.

7. FAQ

Q1. What Is the Difference Between Git and GitHub?

A:
Git is a version control tool that manages change history locally. GitHub is a cloud service that hosts Git repositories and enables sharing and collaboration.

  • Git: Local and offline history management
  • GitHub: Online repository hosting and collaboration

Q2. Should I Use SSH or HTTPS?

A:
SSH is recommended because:

  • No repeated password entry
  • More secure and convenient long-term

However, HTTPS may be required in certain corporate or network environments.

Q3. How Can I Use Multiple GitHub Accounts on Ubuntu?

A:
Generate separate SSH keys for each account and configure them explicitly.

  1. Create separate SSH keys
    Examples: ~/.ssh/id_ed25519_work, ~/.ssh/id_ed25519_personal
  2. Edit ~/.ssh/config:
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work

Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
  1. Update the remote URL:
git remote set-url origin git@github.com-work:your-work-user/your-repo.git

Q4. Can I Use Git with a GUI?

A:
Yes. Visual Studio Code provides GUI-based Git operations, including commits, pushes, diffs, and branch management. It is highly recommended for beginners.

Q5. What Happens If I Delete the Remote Repository?

A:
Deleting a remote repository does not affect the local repository. However, git push will fail.

You can reconnect to a new remote as follows:

git remote set-url origin git@github.com:new-user/new-repo.git

Q6. Is It Possible to Avoid the Terminal Completely?

A:
Yes. GUI Git clients allow terminal-free workflows:

  • GitKraken
  • Sourcetree (not officially supported on Linux)
  • Visual Studio Code (built-in Git support)

For Ubuntu users, the VS Code + GitLens combination is the most practical and user-friendly.

Q7. How Far Back Can Git History Go?

A:
Git preserves all history from the very first commit. You can view it using:

git log

To view history for a specific file:

git log path/to/file
侍エンジニア塾