Git for Dummies

Index

Requirements

Windows

OpenSSH

OpenSSH has been added to Windows as of autumn 2018, and is included in Windows 10.
You only need to generate your ssh key by typing in the CMD the following command:

1
ssh-keygen

Accept the file by pressing Enter both for the file and password (2 times).

After that, under your home user directory (C:\Users\your_username) a new directory called .ssh containing both your public and private key.

Git

Download and install Git from here

Linux/Mac

For each Linux distro use their package manager to download both Git and OpenSSH (in the following example apt is used, for Mac use homebrew):

  • Linux
1
sudo apt install git openssh-client openssh-server
  • Mac
1
brew install openssh git

You only need to generate your ssh key by typing the following command:

1
ssh-keygen

Accept the file by pressing Enter both for the file and password (2 times).

After that, under your home user directory (/home/user) a new directory called .ssh containing both your public and private key.

Setup GitHub repo and SSH key

Create Repo

Navigate to GitHub, login and create a new repo by clicking on New:

image

Next select private repo (highly recommended when doing school/work projects), check Add a README.

Check Add .gitignore, this will create a file called .gitignore which lets you control which files are pushed to the repo and which are kept local (Recommended when working in a team, it prevents any unwanted IDE config files or user based settings).

image

GitHub offers different templates for a variety of programming languages including most common files, in this example we will select Java.

Finally add a repository name (must differ from every other repository in your account) and optionally a description/license.

After creating the repo you will be presented with a screen like this:

image

Let’s analyze the top navigation menu:

  • Code: repo structure, where all the source code is displayed;
  • Issues: issues are threads from collaborators or GitHub users reporting future features or bugs;
  • Pull Requests: pull requests are changes from other users and/or branches ready to be merged into the main branch which needs approval from the owner of the repo;
  • Actions: CI/CD tab;
  • Projects: manage ToDos, workflows etc. directly from Github;
  • Wiki: setup a wiki for your repo;
  • Security: security policies and various options;
  • Insights: track repo main info;
  • Settings: repo settings, under Manage Access you can invite collaborators to your repo.

Setup SSH key

Your unique SSH keys pair (private/public) lets GitHub recognize you and manage your repo from multiple machines.

Navigate to the .ssh folder created in the previous section and copy the content of your id_rsa.pub file.

Next navigate to GitHub settings by clicking in the top right corner of the webpage:

image

Under SSH and GPG keys click New SSH key and paste the previously copied one with a title identifying your machine.

image

Learn git commands and workflow

Clone repo

Now that we have created a repo we need to download it to our machine.

First navigate to your repo, click Code, choose SSH and copy the link:

image

Now open Git Bash on Windows or your terminal on Linux/Mac, navigate to your preferred folder and clone your repo as shown below.

image

As we can see the local directory matches the remote one.

image

Understand Git basic workflow

The previous image summarizes the basic Git workflow, now we will analyze it in depth.
1
git pull

Fetch all changes, branches and commits stored on the remote repo.

When working in team whenever one collaborator pushes their changes (in the same branch), another one can retrieves the changes by simply using git pull.

Git pull will warn you if the update involves a file you are editing, so it’s always recommended to pull before commencing to edit your repo or commit all untracked files (explained later on).

1
git add <filename>

Add files to the current index of tracked files, specifying a file will only add it, while using git add . will track all files in the current working directory and subdirectories.

1
git status

Print the whole status of the working directory, dividing files into untracked, modified, new etc.

This command is very useful after a long work session in order to clarify which file and directory has been modified.

Here is an example:

image

1
git commit -m"commit message"

Create a local commit (snapshot of the current working directory), you need to add the commit message explaining which changes were made.

Below, I linked you various sources in order to understand different conventions used for commits, you can decide to follow them or create your personal one:

  • Git Commit Emoji: link

  • Semantic Commit Messages: link

  • Commit Message Guidelines: link

1
git push

Send local commit to remote repo, it will give you an error if your local repo isn’t updated.

Below an example of the described flow:

image

Useful commands

1
git restore <filename>

Restore a local file to its previous state, reverting all changes.

1
git stash

Save the current state of the working directory in the index letting you pull your repository without any conflicts.

This is very useful when you are editing a file and you know your solution is wrong so you need to discard all changes made.

Execute git stash then git pull and finally git stash drop to discard your changes.

1
git rm

Delete a file from index and from the remote repo. When used with the --cached flag, it removes only from the remote repo.

Further info

Other more complex commands can be found in the Git documentation: here

Git explained in 100 seconds (video): here

Branches

When working in a team on the same project, the best practice is to use branches in order to avoid conflicts on files.

I will carefully explain the simple flow of a merge (the gray arrow indicates the latest state of the repo).

Create a branch with git branch, this will create an exact copy of the current working directory in another branch.

image

Switch to the newly created branch with git checkout <branch-name>.

image

Notice that the created branch is only available on the current local machine in order to push it remotely you need to execute `git push –set-upstream origin after switching to it.

You can now see the branch in your remote repo by selecting main.

image

After editing the working directory, committing and pushing to the remote repo, the branch will be ahead of main by 1 commit.

image

Switch back to main.

image

Merge main branch with the other one; this will update the main branch with the latest updates.

image

Then use git push to remotely update the main branch.

If you want to delete a branch just type git branch --delete <branch-name>

Merge conflicts

When working with multiple branches, sometimes merge conflicts will occur; these are chunks of codes in your files where Git couldn’t resolve the right version.

In order to resolve these conflicts you need to manually select which version of the file you want to keep.

Most IDEs will have an automatic method to select the different versions, here is an example with Visual Studio Code.

image

Where HEAD is the current change, while the other one could be a commit ID or branch name indicating the incoming change.

Simply removing one or another will fix the merge conflict.

GitHub Desktop

If you are using Github as your repository holder you can use GitHub Desktop which is the GUI tool for Git; it is very easy to use and intuitive so I will not cover its usage in this guide.

Download it here

Git integration with Intellij

Like most IDE, Intellij has a Git integration which includes all commands covered in this guide.

In order to access it just go over the Git tab.

image

Git clone

Cloning a repo can be done either by adding the repository URL or login directly via GitHub (recommended).

image

Git pull

Pull section lets you select which branch to pull from.

image

Git merge

Merge section lets you select which branch to merge with.

image

Git commit

When opening the commit section the following window will appear:

image

In the top left corner, there is the Changelist section, a list of files/directories to check in order to include in the current commit.

On the right there are different sections:

  • Git: specify author of the commit etc.;

  • Before Commit: cleanup and reformat code, optimize imports (remove unused imports), code analysis (warning/errors checks), ToDo check;

  • After Commit;

Finally on the bottom the diff tab showing all changes made on the selected file/directory.

You can choose between Commit (local) and Commit and Push (remote).

GitHub dropdown menu

Under Git>GitHub you have different choices:

  • Share Project on Github: create a remote repo with the current project (only when the current working directory isn’t a .git repo);

  • Create Pull Requests (not covered in this guide);

  • View Pull Requests (not covered in this guide);

-Rebase my Github fork (not covered in this guide);

image