How to Use Git and GitHub on Ubuntu: Complete Beginner’s Guide (2025)

目次

1. Introduction

Why Integrate Ubuntu with GitHub?

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

Git is a tool used for version control of source code, essential for efficient collaboration in team development. GitHub, on the other hand, is a hosting service that allows you to share and publish repositories managed with Git over the internet, serving as a hub for developers around the world to collaborate and exchange code.

Why Use GitHub in an Ubuntu Environment?

Ubuntu is a popular Linux distribution among developers, well known for its compatibility with open-source development. It provides an environment where tools like Git can be installed easily, allowing seamless integration with GitHub.

Here are some common cases where Ubuntu and GitHub work well together:

  • Managing and sharing source code for languages like Python or C++ on GitHub
  • Contributing to open-source projects
  • Publishing your work as a portfolio

What You’ll Learn in This Guide

This guide will walk you through everything from basic setup to advanced usage of GitHub on Ubuntu, step by step.

  • How to install Git on Ubuntu
  • Initial Git configuration and setting up SSH authentication
  • Creating and managing repositories on GitHub
  • Boosting productivity with Visual Studio Code integration
  • Common troubleshooting tips and FAQ

Even if you’re new to GitHub, don’t worry. We’ll include practical command examples and important tips along the way. If you’re an Ubuntu user looking to get the most out of GitHub, this guide is for you!

年収訴求

2. Installing Git on Ubuntu

What Is Git? A Quick Refresher

Git is a Version Control System (VCS) used in software development. It helps manage the history of source code changes, allowing you to restore previous states and collaborate with others efficiently.

One major advantage of using Ubuntu or other Linux environments is how easily Git can be installed. With the method shown below, you can complete the installation in just a few minutes using the terminal.

How to Install Git via APT

The most common way to install Git on Ubuntu is by using APT (Advanced Package Tool). Follow the steps below to get started.

1. Update Your Package List

sudo apt update

This command refreshes your system’s package list. Be sure to run it before installing anything new.

2. Install Git

sudo apt install git

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

Verify Git Installation

After the installation is complete, use the following command to confirm Git was installed correctly:

git --version

If the installation was successful, you should see output like this:

git version 2.34.1

The version number may vary depending on your Ubuntu version or the latest available package, but as long as you see version info, you’re good to go.

Alternative: Install Git via Snap (Optional)

You can also install Git using the snap command in Ubuntu. However, APT is generally more stable and widely used. Unless you have a specific reason, we recommend sticking with the APT method.

3. Initial Git Configuration

Essential Git Settings Before You Start

Once Git is installed, the next step is to configure your user information and authentication method. Setting these up ensures smooth communication with remote repositories and better collaboration with your team.

Set Your Git Username and Email Address

Every Git commit records who made the change. To do this, you’ll need to set a username and email address in advance.

Configuration Commands

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

The name and email you enter here don’t have to match your GitHub account, but using the same ones helps link your commits to your GitHub profile.

Check Your Current Settings

git config --list

This command shows a list of your current Git configurations.

Generate an SSH Key and Add It to GitHub

While GitHub also supports HTTPS connections, using SSH is more secure and eliminates the need to enter your password every time.

1. Generate an SSH Key

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

Follow the prompts by pressing Enter a few times. This will create a private key ~/.ssh/id_ed25519 and a public key ~/.ssh/id_ed25519.pub.

2. Start the SSH Agent and Add Your Key

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

Now your SSH key is ready to be used.

3. Add Your Public Key to GitHub

Copy your public key to the clipboard:

cat ~/.ssh/id_ed25519.pub

Copy the entire output and register it on GitHub by following these steps:

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

4. Test Your SSH Connection

Use this command to test your connection:

ssh -T git@github.com

The first time, you’ll be asked if you trust the host — type “yes”.
If successful, you’ll see a message like:

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

Recommended Actions After Setup

Once you’ve completed the setup above, you can start cloning, pushing, and pulling from GitHub repositories. In the next section, we’ll cover how to create and work with GitHub repositories in detail.

4. Working with GitHub Repositories

Creating a New Repository on GitHub

Once you’re logged into GitHub, the first step is to create a new repository.

Steps (via Web Browser)

  1. Click the “+” icon in the top-right corner of GitHub and select “New repository”
  2. Fill in the following details:
  • Repository name: e.g. my-first-repo
  • Description (optional): A short summary of your project
  • Public / Private: Choose visibility settings
  1. Click “Create repository”

Once created, GitHub will display the repository’s URL. You’ll use this URL to perform clone and other Git operations.

Cloning an Existing GitHub Repository

To copy a GitHub 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’re using HTTPS, the URL format will be different.

Running the command will create a folder named my-first-repo in your current directory, and all repository contents will be downloaded into it.

Workflow: Modify, Stage, and Commit Files

1. Add or Edit a File

For example, create a new file with:

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

2. Stage Your Changes

git add README.md

Staging means selecting which changes to include in your next commit.

3. Commit the Changes

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

This saves the staged changes to your local repository’s history.

Push Your Changes to GitHub

To upload your local changes to GitHub, use the git push command:

git push origin main

*If your default branch is master or something else, replace main with the correct branch name.

Pull Remote Changes to Your Local Repository

If other developers make changes on GitHub, you can pull those updates to your local machine using:

git pull origin main

This will sync your local branch with the latest changes from the remote repository.

Additional Common Git Tasks

  • Check the current remote repository:
git remote -v
  • Add a new remote repository later:
git remote add origin git@github.com:your-username/another-repo.git
  • You can also version-control files like README.md or .gitignore using the same steps above.

5. Integrating with Visual Studio Code

Why VS Code Is Ideal for Git Operations

When working with GitHub on Ubuntu, using Visual Studio Code (VS Code) can greatly improve your productivity. Developed by Microsoft, VS Code is a free, open-source code editor known for its excellent Git integration. With its user-friendly interface, you can commit, push, and review changes right from the GUI—making it perfect for beginners.

How to Install VS Code on Ubuntu

You can install VS Code on Ubuntu by following these steps:

1. Add Microsoft’s 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 Source

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

Once installed, you can launch the editor by typing code in the terminal.

Built-In Git Support

VS Code comes with built-in Git support, so you can start using Git features right away—no additional plugins required. However, for enhanced functionality, these extensions are highly recommended:

  • GitHub Pull Requests and Issues
    → Manage pull requests and issues from GitHub directly within VS Code.
  • GitLens
    → View detailed commit history, author info, and line-by-line change tracking within the editor.

Basic Git Operations in VS Code

Opening a Repository

Navigate to your cloned project directory in the terminal, then run:

code .

This command opens the current folder in VS Code.

Reviewing Changes and Committing

  • Click the “Source Control” icon on the sidebar (the one that looks like a branch)
  • You’ll see a list of modified files
  • Select a file to see the differences
  • Write a commit message and click the checkmark icon to commit

Pushing and Pulling

  • Use the “…” menu in the Source Control panel to select “Push” or “Pull”
  • You can also push/pull using the status bar in the lower right corner

You Can Still Use the Terminal

VS Code includes a built-in terminal (open it with Ctrl + `), allowing you to switch between GUI and command-line interfaces easily. For example, you might commit using the GUI and then use the terminal to manage branches.

Tips for Troubleshooting

  • If you run into SSH connection errors, make sure VS Code is using the correct SSH key
  • If authentication fails, you may need to reset your credentials or generate a new Personal Access Token (PAT) on GitHub

6. Common Troubleshooting Tips

SSH Connection Error: “Permission denied (publickey).”

Possible Causes

  • The SSH key was not properly generated or registered
  • The public key has not been added to GitHub
  • The SSH agent is not recognizing your key

How to Fix It

  1. Check if the SSH key exists:
ls ~/.ssh/id_ed25519.pub

If it doesn’t exist, generate it with:

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

→ Then go to GitHub > “Settings” > “SSH and GPG keys” to add it

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

Authentication Error (HTTPS): Prompted for Username and Password

Cause

  • GitHub has disabled password-based authentication — you must use a Personal Access Token (PAT)

How to Fix It

  1. Generate a Personal Access Token on GitHub
    https://github.com/settings/tokens
    → Check repo scope when creating it
  2. Use your GitHub username and the token (instead of password) when prompted
  3. To remember credentials, enable caching with this command:
git config --global credential.helper cache

“fatal: not a git repository” Error

Cause

  • The directory you’re working in is not a Git repository

How to Fix It

  • Navigate to a folder that already has a Git repo:
cd ~/your-project-directory
  • Or initialize a new repository:
git init

Merge Conflicts

Cause

  • A merge conflict happens when two people edit the same part of a file at the same time

How to Resolve It

  1. When git pull throws a conflict error, open the file in an editor
  2. Look for markers like this:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> origin/main
  1. Decide what to keep, remove the markers, and save the file
  2. Then run the following to complete the merge:
git add .
git commit

“remote: Repository not found.” Error

Possible Causes

  • The repository URL is incorrect
  • Your GitHub account does not have access to the repository

How to Fix It

  • Check the remote URL:
git remote -v
  • If needed, update the URL like this:
git remote set-url origin git@github.com:your-username/your-repo.git

These types of errors are common for beginners. The good news is that the causes and fixes are usually straightforward. Think of errors as learning opportunities, and take your time to get comfortable with Git.

7. Frequently Asked Questions (FAQ)

Q1. What’s the difference between Git and GitHub?

A:
Git is a version control tool used to track changes in your source code locally. GitHub is a cloud-based service that hosts Git repositories and allows you to collaborate and share your work online.

  • Git: Lets you manage code history offline
  • GitHub: Makes your Git repositories accessible and collaborative via the web

Q2. Should I use SSH or HTTPS for connecting to GitHub?

A:
SSH is generally recommended for its convenience and security. Here’s why:

  • No need to enter your password each time (authentication is handled via keys)
  • More secure and efficient for long-term use

That said, some workplaces or networks may require HTTPS, so choose based on your situation.

Q3. How can I use multiple GitHub accounts on Ubuntu?

A:
You can generate separate SSH keys for each account and configure them using your SSH config file.

  1. Generate a separate SSH key for each account:
    e.g., ~/.ssh/id_ed25519_work and ~/.ssh/id_ed25519_personal
  2. Edit ~/.ssh/config like this:
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. Change the remote URL in your repo using .git/config or this command:
git remote set-url origin git@github.com-work:your-work-user/your-repo.git

Q4. Can I use Git with a graphical interface (GUI)?

A:
Yes! Visual Studio Code provides built-in Git integration for GUI-based workflows.
You can commit, push, view diffs, and switch branches—all from the editor interface. It’s especially helpful for beginners.

Q5. What happens to my local repo if I delete the remote GitHub repository?

A:
Deleting a GitHub repository will not affect your local copy. However, git push will fail since the remote no longer exists.

To connect your local repo to a new remote, update the URL like this:

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

Q6. I’m not comfortable using the terminal. Can I use only GUI tools?

A:
Yes, there are several GUI Git clients available that don’t require terminal usage:

  • GitKraken
  • Sourcetree (Note: not available natively on Linux—can be used via Wine)
  • Visual Studio Code (with built-in Git integration)

For Ubuntu users, the VS Code + GitLens extension combo is one of the easiest and most powerful setups.

Q7. How far back can I view my Git history?

A:
Git tracks your project’s history from the very first commit. You can browse the full commit log using:

git log

To see history for a specific file, use:

git log path/to/file