Upgrade to Pro — share decks privately, control downloads, hide ads and more …

git - eine praktische Einführung

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

git - eine praktische Einführung

Eine praktische Einführung in git.

Avatar for Marcel Eichner

Marcel Eichner

June 13, 2012
Tweet

Other Decks in Programming

Transcript

  1. Git ([ɡɪt], engl. Blödmann) ist eine freie Software zur verteilten

    Versionsverwaltung von Dateien, die von Linus Torwvalds ursprünglich für die Quellcode-Verwaltung des Linux-Kernels entwickelt wurde.
  2. Wieso git? • Klein und schnell! • Schnelles Branching &

    Merging • Verteilte Repositories (indirekter Backup) • Freie, offene Software • Daten-Sicherheit durch Content-Hashes http://git-scm.com/about/
  3. Geschwindigkeit • (fast) Alle Operationen lokal • .git Verzeichnis nur

    auf root Ebene • http://git-scm.com/ about/small-and-fast
  4. Dezentralität • Klon des kompletten Repositories lokal • ermöglicht andere

    Work ows • lokales branchen, commiten, reverten etc.
  5. Hashes statt Nummern • dezentrale Architektur erlaubt keine fortlaufende Revions-Nummerierung

    • Inhalte zählen mehr als Struktur • Identi zierung einzelner Commits über Content-Hashes (SHA-1)
  6. # ~/.gitconfig [user] name = Marcel Eichner email = [email protected]

    $ git config --global user.name "Marcel Eichner" $ git config --global user.email "[email protected]" Kon guration Einstellung für Identi kation in git
  7. $ mkdir myrepo $ cd myrepo $ git init Initialized

    empty Git repository in ../myrepo/.git Repository erstellen Lokales Repository erstellen
  8. # On branch master # # Initial commit # #

    Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: index.html # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: index.html $ git status git status Anzeigen aller geänderten und gelöschten Dateien
  9. Staging $ git add index.html $ git add *.html $

    git add --all Alle Änderungen stagen (inklusive gelöschter Dateien) Eine oder mehrere bestimmte Dateien stagen
  10. [master 7626937] changed 1 files changed, 1 insertions(+), 1 deletions(-)

    $ command $ git commit -m "My first commit" Commit Änderungen in Stage mit einer Nachricht ins lokale Repo comitten
  11. $ git commit -a -m "My Message" [master 7626937] changed

    1 files changed, 1 insertions(+), 1 deletions(-) Shortcut: Stage & Commit Alle Änderungen stagen und sofort commiten $ git commit --amend Letzte Commit-Message editieren
  12. Cloning into 'mydir'... remote: Counting objects: 3, done. remote: Total

    3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. $ git clone [email protected]:testing.git mydir Remote Repo klonen git remote repository in den Ordner "mydir" klonen
  13. # Counting objects: 5, done. # Writing objects: 100% (3/3),

    272 bytes, done. # Total 3 (delta 0), reused 0 (delta 0) # To [email protected]:testing.git # edfec50..2fc284e master -> master $ git push origin master Commits hochschieben Alle commits zum Remote pushen $ git push [remote-name] [remote-branch-name] Allgemeine Syntax
  14. $ git branch my-rad-feature $ git checkout my-rad-feature Switched to

    branch 'my-rad-feature' Branching $ echo "My Branch is different" > index.html $ git commit -a -m "changed content to my branch" Branch anlegen und auschecken
  15. $ git branch master * my-rad-feature Branches listen master *

    my-rad-feature remotes/origin/HEAD -> origin/master remotes/origin/master Alle verfügbaren lokalen branches anzeigen $ git branch -a Alle verfügbaren branches anzeigen (lokal & remote)
  16. $ git push origin my-rad-feature Counting objects: 6, done. Delta

    compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (6/6), 482 bytes, done. Total 6 (delta 0), reused 0 (delta 0) To [email protected]:testing.git * [new branch] my-rad-feature -> my-rad-feature Branch -> Remote "my-rad-feature"-Branch ins Remote schieben
  17. $ git pull remote: Counting objects: 7, done. remote: Compressing

    objects: 100% (4/4), done. remote: Total 4 (delta 2), reused 0 (delta 0) Updating 361303d..f2cd831 Fast forward index.html | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) Änderungen holen Alle Änderungen vom Repository holen
  18. # assume we are in branch "my-rad-feature" and everything commited

    $ git checkout master Switched to branch 'master' Branch Merging $ git merge my-rad-feature Updating edfec50..2bc1785 Fast-forward index.html | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) Änderungen sind noch nicht auf dem Remote Server. Damit sie dort landen muss man immer noch einmal git push origin master ausführen! In den Merge-Ziel-Branch wechseln und mergen
  19. $ git remote add origin [email protected]:testing.git $ git push origin

    master Counting objects: 3, done. Writing objects: 100% (3/3), 231 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:testing.git * [new branch] master -> master git remote Locales Repository mit dem Remote verbinden
  20. $ git log commit edfec504eb864dc667f3f5b9d301617036d15f3a Author: Marcel Eichner <[email protected]> Date:

    Thu Jun 7 15:37:26 2012 +0200 My First Commit Log Log des gesamten Repositories anzeigen
  21. $ git log --graph Log Extended Anzeige des Baum-graphen im

    Log $ git log --before="2 weeks ago" --after="2012-04-01" --author="Marcel Eichner" Filter nach Zeit & Autor $ git log --grep="fixed" commit message lter $ git log -p index.html Log einer Datei mit Inhalt Merge
  22. 2fc284e5 (Marcel Eichner 2012-06-07 15:43:47 +0200 1) Hello World it’s

    me! $ git blame index.html blame Blame für eine Datei Revision Hash Committer Datum der Änderung $ git blame index.html [REVISION] Blame zu einem bestimmten Zeitpunkt
  23. $ git [command] --help NAME git-blame - Show what revision

    and author last modified each line of a file SYNOPSIS git blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>] [--abbrev=<n>] [<rev> | --contents <file> | --reverse <rev>] [--] <file> DESCRIPTION Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision. The command can also limit the range of lines annotated. help
  24. $ git stash Zwischenablage Alle Änderungen in die Zwischenablage des

    lokalen git Repositories schieben $ git stash apply Änderungen wieder zurückholen $ git stash list Liste aller Objekte in Zwischenablage $ git stash apply stash@{1} Spezi schen Eintrag (2. Eintrag) aus Zwischenablage wieder herstellen
  25. $ cat .gitignore # specific files in all directories .DS_Store

    # wildcards *.tmp # ignore whole directories tmp/**/* Ignorieren $ git config --global core.excludesfile ~/.gitignore globale .gitignore Projekt-Spezi sche ignores
  26. Sub-Modules • Vergleichbar mit Subversion- Externals • Geklontes Submodule verhält

    sich wie ein eigenes Git Repository • DRY (Komponenten, Plugins etc.)
  27. $ git submodule add https://github.com/jeresig/jquery.hotkeys.git public/js/vendor/jquery.hotkeys.git Cloning into 'public/js/vendor/jquery.hotkeys.git'... remote:

    Counting objects: 171, done. remote: Compressing objects: 100% (79/79), done. remote: Total 171 (delta 104), reused 155 (delta 91) Receiving objects: 100% (171/171), 170.27 KiB | 204 KiB/s, done. Resolving deltas: 100% (104/104), done. Submodul hinzufügen
  28. Hook-Beispiele • E-Mail-Noti cation bei Push • IRC / Jabber-Nachrichten

    bei Push • PHPUnit Tests automatisch ausführen beim Commit • PHPLint Syntax-Check beim Commit
  29. git-svn • Subversion Repository mit git nutzen • Geschwindigkeit von

    git für SVN repositories • Merging, Branching wie man es aus git gewohnt ist
  30. Internet-Hilfe • Git-Homepage http://git-scm.com • Git-Kurs http://git.or.cz/course/ • Every day

    git http://mgorski.net/2011/dev/everyday-git • Git Ready http://gitready.com/