Skip to main content

Basics

Install & configure Git (WSL)

First of all we can check if Git is already installed in our WSL. To do this we run the git --version command. If there is no Git version installed we can then do that by running sudo apt install git. We can then use git normally and set up our credentials. We do this so we don't have to log in every time we try to do anything. Grab your Login details (Username and E-Mail) and set them up like this:

git config --global user.name <YOUR USERNAME> and then git config --global user.email <YOUR EMAIL>

We're not done yet, however. To skip entering our password as well, we use our local SSH-key.

To set up the SSH-Key with Git, navigate to the folder that your keys are in (Usually "C:/Users/Name/.ssh") and open your public key. It's the one with .pub at the end of the file name. Then, navigate to your account settings in Git and go to the "SSH Keys" tab. Here, copy the contents of the public key into the "Key" input, give it a name and expiration date (optional) and you've done the Git side of things.

Now we need to copy your keys into the WSL. To do this create a folder in the root by running mkdir ~/.ssh. Then, we copy our files into this folder by running this command:

cp -r /mnt/c/Users/<YOUR USERNAME>/.ssh ~/.ssh

Please note that if your Username contains a whitespace, you'll need to escape it. So if your user is called "Max Mustermann", your command will look like this:

cp -r /mnt/c/Users/Max\ Mustermann/.ssh ~/.ssh

Finally, we set the file permissions of the private key with chmod 0600 <FILE NAME>. Your private key is the one that has the same name as your public one, but does NOT have a file ending. Normally it will be called "id_rsa".

Start a Project

Create a new Project

If you have a folder you want to push into a new Repository, navigate into this folder and run the following commands:

git init --initial-branch=main This will initialise git in this folder AND tell it in which branch it should add files to in the future.

git remote add origin git@<YOUR REPOSITORY URL> This will tell Git which repository to push into. The repository needs to be empty for this. To do this, simply uncheck the "Add Readme.md" option when creating the repository.

Clone a repository from Git

To clone an existing repository, go to the repository in Git, find the "Clone" button in the top right corner and copy the URL from there. Then navigate to the Folder you want to clone the repository into and run: git clone <REPOSITORY URL> . The . at the end will tell Git to copy the files into the folder you're currently in, as opposed to creating a new subfolder.

Process changes

See status

Once you have an initialized git folder up and running, git will keep track of any and all changes. To check if anything has changed within the folder, use:

git status

This will give you an overview of any and all changes. These will not have been commited to the repository yet however.

Add files to staging

To add files to your staging, run either of these commands:

git add . or git add <FILE OR FOLDER PATH>

This will add - you guessed it - either all changes or just a specific file or folder.

Add specific changes (hunks) to staging

You can add the flag -p to your git add command to then be prompted to select specific changes from the file(s)

git add -p . or git add -p <FILE OR FOLDER PATH>

You will then be shown every single change within those files, split up into so called hunks. It will ask you for different inputs, like y for "yes, add this" or n for "no, skip this". To see all your options, type in ? and check what it says there.

Commit files to git

To commit all staged files (see above), use git commit. You will have to add a commit message though. You do this by adding -m "do XYZ". Now, why do i write "do XYZ" in this message here? This is because you should always phrase your commit messages as a way to end the sentence "This commit will (...)". So for example, if our commit adds a .txt file:

git commit -m "Add a .txt file"

Push commits

To push the commit(s) you have added with the git commit lines, you use:

git push

If your repository is new, you might need to use this instead:

git push --set-upstream origin main

This will allow you to push into the new repository.

Check changes

You can see what changes are currently being tracked. You can do this either for staged or unstaged files.

For unstaged files use git diff. For staged files, add the suffix parameter --staged.

Reset Changes

If you want to reset all your tracked changes, you can use:

git reset <branch-reference>

This will reset everything to the state of the branch you're specifying, leaving differences as uncommited changes. If you want to completely throw away what you have done, use the parameter --hard.

Branches

If you want to work on a repository, but you don't want to interfere with other people working on it, or you want to develop different versions of a repository at the same time, you use branches. Like branches of a tree, these run parallel but independent from the main branch. Any changes here will not be applied to the main branch without being explicitly merged (see below).

See all available branches

To get an overview over existing branches, use:

git branch

Create a new branch

In your repository folder, you can add branches by running:

git branch <new-branch-name>

Alternatively, you can create a branch AND switch to it at the same time by using

git checkout -b <new-branch-name>

You will then be able to push changes into that specific branch.

Switch to existing branch

To switch to an existing branch, you can use the checkout command from above, but without the "-b" flag:

git checkout <branch-name>

Delete a branch

If you have a good reason to do so, you can delete branches by using -d for deleting a merged branch or -D to delete a branch, no matter if it was merged or not. Usage:

git branch -d <branch-name> or git branch -D <branch-name>

Stashing

Some operations will not be permitted by Git if you have untracked changes. Let's assume you try to push a file that was changed by someone else in the meantime. You might have to overwrite that file by pulling, losing some if not all of your changes. In that case you'll need to stash your changes. This will basically put the files into a kind of cache, so you can take it out of there later and reuse.

Stash changes

To stash changes, use:

git stash

This will have saved all your changes into the stash, and revert all changes back to a clean state.

Apply changes from stash

Now assume pull and overwrite our changes. How do we restore what we did? To do this, use this command:

git stash apply

This will take the most recently stashed changes and apply them to the current branch you're in. It will further also still keep these changes in the stash. If you want to apply changes and also delete them from the stash, use:

git stash pop

This will take the most recently stashed changes, delete them from the stash and apply them to the current branch you're in.

Stash list

You can stash multiple changes. Each time you run git stash, a separate element will be created in the stash. To show a list of all stashed changes, run:

git stash list

This will show you a list of all stashed changes and also show you the corresponding index of them. You can use this index to apply/pop other stashed elements than the most recent one by using it as part of the commands:

git stash apply stash@{1} or git stash pop stash@{1}

Merging

Merging is the concept of adding changes made to one branch to another branch. This is done via merge requests. Do this in the Web UI and not via the console.

You need to assign a reviewer and an asignee. Our workflow is that the reviewer (not you) checks your changes for errors and then approves it. Afterwards the Assignee (you) is allowed to go through with the merging.