Git is a very useful version control tool. It allows you to work collaboratively with any number of developers, as well as keeping backups of every version of your project.
Installation and creation of a project
Intallation on Ubuntu is very simple, just press
Ctrl+Alt+T to open a terminal window and run
sudo apt install git.
Next, create a folder for your new project by running
mkdir MyProject, and enter it with
cd MyProject. Now you must initialize this folder as a git repository, by tunning
git init. This will create a hidden folder called
.git inside your project folder, which you can see with
ls -a (the
-a parameter will show hidden folders).
Next, we want to add a file to our project. Let’s add a README file.
touch README.md will create an empty file called
nano README.md will open that file with the nano command-line editor (you could use any text editor, for instance vi or gedit). Once you have added some text to your README file, press
Ctrl+X followed by
Enter to save the changes and return to the command line (in nano).
Now that we have a new file, run
On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) README.md nothing added to commit but untracked files present (use "git add" to track)
README.md is currently “untracked”. This means there have been changes to this file since the last “commit” in our repository. A “commit” is essentially a snapshot of the project that git has stored in its repository. To add files to the next commit, we will use the command
git add. This will add the files to the so-called “staging area”. Running
git add README.md will add only that file, running
git add . will add all untracked files. Running
git status again will show us the changes we made:
On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md
git commit will now create a new commit with all the staged files added to the repository. If you run
git log you will see the commits in your repository.
Accessing a remote repository via SSH
If you have created a repository on a remote hosting service (for instance GitHub, BitBucket or ETH GitLab), you will want to be able to work on it on your computer. Git allows you to download (or “clone”) the repository to your computer, make new commits and push and pull them from the server.
First off, we want to generate an SSH public/private key pair (unless you already have one). In order to do this, run the command:
ssh-keygen -t rsa -C "firstname.lastname@example.org" -b 4096
Then you will be prompted to specify a path to save the keys to. Simply hitting
Enter here will save them in the default path and overwrite any existing keys. Next, you will be asked to enter a password/passphrase to secure your keys. This is considered good practice, but not required and can be skipped by pressing
Now that we have the keys, we want to copy the public key to our clipboard, so you can paste it into your website of choice (GitHub, ETH GitLab etc.). We can do this with the program
xclip (which you may need to install first with
sudo apt install xclip):
xclip -sel clip < ~/.ssh/id_rsa.pub
~/.ssh/id_rsa.pub is the default path for Ubuntu, you may need to change it.
Important: your private key is stored at
~/.ssh/id_rsa. NEVER copy this anywhere or share it with anyone! If anyone should get their hands on your private key, they would be able to assume your identity. Only the public key should be copied, shared, sent over the internet etc.
Once you have your SSH public key copied, you can paste it into the appropriate filed on GitHub (there is usually a “SSH keys” section in the settings).
Finally we want to clone the repository from the remote source to your computer. You will need to get the SSH address from your online repository. This looks something like
email@example.com:user/my-repo.git. Here I have
gitlab.ethz.ch, which is my university’s custom GitLab location, but you may have something like
github.com instead. Then you can clone the repository with
git clone firstname.lastname@example.org:user/my-repo.git.
Now we can work on our project and commit as usual. Every now and then you will want to sync your repository with the commands
git push (upload your changes to remote) and
git pull (download changes from remote).