This lesson is still being designed and assembled (Pre-Alpha version)

A quick introduction to Git and Github

Overview

Teaching: 0 min
Exercises: 0 min
Questions
  • What are the basic terms used by version control systems?

  • Which files are contained within the .git directory?

  • How to install git?

  • How does the basic collaborative workflow look like?

  • What are some of the most important git commands?

Objectives
  • Understand the basic terminology of Git and Github

  • Install and setup git

  • Understand the collaborative workflow and commit message etiquette

  • List some of the most useful commands that can be easily accessed in your everyday work

Version Control Basics

There are various Version Control Systems such as:

A version control system can be either:

Terminology

Version Control System / Source Code Manager

A version control system (or source code manager) is a tool that manages different versions of source code. It helps to create snapshots (“commits”) of project files, thereby, supporting the tractability of a project.

Repository / repo

A repository is a directory which the version control system tracks and should contain all the files of your project. Besides your project files, a repository contains (hidden) files that git uses for configuration purposes. Git, by default, tracks all your files in a repository. If there are files you do not wish to track, you can include them in the manually created .gitignore file.

Repositories can be located either on a local computer or on the servers of an online version control platform (such as Github).

Staging Area / Staging Index / Index

Before committing changes to your project code, the files you want to snapshot need to be added to the Staging Index. Changes to these files can be captured in a commit.

Commit

A commit is a snapshot of the files that are added to the Staging Index. Creating a commit can help you to save a particular version of your project. When committing changes, you should also include a commit message that explains the changes of the project files since the previous commit. Therefore, commits track the evolution of your project and allows you to see the changes from one commit to another. It is also useful when experimenting with new code, as git makes it possible to jump back to a previous commit in case your code changes do not work out as planned.

SHA

A SHA(“Secure Hash Algorithm”) is an identification number for each commit. It is a 40-character string composed of characters (0–9 and a–f) such as e2adf8ae3e2e4ed40add75cc44cf9d0a869afeb6.

Branch

A branch is a line of development that diverges from the main line of development. It further allows you to experiment with the code without modifying the line of development in the master branch. When the project development in a branch turns out successful, it can be merged back to the master branch.

Checkout

Checkout allows you to point your working directory to a different commit. Therefore, you can jump to a particular SHA or to a different branch.

.Git Directory Contents

The .git directory contains:

Install, setup git

Git workflow

Making changes

Git_changes

Collaborate with others

In practice, it is good to be sure that you have an updated version of the repository you are collaborating on, so you should git pull before making your changes. The basic collaborative workflow would be:

It is better to make many commits with smaller changes rather than of one commit with massive changes: small commits are easier to read and review.

Git_collaborate

Branching, conflicts

Note, if someone pushes a commit to GitHub before you push your changes, you’ll need to integrate those into your code (and test them!) before pushing up to GitHub.

Conflicts occur when two or more people change the same file(s) at the same time. The version control system does not allow people to overwrite each other’s changes blindly, but highlights conflicts so that they can be resolved.

Git_conflict

Useful commands

Code Short description
git init Initialize local git repository
git status Check the status of git repository (e.g. the branch, files to commit)
git add Add files to staging index
git add . Add all modified files to staging index
git commit -m"Text" Commit changes with commit message
git log Check git commits specifying SHA, author, date and commit message
git log --oneline Check git commits specifying short SHA and commit message
git log --stat Check git commits with additional information on the files changed and insertions/deletions
git log -p Shows detailed information on lines inserted/ deleted in commits
git log -p --stat Combines information from previous two commands
git log -p -w Shows detailed information on commits ignoring whitespace changes
git show Show only last commit
git show <options> <object> View expanded details on git objects
git diff See the changes that haven’t been committed yet
git diff <SHA> <SHA> Shows changes between commits
git tag Show existing tags
git tag -a "tagname" Tag current commit
git tag -d "tagname Delete tag
git tag -a "tagname" "SHA pattern" Tag commit with given SHA pattern
git branch "name_of_branch" "SHA pattern(optional)" Create new branch – at SHA pattern
git branch “name_of_branch” master Start new branch at the latest commit of master branch
git checkout “name_of_branch” Move pointer to the latest commit of the specified branch
git branch -d “name_of_branch Delete branch, use -D to force deletion
git checkout -b “name_of_branch” Create branch and checkout in one command
git log --oneline --graph --all Show branches in a tree
git merge “name_of_branch_to_merge_in” Merge in current branch to another

Useful resources for mastering git and github:

Useful GUI tools for version control:

Key Points