Git Power-Workshop

Post on 19-May-2015

1.053 views 2 download

Tags:

description

Interner Git-Power-Workshop am Lehrstuhl für Informatik III Dauer: 2,45 h Teilnehmer hatten die Gelegenheit, Gelerntes direkt am eigenen Laptop auszuprobieren.

Transcript of Git Power-Workshop

www3.informatik.uni-wuerzburg.de

Institute of Computer Science Chair of Communication Networks

Prof. Dr.-Ing. P. Tran-Gia

Git Crashkurs Steffen Gebert

steffen punkt gebert ät informatik punkt uni-wuerzburg punkt de

Steffen Gebert 2 2

Warum Git?

u  Git ist hipp! (u.a. wegen Github) u  Git ist schnell – vieles geht lokal u  Git ermöglicht bessere Zusammenarbeit / lokale Abweichungen u  Arbeit in Branches funktioniert u  uvm. (als Text sicher nicht soo überzeugend J)

Steffen Gebert 3 3

GIT INTERNALS

Steffen Gebert 4 4

Git-Datenstrukturen

u  Objekte identifiziert über SHA-1 hashes u  Blob: zB Inhalt einer Datei

§  Ohne Dateiname §  Ohne Meta-Information

Vieles geklaut von: http://eagain.net/articles/git-for-computer-scientists/

Steffen Gebert 5 5

Git-Datenstrukturen

u  Tree-Objekt verknüpft Meta-Informationen mit Datei- und Verzeichnisinhalten §  Beschreibt, welche Dateien mit welchen Inhalten existieren

http://www.slideshare.net/chacon/getting-git

Steffen Gebert 6 6

Git-Datenstrukturen

u  Commit als Pointer auf einen Tree

u  SHA-1 des Commits (u.a.) aus SHA-1 des Trees gebildet §  Integrität gewährleistet

Steffen Gebert 7 7

Git-Datenstrukturen

u  Branch als Pointer auf einen Commit §  Menschen-les/merkbar

u  HEAD als Pointer auf Stand des eigenen Working Trees („Checkout“)

Steffen Gebert 8 8

Das .git-Verzeichnis

u  Enthält alle Meta- und History-Informationen

$  ls  -­‐l  .git  total  1028  -­‐rw-­‐r-­‐-­‐r-­‐-­‐    1  st            23  Feb  28  14:44  HEAD  drwxr-­‐xr-­‐x    2  st            68  Feb  28  14:43  branches  -­‐rw-­‐r-­‐-­‐r-­‐-­‐    1  st          295  Feb  28  14:44  config  -­‐rw-­‐r-­‐-­‐r-­‐-­‐    1  st            73  Feb  28  14:43  description  drwxr-­‐xr-­‐x  10  st          340  Feb  28  14:43  hooks  -­‐rw-­‐r-­‐-­‐r-­‐-­‐    1  st  1019488  Feb  28  14:44  index  drwxr-­‐xr-­‐x    3  st          102  Feb  28  14:43  info  drwxr-­‐xr-­‐x    4  st          136  Feb  28  14:44  logs  drwxr-­‐xr-­‐x    4  st          136  Feb  28  14:43  objects  -­‐rw-­‐r-­‐-­‐r-­‐-­‐    1  st      19379  Feb  28  14:44  packed-­‐refs  drwxr-­‐xr-­‐x    5  st          170  Feb  28  14:44  refs  

blobs, commits

branches, tags

config

Aktueller Commit

Steffen Gebert 9 9

Das .git-Verzeichnis

u  cat  .git/HEAD  ref:  ref/heads/master  

u  cat  .git/refs/heads/master    60117b50a7ad22ba2be2c2b1c1b282bc3649fd02  

u  git  cat-­‐file  –p  60117b50a7ad22ba2be2c2b1c1b282bc3649fd02  tree  0d55691a8e7b252c1a4b04568619fafaaf9ea6e3  parent  72cc822724effb930feff2f1b8c1bb5c3651fdde  author  Steffen  Gebert  <steffen.gebert@typo3.org>  1362056598  +0100  committer  Steffen  Gebert  <steffen.gebert@typo3.org>  1362056598  +0100    Here  comes  the  comit  message  

u  git  cat-­‐file  –p  0d55691a8e7b252c1a4b04568619fafaaf9ea6e3  100644  blob  40c7064c2a5f56e6f3129616f11448c87e444ff8  README.md  

u  git  cat-­‐file  –p  40c7064c2a5f56e6f3129616f11448c87e444ff8  Willkommen  …  

Branch: master

„trunk“ bei SVN

Commit

Tree

Vorheriger Commit

Steffen Gebert 10 10

GIT BENUTZEN

Steffen Gebert 11 11

Lokales Repository anlegen

u  mkdir  myrepo  

u  cd  myrepo  

u  git  init  

Initialized  empty  Git  repository  in  myrepo/.git/  

u  ls  –a  

.    ..    .git  

Steffen Gebert 12 12

Initiales Setup

u  git  config  -­‐-­‐global  user.name  „John  Doe“  u  git  config  -­‐-­‐global  user.email  „example@uni-­‐wuerzburg.de“  

u  Wird wegen -­‐-­‐global in ~/.gitconfig gespeichert u  Ansonsten in .git/config  (repo-spezifisch)

u  git  config  -­‐-­‐global  color.ui  auto  u  Show current branch in prompt:

http://wiki.typo3.org/Tips_and_Tricks_(Git)#Current_branch_in_prompt

Steffen Gebert 13 13

Datei erstellen

u  echo  Willkommen  >  README.md  u  git  status  

 #  On  branch  master  #  #  Initial  commit  #  #  Untracked  files:  #      (use  "git  add  <file>..."  to  include  in  what  will  be  committed)  #  #  README.md  nothing  added  to  commit  but  untracked  files  present  (use  "git  add"  to  track)  

Immer lesen, was Git uns sagt! (hat tendenziell gute Ideen)

zum Index hinzufügen

Steffen Gebert 14 14

Datei dem Index hinzufügen

u  git  add  README.md    

u  git  status    #  On  branch  master  #  #  Initial  commit  #  #  Changes  to  be  committed:  #      (use  "git  rm  -­‐-­‐cached  <file>..."  to  unstage)  #  #  new  file:      README.md  #  

Steffen Gebert 15 15

Commit erstellen

u  git  commit    

u  export  GIT_EDITOR=nano  

u  git  log  u  git  log  –p  

u  Abkürzung  git  add  +  git  commit:  git  commit  -­‐a  

Steffen Gebert 16 16

Lebenszyklus einer Datei

u  untracked (nicht versioniert) u  unmodified (versioniert, aber nicht verändert) u  modified (versioniert, verändert, nicht im Stage) u  staged (versioniert, verändert und im Stage, aber nicht commited)

git add

http://bit.ly/PHPUG_JUN_GITvsSVN

Steffen Gebert 17 17

Änderungen Rückgängig machen

u  Uncommittet: git  checkout  -­‐-­‐  README.md  

u  Add rückgängig machen: §  git  reset  HEAD  README.md  

u  Generell: Lesen, was git  status  vorschlägt

Steffen Gebert 18 18

BRANCHES

Steffen Gebert 19 19

Mit Branches arbeiten

u  Jede Arbeit sollte in einem Feature-Branch erledigt werden u  git  branch  myfeature  u  git  checkout  myfeature  

(oder git  checkout  -­‐b  myfeature)

u  git  commit  u  Noch einer (und noch einer)

u  git  checkout  master  u  git  merge  myfeature  u  Resultiert in Fast-Forward

Steffen Gebert 20 20

Merges

u  git  checkout  -­‐b  myfeature2  

u  Neue Datei erstellen,  git  commit  

u  git  checkout  master  

u  Neue Datei erstellen,  git  commit  

u  git  merge  myfeature2  

u  Resultiert in Merge commit

Steffen Gebert 21 21

ARBEITEN MIT REMOTES

Steffen Gebert 22 22

Remote Repositories

u  GIT ist dezentrales Versionskontrollsystem (DVCS) §  viele Aktionen ohne Netzwerkkommunikation möglich §  es gibt nicht den einen zentralen Server

fetc

h / p

ull

push

Steffen Gebert 23 23

Protokolle u  file:// u  git://

§  nur pull, kein push u  ssh://

§  push + pull §  Repository im normalen Dateisystem §  Auth über System

u  http(s):// (SmartHTTP) §  push + pull §  Auth über Passwort, kann über Credential Helper gecached werden

https://help.github.com/articles/set-up-git#password-caching

u  Performance-technisch kaum Unterschiede

Steffen Gebert 24 24

Repository clonen u  git  clone  ≙  svn  checkout  u  git  clone  https://dike.informatik.uni-­‐wuerzburg.de/git/test/workshop.git  

u  git  remote  –v  origin  https://dike.informatik.uni-­‐wuerzburg.de/git/test/workshop.git  (fetch)  

origin  https://dike.informatik.uni-­‐wuerzburg.de/git/test/workshop.git  (push)  

u  cat  .git/config  [remote  "origin“]  

 fetch  =  +refs/heads/*:refs/remotes/origin/*    url  =  https://dike.informatik.uni-­‐wuerzburg.de/git/test/workshop.git  

     [branch  "master"]    remote  =  origin    merge  =  refs/heads/master    rebase  =  true  

Default-Name

Steffen Gebert 25 25

Arbeiten mit Remotes

u  git  pull  = git  fetch;  git  rebase  u  git  fetch  -­‐-­‐all  holt Änderungen aller Remotes u  git  rebase  bringt aktuellen Branch vorwärts (auf Stand des

getrackten Branch) u  git  fetch  -­‐-­‐tags  holt auch Tags u  git  log  

§  HEAD..origin/master  listet Unterschiede von HEAD zu o/m §  origin/master..HEAD  listet Unterschiede von o/m zu HEAD

Steffen Gebert 26 26

Pushen

u  Neuen oder andersnamigen Branch pushen: §  git  push  <remote>  <branch>  

u  Alle lokalen und remote existierenden <branch>  nach  <remote>/<branch> pushen §  git  push  

Steffen Gebert 27 27

Branches auschecken

u  git  branch  -­‐a  listet alle bekannten Branches auf u  git  checkout  <zielbranch> wechselt Branch

u  git  checkout  -­‐b  <new_branch>  erstellt Branch basierend auf HEAD  

u  Tracking branches §  Hält „Verbindung“ des lokalen Branches mit remote Branch §  git  pull  weiß sonst nicht, was rebased werden soll / push wohin gepusht §  git  checkout  -­‐b  -­‐-­‐track  <new_branch>  <remote>/<branch>  §  Nachträglich:  git  -­‐-­‐set-­‐upstream  <branch>  <remote>/<branch>

u  Detached head (ausgelöst durch git  checkout  <remote>/<branch>) §  Arbeiten losgelöst von jeglichen Branches

Branch erstellen

Steffen Gebert 28 28

Remote hinzufügen

u  git  remote  add  github  https://github.com/lsinfo3/workshop.git  u  git  fetch  -­‐-­‐all  

Fetching  origin  Fetching  github  

u  git  branch  -­‐av  *  master                                a97d21d  Explain  URLs  better      remotes/github/master  a97d21d  Explain  URLs  better      remotes/origin/HEAD      -­‐>  origin/master      remotes/origin/master  a97d21d  Explain  URLs  better  

Name des neuen Remotes

aktueller Branch

Steffen Gebert 29 29

Eigenen Branch pflegen u  git  checkout  –b  TYPO3_4-­‐5-­‐rzuw  u  git  commit  u  git  fetch  -­‐-­‐all  u  git  log  HEAD..typo3org/TYPO3_4-­‐5  u  git  log  typo3org/TYPO3_4-­‐5..HEAD  

u  git  merge  origin/TYPO3_4-­‐5  

u  git  rebase  ebenfalls möglich, aber nur lokal ungefährlich!

u  git  cherry-­‐pick  holt Commit(s) aus anderem Branch

Steffen Gebert 30 30

Git Tipps u  Essenziell

§  Shell completion, $PROMPT einrichten (branch anzeigen, show dirty state) §  git  stash, die Zwischenablage §  git  alias  (git  alias.st  status) §  ~,  ~2,  ^  etc. als Referenzen auf parent commits (git  show  HEAD~25)

u  Nett §  git  bisect  zum finden „böser“ Commits §  git  svn  zum Arbeiten mit SVN repositories §  submodules (wie svn:external)

u  Lesen §  „Pro Git“ (http://git-scm.com/book, kostenlos) §  AlBlue‘s „Git Tip of the Week“

http://alblue.bandlem.com/Tag/gtotw/

Steffen Gebert 31 31

Vorsicht! u  Niemals tun!

§  git  commit  -­‐-­‐amend  oder git  rebase  schon veröffentlichter Änderungen

§  git warnt, push lässt sich mit -­‐-­‐force  erzwingen

u  Aufpassen! §  Mit git  reset  -­‐-­‐hard Branch auf anderen SHA setzen

–  Niemals danach pushen! –  Verlorene Commits können nur über git  reflog  wiedergeholt werden