Skip to content
Christian De Frene edited this page Dec 1, 2018 · 2 revisions

Beginner

Git is a version control system (VCS) created by @torvalds. It is used to keep track of file changes made by multiple contributors on a project and thus keep everyone in sync, regardless of what files they may or may not have been working on.

A repo is a project that has enabled git. These repos can be uploaded to remote servers that people on the same team can use to upload and download files to. You can host a remote server on your own computer, or you can use one of many git services, e.g. Github.

To get started with git, first you need to install it:

  • Windows: Install a git client, e.g. Git bash
  • MacOS: Install the command line tools by running xcode-select --install in the terminal, which comes with git
  • Linux: Install git through a package manager, e.g. sudo apt-get install git on Ubuntu

A command line interface (CLI) is used to execute commands, either in terminal (macOS and linux), or in the git bash command line tool (windows). The base command is git, followed by the action you want to perform. We'll look at the most common use cases for git below.

Intermediate

Git treats your project like a tree, where each file is thought of as a branch in the tree. Whenever you do new work on an existing file, the branch splits in two: The original file, and your local version of the file. This process is known as branching (sometimes called forking since the branch split sorta resembles a fork). Once you are done making your changes, you have two options: Either, you let the new branch live on in the system, or you can merge it into the original branch. By doing the latter, you are essentially adding your changes into the original file, with git keeping track of what those changes were. Note that you can also simply add all changes to the original branch; don't do this if you have other people than yourself working on a project.

To setup git for your new project, you open the CLI, navigate to your project (root) folder, and run:

git init

This will add the file .git inside the project folder which handles all git infrastructure for your project.

To obtain a local copy of an existing project that already uses git, you obtain the repo URL and run:

git clone <URL>

For ksg-nett, you obtain the github repo URL by clicking the green "Clone or download" button in the top right corner.

Basic git example

Now that you have a valid copy of the repo, it's time to start contributing. If using a remote, you should first check for new changes made by others and merge them into your local version by pulling the remote:

git pull

# If pulling a specific branch
git pull origin/remote-branch-name

Before or after having made your changes, you should branch out to your own branch:

# Create your branch and then switch to it
git branch branch-name
git checkout branch-name

# You can do the above in one operation
git checkout -b branch-name

Now that you are on a branch separate from the original, you are ready to commit your changes. A commit is how you tell both git and your team about what changes you have made, and consists of a list of file changes and a commit message. To commit files, you first need to add them to the staging area:

# Add single file
git add folder/file_name.py

# Add all changed files
git add .

# Remove single file from staging area
git reset HEAD folder/file_name.py

Now we are ready to commit the changes:

# Create a commit, and open default text editor for writing the commit message
git commit

# Create a commit with the supplied message
git commit -m "Your message here"

Writing a good git commit message is an artform in itself. Check out the KIT Soft skills page for some tips and guidelines.

After having created your commit, you should push your changes to the remote so that your team can make use of them:

# Push your changes to a specific remote branch
git push origin branch-name

# You can alternatively set the "upstream" so you don't have to specify the branch name each time
git push -u origin branch-name

# Successive pushes will go to branch-name as default
git push

Congratulations! You have now successfully versioned your changes into the git tree and sent them to the remote. That wasn't so hard, was it?

Git management commands

Not all interactions with git goes according to plan; here are some commands that are helpful to regain control:

  • git status: Get an overview over all files you have changed, and which of them you have added to the staging area
  • git log: List all commits for the current branch, and which have not been pushed to the remote
  • git branch: List all local branches

Pull requests

You might be satisfied with the changes you just pushed, but that doesn't mean the rest of the team sees it that way. Therefore, all branches that are pushed should be added as a pull request (PR) so that other team members can review the changes before they are merged into the main branch.
While it's possible to create a new PR from the CLI, the preferred and easiest way is to do it directly from github. This can be done by going to the "Pull requests" tab. Click the green "New pull request" and select your branch from the right dropdown menu. If you recently pushed your branch, it will appear as a suggestion at the top when going to the tab.

Advanced

commit -a --amend --no-edit

reset

fetch

rebase, merge, squash

stash


Learn more:

Resources to learn git
Oh shit, git! (Practical examples on how to resolve tricky git situations)
Git cheat sheet
Official git documentation

Clone this wiki locally