git
An Introduction
What is it?
Why should you use it?
How does it work?
What is it?
“Git is a free and open source distributed version
control system, which is fast and efficient.“
- Git Homepage
• Version control system = tracks versions of files
e.g. source code, LaTeX thesis, paper or talk, website html, etc.
(Rule of thumb: Everything you edit in text editors)
• Distributed = everyone has a full local copy of the repository
distributed centralized
http://thepilcrow.net/explaining-basic-concepts-git-and-github/ 2
Why
should
you use
it?
3
Why should you use it?
It helps you!
• Transparent history of all changes
• Moving back and forth in time
• Everything is easily traceable and reversible (e.g. errors)
Good scientific practices:
• Reproducibility and traceability
• Enables collaboration
4
How does it work?
• Repository = database containing all
versions of the files
• Snapshot-based system
• Snapshots are called commits
• Commits are named by checksums
(also used to ensure data integrity) https://git-scm.com/book/en/v2/Getting-Started-Git-Basics
• Almost every operation is local
• Working without network connecting
• Distributed system
(everyone carries a backup)
5
How does it work?
4 distinct places
local
also staging area
All of your For now: All changes go Manually synced copies of
(to be) edited here first all committed versions of
project files (Later: Helps disentangling all project files
changes)
6
How does it work?
• Overview of most (Not discussed)
common commands
• Most common use
by command status
line also staging area
Don’t Panic
This is an log
introduction. diff --staged
It is worth it.
7
Gist of this Introduction
Command by command
1. The basic workflow: Set up a local repository
and save changes to it
—> Exercise 1
2. Branches: Work on different features in parallel
—> Exercise 2
3. Basic interactions with a remote repository
8
Notation
what follows are commands to be entered on the command
$ line e.g. $ git log
marks names and other variables that change during
<… use e.g. $ git branch <branch_name>
>
Advanced:
Tips for more advanced user will be in green
boxes
9
$ git help
Get help for any command
$ git help <command>
$ git <command> --help
$ man git-<command>
Works offline!
Installation: Good changes it is already installed, if not [Link]
10
Commands
config
init
branch clone
status
checkou push
add
t merge pull
comm
it diff
log
11
$ git config
Before doing anything else: Identify yourself
$ git config --global user.name <your_name>
$ git config --global user.email <your_email>
Make sure this is consistent across all your machines
Advanced:
Aliases
Make your life easier: $ git config --global alias.st „status“
Set up tab completion „Did you mean?“
[Link] $ git config --global help.autocorrect 10
12
$ git init
Initalizing a empty (local) Git repository
$ git init
Initialized empty Git repository in /path/to/
example_repo/.git/
Adds a hidden .gitdirectory, where git stores all of its
information
Git only uses relative paths (source directory can be moved
freely)
13
$ git status
Display status information of working directory and staging area
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.cpp
nothing added to commit but untracked files present (use
"git add" to track)
(Output for a newly initialized repo with one new file hello.cpp)
14
$ git add
Adding changes to the staging area
$ git add <file_name>
Adds all changes of a file to the staging
area
$ git add <directory_name>
Adds all changes of all files in the directory to the staging area.
Advanced:
You can also add only specific changes to a
file (so called hunks) to the staging area.
$ git add -p <file_name>
Ignore certain files with .gitignore [Link]
15
$ git commit
Committing staged changes to your repository
$ git commit
External text editor (most likely vi) will
open and ask for a commit message
Don’t like vi: $ git config --global core.editor „nano“
Invest in good commit messages!
• Subject line + body (Follow 50/72 rule [Link])
• Write them like a email to yourself / the other developers
• Document why you made the changes
Good commits are small and often, conceptually separated,
only include source files & at best working code
16
$ git diff
Display changes to your tracked files
$ git diff
To be precise: Differences between working directory
and staging area only unstaged changes
Helpful to inspect what you have done
17
$ git log
Display history of your commits
$ git log
See the last changes that were made
including the commit message (at least per default)
18
Basic Workflow
WORK
git status / git diff
git add
git commit
19
$ git commit -a
Directly commit unstaged changes
Advanced:
$ git commit -a Stage all changes to
tracked files at once
Shortcut for $ git add -u $ git add -u
$ git commit
What is the purpose of the staging area?
• Allows to only commit part of your changes (Assemble
your commit to your liking)
• Split changes across commits
• Also other use cases e.g. for reviewing your changes, …
20
Commands
config
init
branch clone
status
checkou push
add
t merge pull
comm
it diff
log
21
Branches
• Branches store different versions of your project
• Parallel development
• Implement new features
• Fix bugs
• Try out something
• Cheap to do in git
(technically just pointers to a commit)
• Main branch = master https://www.atlassian.com/git/tutorials/using-branches
• By default created at initialization
• Usually development is done on other (feature) branches
22
$ git branch
Create new branch
$ git branch <branch_name>
List all branches of local repository
$ git branch
Delete branch
$ git branch -d <branch_name>
Save option to delete a branch, since it prevents data loss
$ git branch -D <branch_name>
Use CAREFULLY! Be sure you want to lose this progress
23
$ git checkout
for branches
Switch between existing branches
$ git checkout <branch_name>
Changes your project files
Only works with no uncommitted changes („Clean working tree“)
Shortcut: Create and checkout new branch
$ git checkout -b <new_branch_name>
24
The HEAD
HEAD = special pointer to currently checked out branch (commit)
$ git checkout master $ git checkout testing
https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
25
$ git diff
Complete Overview
Display changes to your tracked files alre
ady
dis
$ git diff cus
sed
To be precise: Differences between working directory
and staging area only unstaged changes
Helpful to inspect what you have done
$ git diff HEAD
Differences between working directory and
HEAD (last commit)
$ git diff --staged
Differences between staging area and HEAD
26
$ git merge
Merge changes in checked out branch
$ git merge <feature_branch>
Smart automatic three-way merges
Only changes the checked out
branch (ensure you are on the
correct branch)
https://www.atlassian.com/git/tutorials/
using-branches/git-merge
27
$ git merge
Two different merges:
Fast-forward Three-way
Only three-way
merges have
merge
commits
and potential
merge conflicts
https://www.atlassian.com/git/tutorials/using-branches/git-merge
28
$ git merge
Merge conflicts
$ git merge <branch_name>
Auto-merging <file>
CONFLICT (content): Merge conflict in <file>
Automatic merge failed; fix conflicts and then commit the result.
Conflicts if same part of file (hunk) is changed in both branches
Resolve
…
1. Run git statusto see „unmerged paths“ <<<<<<< HEAD
std::cout << "Hello!“;
======
2. Find problematic hunks: Highlighted in files by =
std::cout << „Goodbye!“;
>>>>>>>
<<<<<<< , =======, >>>>>>> say_goodbye_branch
…
3. Create the intended code version and remove <<<<<<< ,… std::cout << „Goodbye!“;
…
4. Then git add <file_with_merge_conflict>
5. git commit(Auto-generated merge commit message)
29
Commands
config
init
branch clone
status
checkou push
add
t merge pull
comm
it diff
log
30
Interacting with Remotes
• So far everything were local operations
• Following interactions with a remote repository require
network connection
• Remote repositories enable collaboration and backup
• Local repository has to be manually synced with remote
repository
Advanced:
Note: In the following only
tracking branches are used
to interact with a remote
repository to keep it simple.
31
$ git clone
Clone (download) a remote repository
$ git clone <link_to_repository>
Creates directory with project name in current directory
Remote repository is (by default) referred to as origin
You can also clone on the same machine locally
See all remote branches of repository
$ git branch -r
Branches that are in remote are prefixed by
origin/then the <branch_name>
32
$ git pull
Checkout a branch of the repository (as usual)
$ git checkout <branch_name>
Do not use the origin/prefix here
Update a branch with the new version from the remote repository
$ git pull
… while the branch is checked out
Changes your working directory
Make sure you pull before committing and merging to stay
in sync! (especially on master, maybe someone else updated it)
33
$ git push
Create a new branch in the remote repository
$ git push -u origin <branch_name>
from the currently checked out branch
Update the remote branch from the local branch afterwards
$ git push
Only changes that are committed are pushed
If the remote and local history diverge (e.g. forgot to pull
before committing) pushes will be rejected
Make sure you push after committing and merging
to stay in sync! 38
The Rest
Other useful commands worth looking up yourself
Quickly stash away your changes for
$ git stash
later to obtain clean working tree
$ git revert Undoing changes and commits
$ git reset —> good tutorial under this [Link]
$ git blame See line for line, who and which commit is
<file> responsible for the last change to this line
Bisect the git history to find which
$ git bisect
commit introduced a bug
35
Summary
config
branch (Not discussed)
init
checkou
status
t merge status
add also staging area
clone
comm
push
it diff
log
pull
diff --staged
log
36
Good Resources
Beginner:
• Bitbucket Tutorials for git
• Resources to learn by Github (includes interactive tutorials)
• Git Documentation
More advanced:
• Pro Git by Scott Chacon and Ben Straub: THE git book (free)
• Good talk: Introduction to git with Scott Chacon of GitHub
• Good talk: Linus Torvalds (creator of git) on git
37
Summary
config
branch (Not discussed)
init
checkou
status
t merge status
add also staging area
clone
comm
push log
it diff diff --staged
pull
log
Online Slides with the hands-on exercises:
CRC Redmine Z02 Project Wiki or Transport Meeting Website
38