Git is the most widely used version control system in the world. If you're building software, writing scripts, or even just managing configuration files, it's an essential tool to learn.
In this guide, we'll walk step-by-step through:
- π οΈ Installing Git on Ubuntu
- βοΈ Configuring your Git identity
- π Creating a local repository
- π Setting up SSH access to GitHub
- π Connecting it to GitHub
- π Pushing your first commit
Prerequisite: You'll need a GitHub account before starting. If you don't have one yet, sign up. It's free and takes just a minute.
π οΈ Step 1: Install Git on Ubuntu
Check if Git is already installed:
git --version
If you see command not found
, continue with the installation steps below. If it's already installed, move on to Step 2.
sudo apt update
sudo apt install git -y
The
-y
flag "yes" automatically answers "yes" to any prompts during the installation.
Verify the installation:
git --version
If successful, it should return something like this:
git version 2.34.1
βοΈ Step 2: Configure your Git identity
Git needs to know your name and email so it can label your commits. You'll see them locally in git log
and on GitHub next to each commit.
To set your name and email run the following commands:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
user.name
does not have to match your GitHub username. It's just a label on your commits. Use your real name or a handle, up to you.user.email
should match one of the emails registered with your GitHub account if you want your commits linked to your profile. If it doesn't, commits still appear, but won't show your avatar or count toward your contribution graph.
Verify your settings:
git config --global --list
The output should look like this:
user.name=Your Name
user.email=your-email@example.com
π Step 3: Set up SSH access to GitHub
Using SSH means you won't have to type your username and password every time you push code to GitHub.
3.1. Check existing SSH keys
Let's first check whether you have existing SSH keys.
ls -a ~/.ssh
If you see files like id_ed25519
and id_ed25519.pub
, you already have a key pair and you can continue with Step 4.
If you don't have any or wish to create new ones, continue with this step.
3.2. Generate a new SSH key
π Warning: If you generate a new key with the same name as an existing one, you will overwrite it and any service (like GitHub) using the old key will stop recognizing your machine until you add the new one.
Generate an SSH key:
ssh-keygen -t ed25519 -C "test-key"
-t
defines the key type which ised25519
in our case.-C
adds a comment to your SSH key. It's just a label to help identify the key later if you generate multiple ones. It's not used for authentication or commit association.
You'll be asked to enter a name for the new key. If you simply press Enter
, the key will be saved under the default name id_ed255119
. To avoid overwriting any existing keys, append the proposed name with _test
. Make sure to include the full path and replace walodja1987
with your Ubuntu username:
/home/walodja1987/.ssh/id_ed25519_test
You'll be asked to set a password to protect your keys. It's optional but recommended.
3.3. Start the SSH agent and add your key
Before we run the commands, let's briefly understand what an SSH agent is:
- When you generated the key with
ssh-keygen
, it was saved as a file on your disk (/home/walodja1987/.ssh/id_ed25519_test
). - That file contains your private key, but SSH clients like Git won't use it automatically. And if it's protected by a passphrase, they can't read it directly without your help.
- An SSH agent is a small background program that runs in your session and remembers unlocked keys in RAM. It handles authentication automatically any time you connect to a server like GitHub over SSH, without asking you to type the passphrase again.
- Think of
ssh-keygen
as creating a key and storing it in a safe.ssh-add
is like unlocking it once and handing it to a trusted assistant (the SSH agent), who will present it whenever needed, so you don't have to fetch and unlock it yourself each time.
With that in mind, let's start the SSH agent and add your key. π
First, check that SSH tools are installed (they usually are on Ubuntu, but minimal Docker images not include them):
ssh -V
If you don't see an OpenSSH version in the output, install the client:
sudo apt update
sudo install -y openssh-client
Now start the SSH agent and add your key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_test
Here's what's happening:
-
eval "$(ssh-agent -s)"
: Starts the SSH agent and sets up your environment to talk to it. -
ssh-add ~/.ssh/id_ed25519_test
: Loads your private key into the agent. If it's passphrase-protected, you'll be prompted once. After that, the agent remembers it for the rest of your session.
You can confirm the key was added with:
ssh-add -l
3.4. Add your public key to GitHub
Show your public key:
cat ~/.ssh/id_ed25519_test.pub
Copy the full line (it starts with ssh-ed25519
and ends with the comment you chose, e.g. test-key
).
Then go to GitHub β Settings β SSH and GPG keys β New SSH key, then paste it there.
3.5. Test the SSH connection
ssh -T git@github.com
If everything is set up correctly, you'll see:
Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.
Note: The first time you run this command, you may see
The authenticity of host 'github.com (140.82.121.4)' can't be established
. Just confirm withyes
to continue.
π Step 4: Create a new repository on GitHub
Go to https://github.com/new and create a new repository.
- Choose a name (e.g.
test
) - Leave it empty (don't initialize with a README or
.gitignore
) - Click Create repository
Note: There are ways to create a GitHub repository entirely from the terminal (e.g. using GitHub CLI or the REST API). But to keep things simple, we're creating it through the web interface here. We'll cover terminal-only workflows in a future post.
π Step 5: Initialize a local repository
Back in your terminal, create a new folder and initialize a Git repository:
mkdir ~/test
cd ~/test
git init
Create a simple file to commit:
echo "My first Git-backed config!" > README.md
Check the status:
git status
π₯ Step 6: Add and commit your changes
Stage the file:
git add README.md
Commit it:
git commit -m "Initial commit"
This records a snapshot of your project in Git history.
π Step 7: Connect local repo to GitHub
In order to be able to push to the remote GitHub server, you have to connect your local repository to it.
For this, copy the SSH URL from the GitHub repository page.
It should look like this:
git@github.com:your-username/test.git
Now connect your local repo to that remote:
git remote add origin git@github.com:your-username/test.git
Explanation:
-
git remote add
: Creates a new remote connection. -
origin
: The name (alias) you give to that remote (conventionally calledorigin
, but you can name it anything). -
git@github.com:yourusername/test.git
: The remote repository URL (over SSH in this case, not HTTPS). - You are basically telling Git that the local repository now has a remote called
origin
, and it lives at the provided URL (git@github.com:...
).
To list all currently configured remote repositories, run:
git remote -v
Output:
origin git@github.com:yourusername/test.git (fetch)
origin git@github.com:yourusername/test.git (push)
π‘ Note: Git can use different URLs for pulling and pushing. Usually they're the same, but you can configure them separately. For example, if you push via SSH but fetch from a read-only HTTPS mirror.
SSH vs HTTPS
There are two main ways to connect your local Git repository to GitHub: SSH and HTTPS.
-
SSH (
git@github.com:...
): This method uses your SSH key for authentication. Once your key is added to GitHub, Git can authenticate automatically, no username, password, or token required. It's usually the easiest and most seamless option for everyday use. -
HTTPS (
https://github.com/...
): This method uses an HTTPS connection instead of SSH. However, GitHub no longer accepts your regular password for Git operations. Instead, you'll need to create a Personal Access Token (PAT) and use that as your password. You can generate a PAT in GitHub β Settings β Developer settings β Personal access tokens β Tokens (classic).
When using HTTPS, you'll clone or add your remote with the HTTPS URL. Then, when prompted for credentials:
- Username: your GitHub username
- Password: your Personal Access Token (PAT)
Both approaches work equally well. SSH is usually the smoother choice for daily development because it "just works" once set up, while HTTPS + PAT can be handy in automated scripts, CI/CD pipelines, or environments where SSH is blocked.
π€ Step 8: Push your file to GitHub
Let's first rename the default branch named master
to main
, the modern convention used by most projects today:
git branch -M main
This is typically a one-off operation after you created a new repository. When you create new branches in the future, you'll name them yourself (e.g.
feature-branch
), so you won't need to rename anything again unless you want to rename an existing branch.
Then push to GitHub:
git push -u origin main
-u
(short for--set-upstream
): Creates themain
branch on remote (origin
) and links your localmain
branch with that remote branch (origin/main
).- Once this upstream relationship is established, you can use the short commands
git push
andgit pull
instead of typing the full formsgit push origin main
andgit pull origin main
, respectively, when working on themain
branch.- Whenever you create a new branch (e.g.
git checkout -b feature-branch
), you should also includeu
the first time you push it to ensure the new local branch is linked to the corresponding remote branch (origin/feature-branch
), so you can use the short commandsgit push
andgit pull
while working onfeature-branch
.- If you forget the
u
, thegit push origin feature-branch
may still succeed, butgit pull
will give you an errorfatal: no upstream configure...
.- To show all the local branches as well as their linked remote branches, run
git branch -vv
.
If everything is correct, you'll see something like:
π Step 9: Verify on GitHub
Go to your new repository's page on GitHub. You should now see your README.md
file and your initial commit π
β¨ Conclusion
Congratulations! You've successfully installed Git on Ubuntu, connected it to GitHub, and pushed your first commit. This simple workflow is the foundation of virtually every modern software project.
The next step is to deepen your understanding of Git's most common commands and workflows, things like branching, merging, and rebasing. The Git command reference by Atlassian is a great place to continue the learning path.
Note: I use ChatGPT to help me gather information and shape these posts, but I always test the commands myself and add my own insights to make them easier to understand. This blog is my personal record of what I learn as I go deeper into Linux. If it helps someone else too, even better!
Top comments (0)