introduction to git - luis - leibniz universität it services · pdf fileeveryone who...

39
Introduction to Git Andreas Gerdes Cluster User Group – Jun 02, 2014

Upload: dinhkhue

Post on 16-Mar-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Introduction to Git

Andreas Gerdes

Cluster User Group – Jun 02, 2014

Page 2: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Motivation - Why use version control?

Versions in file names: does this look familiar?$ lsfile file.2 file.keep file.old.2file~ file .20090803 file.new file.savefile.1 file.bak file.old

This is better than nothing, however what happened between thedifferent versions? Which file is actually the most current?

Version control is a way tokeep a backup of changing files

store a history of those changes

and manage merging of changes in versionswith different change sets.

Introduction to Git Andreas Gerdes 2

Page 3: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Use of version control

Who uses version control?

Everyone who would like to access an old version of a document. Oreveryone to whom such things have happened:

It would be nice to have the version from 2 hours ago . . .

I wrote that really well three days ago. How did that go again?

Oh no! I deleted the file!

Where is version control used?

Software development

Text and document processing/writing

Graphic design

System administration

Introduction to Git Andreas Gerdes 3

Page 4: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Use of version control (cont.)

What kinds of files should be kept under version control?

Any kind of file which will be changedMainly text files

Program code, documentationTheses, dissertationsConfiguration files

Binary files also possibleGraphics files: .png, .tiffDocuments: .odt, (.pdf)

What shouldn’t be kept under version control?

Automatically generated files, e.g.: .o, .log, .pdf

Editor backup files, e.g.: file˜, file.bak

Introduction to Git Andreas Gerdes 4

Page 5: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Course prerequisites

Bash

Text editor

Mac: Text Wrangler or Text Mate 2Windows: Notepad++Linux: gedit, leafpad, kate, nano, vim . . .

Git

For CUG meeting June 02, 2014:Start up computers, choose Xubuntu 12.10$ sudo apt -get update$ sudo apt -get install git git -doc$ sudo apt -get install vim

Do not run apt-get upgrade in this course. We do not have the time!Introduction to Git Andreas Gerdes 5

Page 6: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Agenda

1 Git history and design

2 Warm up (configuration and getting help)

3 Basic commands (init, status, add, commit)

4 Explore history and changes (log, diff, show)

5 Going back in time (checkout, reset)

6 Ignore, move and remove files

7 Clone a project

8 References, Part II and Thank You

Introduction to Git Andreas Gerdes 6

Page 7: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Git history and concept

Originally written for Linux kernel development.All Linux kernel developers used to be able to use the proprietaryBitkeeper version control system for free.In 2005 there were further restrictions put on Bitkeeper so that itwasn’t as free as it used to be.Linus Torvalds was uneasy with the situation and decided to writehis own tool.

Basically a versioned file systemThe version control system is developed on top of thisMore than 130 commandsporcelain and plumbing commandsDistributed repository model can be used centrallyVery fast!

Introduction to Git Andreas Gerdes 7

Page 8: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Git design

Distributed development

Scalable up to thousands of developers

Fast and efficient

Maintain integrity and privacy

Enforced responsibility

Immutable objects

Atomic operations

Support and promote development with branches

Complete repositories

Clean design

Free as in freedom

Introduction to Git Andreas Gerdes 8

Page 9: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Get help

git --help

Also possible: man git , git help

$ git --helpusage: git [--version] [--exec -path[=<path >]] [--html -path] [--man -path] [--info

-path][-p|--paginate|--no -pager] [--no-replace -objects] [--bare][--git -dir=<path >] [--work -tree=<path >] [--namespace=<name >][-c name=value] [--help] <command > [<args >]

The most commonly used git commands are:add Add file contents to the indexbisect Find by binary search the change that introduced a bugbranch List , create , or delete branchescheckout Checkout a branch or paths to the working treeclone Clone a repository into a new directorycommit Record changes to the repositorydiff Show changes between commits , commit and working tree , etcfetch Download objects and refs from another repositorygrep Print lines matching a patterninit Create an empty git repository or reinitialize an existing onelog Show commit logsmerge Join two or more development histories togethermv Move or rename a file , a directory , or a symlink[...]

Introduction to Git Andreas Gerdes 9

Page 10: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Configure Git

git config

Important for the history of a file:Who made a change and when?

git config --global user.name "Jon Doe"git config --global user.email "[email protected]"git config --global core.editor "vim"git config --global color.ui True

Introduction to Git Andreas Gerdes 10

Page 11: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Create a (local) repository

git init

Initialize an empty Git repository

$ mkdir myDir$ cd myDir/$ git initInitialized empty Git repository in /home/dog/myDir /.git/$ ls -latotal 36drwxr -xr-x 3 dog dog 4096 Jan 30 02:27 .drwx ------ 57 dog dog 24576 Jan 30 02:27 ..drwxr -xr-x 7 dog dog 4096 Jan 30 02:27 .git$ git status# On branch master## Initial commit#nothing to commit (create/copy files and use "git add" to

track)$

Introduction to Git Andreas Gerdes 11

Page 12: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Exercise: help, config, init (10 minutes)

Explore git help

Configure Git with your name and e-mailCreate a folderInitialize a Git repositoryExplore the .git subdirectoryTell me some interesting thing you find

What happens when you rename the directory where you calledgit init ?

What happens when you delete the .git subdirectory?

What happens when you call git init a second time in a Gitdirectory?Type git config --list in your $HOME directory!

Type git config --list in your Git directory!

Introduction to Git Andreas Gerdes 12

Page 13: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Good to know

Git help works also for specific operations:git help add = man git-add

git help config = man git-config

git help init = man git-init

. . .

You can either use git config <parameter>

or edit the .gitconfig or ./.git/config text files.

The same is true for many other operationsNot every configuration option has (yet)a command line tool to change itGit is still being developed.

Introduction to Git Andreas Gerdes 13

Page 14: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

How Git views content

Staging area / Index

staged = indexed = cached

Repository

untracked

modified

stagedgit add committedgit commit

Introduction to Git Andreas Gerdes 14

Page 15: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

State of a repository

git status

Shows if you have untracked, modifiedor staged (= to be committed) files.

In case of a cloned repository git status also shows if youare ahead of your remote (compared to last sync).

See also: git help status

Introduction to Git Andreas Gerdes 15

Page 16: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Add files to the next commit

git add

Adds files to the staging area.

If a file is added once, Git is aware of it and will show it asmodified instead of untracked if you have made a modification.

A modified file is not automatically staged. You have to callgit add every time you want to commit the changes.

In Git add has a different meaning compared to Subversion.

Here it means: Mark this file to be committed in my next snapshot.

In Subversion add means: Add this file to version control.

Introduction to Git Andreas Gerdes 16

Page 17: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Make a snapshot

git commit

Put files from staging area into the repository.

This command does not care about the stateof the working directory!

You could delete all files and still commit themas long as they were staged.

In Git commit is a local operation.

In Subversion commit alias ci always connects to the (central) server.

Introduction to Git Andreas Gerdes 17

Page 18: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Exercise: status, add, commit (10 minutes)

Create files in your Git repositoryAdd them to the staging areaMake one or more commitsCreate two files, stage and commit only one of them.

Modify the committed fileCall git status(one file should be modified, the other one still untracked)Now: Run git commit -a without staging anything first!What happens?

Can you unstage a file that was added with git add ?Make some changes to one of your files and stage it.

Now: Make some more changesCall git statusWhat do you see?What version of the file is put into the repository when you callgit commit now without staging the file a second time?

Introduction to Git Andreas Gerdes 18

Page 19: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Recommendations for commits

A commit should contain a single, self contained idea.The commit message should contain a short description of theidea or change being made in this commit.

A one line subject lineAn optional text describing more details of the change.The Why of the change is important.Remember: this is a communication exercise.

Commits should be as small as possible (atomic commits).

After the git commit commandThe first line of the commit message is shown

There are no revision numbers. Commits are specified via SHA1hashes. The first part of the SHA1 hash is also shown. This can beused as a reference to the commit.

What else is shown?

Introduction to Git Andreas Gerdes 19

Page 20: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Good to know

Extensions to bash make life easier

export VISUAL=vim

GIT_PS1_SHOWDIRTYSTATE ="true"GIT_PS1_SHOWSTASHSTATE ="true"GIT_PS1_SHOWUNTRACKEDFILES ="true"

if [ -e /etc/bash_completion.d/git ]then. /etc/bash_completion.d/gitexport PS1 = '\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;31m\]$(

__git_ps1 "\[\033[0;31m\] (%s)\[\033[0;31m\]") \[\033[00m\]\$ 'elseexport PS1 = '\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 'fi

Introduction to Git Andreas Gerdes 20

Page 21: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Browse the history

git log

Shows previous commits (message, author, date)

There is also a tig command that provides a nice ncurses interface(has to be installed separately).

Summary view with

git log --oneline

See diffs for a specific file:

git log -p <file >

Introduction to Git Andreas Gerdes 21

Page 22: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Compare versions

git diff

Compares different versions of (text) files.

Default:Version in current working directory vs. last committed version.(other modes possible, see git help diff)

Show the diff between staging area and last commit:

git diff --cached [<path >...]

In other words:git diff --cached shows you what will be committed next.

Show the diff between two arbitrary commits:

git diff <commit > <commit > [<path >...]

Introduction to Git Andreas Gerdes 22

Page 23: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Show versions from the past

git show

Shows Git objects (not only commits)SYNOPSIS

git show [options] <object >...

Example:

git show 9f529a:myfile.txt

Introduction to Git Andreas Gerdes 23

Page 24: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Exercise: log, diff, show (5 minutes)

Add some more files and make more commits.

Use git log to view the history.

Use git log -p <path> to see differences in historyfor one of your files.

Make changes to one file and show the difference with git diff

Add the file and use git diff --cached to see what will becommitted.

Commit the change and and use the -v switch to see what’s tobe committed in your editor.

Use git show to inspect a version of the file from a previouscommit.

Introduction to Git Andreas Gerdes 24

Page 25: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Get older versions of a file back

git checkout

Checkout a branch or paths to the working tree

Example 1:You have unstaged changes and want to discard them:

git checkout myfile.txt

Example 2:You want a file back from a certain commit

git checkout 9f529a:myfile.txt

More on git checkout in Part II when we discuss branching.

Introduction to Git Andreas Gerdes 25

Page 26: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Reset the history

git reset

Reset current HEAD to the specified stateSYNOPSIS

git reset [-q] [<commit >] [--] <paths >...

Example 1:You have staged changes and want to unstage them:git reset <file >

Example 2:You want the state of your repository back, i.e. from yesterday evening:git reset [-- hard] 9f529a

Warning: When you do a reset like this, your history is lost (all commitmessages). You can rewrite history by this.When you use the (optional) --hard switch your working tree is also reset!

Introduction to Git Andreas Gerdes 26

Page 27: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Exercise: checkout, reset (5 minutes)

Step 1: Create 5 files in your directory withone line of content in each file.

Step 2: Commit the files to the repository

Step 3: Change 2 of the 5 files and commit them.

Step 4: Undo the changes in step 3 using git reset .

Step 5: Print out the last entry in the log.

Introduction to Git Andreas Gerdes 27

Page 28: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Ignore files

The .gitignore file

Can be in the root or any sub directory of your Git repository.

Example:$ cat .gitignore*.aux*.log*.nav*.out*.snm*.toc*.vrbgit_intro.pdf$

We have already seen another file that we could use!(./.git/info/exclude)

Introduction to Git Andreas Gerdes 28

Page 29: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Move files within your repository

git mv

Put git in front of the mv command to make Git aware of thename change.

Example:$ git mv file3 file5$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file >..." to unstage)## renamed: file3 -> file5#$ git commit -m "Renamed the file , because ..."

Introduction to Git Andreas Gerdes 29

Page 30: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Remove files from version control

git rm

Put git in front of the rm command to make Git aware of theremoval.

The command will remove the file from the working directory!For --cached option, see: git help rm

Example:$ git rm file5rm 'file5 '$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file >..." to unstage)## deleted: file5#$ git commit -m "Deleted old file. Not needed by the project

any more."

Introduction to Git Andreas Gerdes 30

Page 31: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Exercise: ignore, rename and remove (5 minutes)

Add some files with extension .log. Do not stage them

Add a .gitignore file to ignore these files.

Git will not care about logfiles any more.

The content of .gitignore may change over time.

Why not put it into version control, too?Ñ git add .gitignore; git commit

Rename and remove some of your files.Do it with Git and without Git.

Introduction to Git Andreas Gerdes 31

Page 32: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Clone a project

git clone

Clones a repository from some other location.Many protocols are supported. Despite to git init the clonedrepo may not be empty. Use git log to see the history.

Example:From Github

git clone https :// github.com/purepitch/git_course.git

Introduction to Git Andreas Gerdes 32

Page 33: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Pull changes from a remote

git pull

Fetch from and integrate with another repository or a localbranch.

Incorporates changes from a remote repository into the currentbranch. In its default mode, git pull is shorthand for git fetch

followed by git merge .

If you have not much experience with branches (see Part II of thiscourse) and just work on one branch (default: master) with one remote(default: origin), you can use git pull for your convenience.

Sometimes after a pull Git will throw you into an editor with a mergecommit message already prepared. Just save this one and commit thechange locally. If there is a conflict Git cannot resolve itself, it willinform you about it and tell you what to do.

Introduction to Git Andreas Gerdes 33

Page 34: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Push changes to a remote

git push

Update the remote server with changes you have made to yourlocal repository.

You should always do a git pull first, before you try to push.Make this a habit!

At some point you might experience that your push gets rejected by theserver. Don’t worry! Try a git pull and read what Git tries to tellyou. When all conflicts are resolved, the push should work.

If not: Contact your Git server administrator to check if you haveindeed write permission to the repository.

Introduction to Git Andreas Gerdes 34

Page 35: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Exercise: clone, pull and push (10 minutes)

Create an account on Github

Fork the repository:https://github.com/purepitch/git_course.git(online via the web interface)

Clone your fork to your local hard drive

Make some changes and push them

(optional) Give your neighbor write access to your repo

(optional) Make changes, push and pull them.

(optional) Create merge conflicts that will reject a push and haveto be resolved with a pull.

Introduction to Git Andreas Gerdes 35

Page 36: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Git commands covered today

git

confighelp init

status add commit

log diff show

clone pull push

checkout reset

mv rm

Introduction to Git Andreas Gerdes 36

Page 37: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

For further study

Git reference site:http://gitref.orgWebsite of the Git project:http://git-scm.comHere you can find the very good Pro Git book (PDF):http://git-scm.com/bookGit introduction (Katy Huff)http://www.youtube.com/watch?v=T0BE9ApIegcMore advanced Git introduction (Scott Chacon)http://www.youtube.com/watch?v=ZDR433b0HJY(Scott explains the SHA1 hashes in more detail. Furthermore heshows how Git ’thinks’ about version control compared toSubversion. He introduces branching very early - a must see talk!)Tech Talk: Linus Torvalds on Git (very funny)http://www.youtube.com/watch?v=4XpnKHJAok8

Introduction to Git Andreas Gerdes 37

Page 38: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Part II: Advanced Git knowledge

Git objects and hashes in detail

Stage only parts of a file: patch mode for git addgit add -p

Work with (local) branches: create, delete and merge

Resolve (merge) conflicts

More on remotes: What happens when you clone?

Fetch and merge remote branches, git pull

git stash

git tag

git rebase

Workflows in version control: distributed vs. central

Introduction to Git Andreas Gerdes 38

Page 39: Introduction to Git - LUIS - Leibniz Universität IT Services · PDF fileEveryone who would like to access an old version of a document. Or ... For CUG meeting June 02, 2014: ... idea

Thank you

Comic source:http://geek-and-poke.com

Introduction to Git Andreas Gerdes 39