Working with Git as a Newbie
Introduction
Since friends regularly ask me how to work with Git, I decided to write a short guide on how to get started using it. This guide is aimed at beginners who have never worked with Git before. I will cover the basics of Git, how to set up Git on your system, and how to work with Git in a team.
Installing Git on Linux
To install Git on Linux, it is best to use your package manager. A Package Manager is a tool, that allows you to easily download programs that are commonly used.
As one of the most common programs in the field of Computer Science, probably all package managers come with it.
To install Git on Ubuntu, you can use the following command:
! You can check your Distribution with the command
cat /etc/os-release
Important Concepts
Git is the most popular version control system, and is used by millions of developers worldwide. The main use of a version control system is to keep track of changes in your codebase. This allows you to work on a project with multiple people and make sure that everyone is working on the same version of the code.
The main feature of Git is, that it is a distributed version control system. This means that every developer has a copy of the code on their local machine.
A common Git workflow consists of the following steps:
SSH Keys
SSH Keys are a kind of passwordless secret, that allows you to authenticate agains remote servers in a quick and secure way, that is generally more secure, than entering a password during each login.
There are multiple cryptographic technologies in use while authenticating with an SSH-Keys. Older SSH-Keys rely on the RSA-Cryptoalgorithm and newer ones often use the Edwards-curve Digital Signature Algorithm (EdDSA) which is based on elliptic curves.
The most common use of SSH-Keys is to authenticate against remote servers, like GitHub or Gitea, but they can also be used to authenticate against other services, like your own server.
Generating SSH Keys
To get started using SSH-Keys one has to generate a personal key pair before first use. Most Distributions come with the tools used to create, edit and distribute SSH-Keys pre-installed.
To generate a new SSH-Key, you can use the following command:
This command will generate a new SSH-Key using the EdDSA algorithm. The -C
flag is used to add a comment to the key, which is useful if you have multiple keys.
By default, the SSH-Key will be saved in the ~/.ssh
directory with the name id_ed25519
for the private key and id_ed25519.pub
for the public key.
After running this command, you will be asked to enter a passphrase. This passphrase is used to encrypt the private key, and is used to protect your key from unauthorized access. It is optional, but I recommend using a passphrase to protect your key in case it gets stolen.
Adding SSH Keys to GitHub
After generating your SSH-Key, you are are ready to add them to your GitHub account, so you can download and upload code written by you.
To add your SSH-Key to GitHub, first copy the public key to your clipboard using the following command and copy the output:
To also copy the output to your clipboard, you can use the following command:
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
Then go to your GitHub account settings and click on the “SSH and GPG keys” tab. Click on the “New SSH key” button and paste your public key into the text box. Click on the “Add SSH key” button to add your key to your GitHub account. That menu is accessible via the following link: GitHub SSH Keys.
Working with Repositories
Cloning a Repository
To clone a public repository, you simply have to open it in your browser, click on the “Code” button, and copy the URL of the repository.
To use the git clone
command. This command will download the repository to your local machine and create a new directory with the name of the repository.
To clone a public repository, you can use the following command:
This will clone the repository awesome-selfhosted
to your local machine at your current directory.
Cloning a private repository is a bit more complicated, as you have to use SSH to authenticate against the remote server. To clone a private repository, you have to use the SSH URL of the repository. It can be obtained by clicking on the “Code” button and selecting the SSH tab.
Creating a Repository on GitHub
To create a new repository on GitHub, you have to go to your GitHub account, click on the ”+” button in the top right corner, and select “New repository”. You have to enter a name for your repository, a description, and select whether it should be public or private. You can also add a README file, a .gitignore
file, and a license to your repository.
A README.md
file is a markdown file that is displayed on the front page of your repository. It is used to give a brief overview of your project and how to use it. Markdown is a lightweight language that is used to format text.
A .gitignore
file is used to tell Git which files and directories to ignore. This is useful if you have files that you don’t want to be tracked by Git, like log files or temporary files.
A LICENSE
file is used to tell others how they can use your code. There are many different licenses available, like the MIT license, the GPL license, and the Apache license. You can choose the license that best fits your project.
You can clone your new repository to your local machine using the git clone
command explained above.
Your First Commit
After cloning a repository, you can start working on your project. To make changes to your code, you have to use the git add
command to add your changes to the staging area, and the git commit
command to commit your changes to the repository.
If you have not used Git before, you have to configure your name and email address before you can make a commit. You can do this using a command like this:
To add your changes to the staging area, you can use the following command:
To commit your changes to the repository, you can use the following command:
The commit message should be a short description of the changes you made. It should be in the present tense and should be less than 50 characters long. If you need more space to describe your changes, you can use the -m
flag multiple times.
Pushing Changes to GitHub
After committing your changes, you can push the newly written code using git push
. This command will upload your changes to the remote repository on GitHub.
Pulling Changes from Remote
If you are working in a team, you have to pull changes from the remote repository before you can push your changes. This is done using the git pull
command. This command will download the latest changes from the remote repository and merge them with your local changes.