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

Git a life

Git a life

Introduction to git: basic commands, merge and rebase examples, alias.

weLaika

May 05, 2014
Tweet

More Decks by weLaika

Other Decks in Programming

Transcript

  1. http://git-scm.com/book File Status Lifecycle unmodified modified staged untracked stage file

    commited edit file stage file commit file has been committed
  2. $ git status # On branch master # # Initial

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

    commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README.md # new file: hello.rb #
  4. $ git commit -m "first commit" [master (root-commit) c5f4b1e] first

    commit 0 files changed create mode 100644 README.md create mode 100644 hello.rb
  5. $ git status # On branch master # 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: README.md # no changes added to commit (use "git add" and/or "git commit -a")
  6. $ git diff diff --git a/README.md b/README.md index e69de29..75c0a6b 100644

    --- a/README.md +++ b/README.md @@ -0,0 +1 @@ +I added this line!
  7. $ git status # On branch master # Changes to

    be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: README.md #
  8. $ git status # On branch master # Changes to

    be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: README.md #
  9. $ git status # On branch master # 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: README.md # no changes added to commit (use "git add" and/or "git commit -a")
  10. C1 C2 C3 c0c6741a C4 485477ce 727987e1 da2f1cd5 master HEAD

    HEAD^ HEAD^^ HEAD^^^ HEAD~ HEAD~2 HEAD~3
  11. C1 C2 C3 c0c6741a C4 485477ce 727987e1 da2f1cd5 master HEAD

    Changes in da2f1cd5 are now in your working directory You didn't lose any data
  12. C1 C2 C3 c0c6741a C4 485477ce 727987e1 da2f1cd5 master HEAD

    Working directory has been restored to 727987e1 All changes in da2f1cd5 are lost.
  13. $ git reflog 727987e1 HEAD@{0}: reset: moving to HEAD^ da2f1cd5

    HEAD@{1}: commit: fourth 727987e1 HEAD@{2}: commit: third 485477ce HEAD@{3}: commit: second c0c6741a HEAD@{4}: commit (initial): first C1 C2 C3 c0c6741a C4 485477ce 727987e1 da2f1cd5
  14. $ git reset --hard HEAD@{1} HEAD is now at da2f1cd5

    fourth C1 C2 C3 c0c6741a C4 485477ce 727987e1 da2f1cd5 master HEAD
  15. $ git merge feature1 Auto-merging index.html CONFLICT (content): Merge conflict

    in index.html Automatic merge failed; fix conflicts and then commit the result. $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: index.html no changes added to commit (use "git add" and/or "git commit -a")
  16. <<<<<<< HEAD <div id="footer">TCS al TAG Torino</div> ======= <div id="footer">

    TorinoCodingSociety - TAG Torino </div> >>>>>>> feature1
  17. C1 C2 master feature 1 C3 C4 C5 C6 C3'

    C4' HEAD $ git rebase master Before
  18. C1 C2 master feature 1 C3 C4 C5 C6 C3'

    C4' HEAD $ git checkout master
  19. C1 C2 master feature 1 C3 C4 C5 C6 C3'

    C4' HEAD $ git merge feature1 (Fast Forward) Before
  20. pick 3b9556b C3 Commit pick a9f5eb5 C4 Commit # Rebase

    59ec99a..a9f5eb5 onto 59ec99a # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. #
  21. pick 3b9556b C3 Commit squash a9f5eb5 C4 Commit # Rebase

    59ec99a..a9f5eb5 onto 59ec99a # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. #
  22. $ git clone [email protected]:delphaber/skuby.git $ git remote -v origin [email protected]:delphaber/git-a-life.git

    (fetch) origin [email protected]:delphaber/git-a-life.git (push) $ git remote add upstream [email protected]:welaika/skuby.git $ git remote -v origin [email protected]:delphaber/skuby.git (fetch) origin [email protected]:delphaber/skuby.git (push) [email protected]:welaika/skuby.git (fetch) [email protected]:welaika/skuby.git (push)
  23. C1 C2 master origin/master C3 $ git push -u origin

    feature2 feature2 HEAD C5 C4 origin/feature2 Before
  24. C1 C2 master C3 feature2 HEAD C5 C4 origin/feature2 C7

    C6 origin/master C6 and C7 are my colleague's commits. My git repository knowns nothing about them at the moment.
  25. C1 C2 master C3 feature2 HEAD C5 C4 origin/feature2 C7

    C6 origin/master C6 and C7 are my colleague's commits. My git repository knowns nothing about them at the moment. $ git checkout master
  26. C1 C2 master C3 feature2 HEAD C5 C4 origin/feature2 C7

    C6 C6 and C7 are my colleague's commits. My git repository knowns nothing about them at the moment. $ git merge feature2 origin/master
  27. C1 C2 master C3 feature2 HEAD C5 C4 origin/feature2 C7

    C6 C6 and C7 are my colleague's commits. My git repository knowns nothing about them at the moment. origin/master
  28. C1 C2 master C3 feature2 HEAD C5 C4 origin/feature2 C7

    C6 origin/master $ git merge origin/master C8
  29. $ git stash $ git stash list stash@{0}: WIP on

    master: 5716ab8 fix bug $ git stash pop # useful alternatives $ git stash save --keep-index "comment" $ git stash save --include-untracked "comment"
  30. $ git add --patch . diff --git a/.gitignore b/.gitignore index

    f2c1360..d6c8ae0 100644 --- a/.gitignore +++ b/.gitignore @@ -30,5 +30,4 @@ build/ # .ruby-version # .ruby-gemset -# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: -.rvmrc + Stage this hunk [y,n,q,a,d,/,e,?]?
  31. $ git log --oneline --decorate --graph --branches --remotes -20 *

    5716ab8 (HEAD, tag: v1.0.0, origin/master, origin/HEAD, master) Merge branch 'feature1' |\ | * bd7e8aa (origin/feature1, feature1) commit | * a9f5eb5 commit |/ | * 38173a0 (feature2) commit | * b5499c7 commit | * 1814c66 (origin/feature2) commit | * 5bb5b65 commit |/ * 3b9556b commit * 59ec99a commit * 422c3ca commit * 8e7a219 Initial commit
  32. $ git log --oneline --decorate --graph --branches --remotes --simplify- by-decoration

    * 5716ab8 (HEAD, tag: v1.0.0, origin/master, origin/HEAD, master) Merge branch 'feature1' * bd7e8aa (origin/feature1, feature1) commit | * 38173a0 (feature2) commit | * 1814c66 (origin/feature2) commit |/ * 8e7a219 Initial commit
  33. $ git log --until=1.minute.ago $ git log --since=2.days.ago $ git

    log --since=1.hour.ago $ git log --since=1.month.ago --until=2.weeks.ago $ git log --since=2014-04-24 --until=2014-04-28
  34. [user] … [alias] addp = add -p . br =

    branch co = checkout ci = commit st = status unstage = reset HEAD -- unchange = checkout -- # log l = log --oneline --decorate --graph --branches --remotes -20 ll = log --oneline --decorate --graph --stat ls = log --oneline --decorate --graph --branches --remotes --simplify- by-decoration # list aliases al = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /'
  35. [core] excludesfile = ~/.gitignore_global [pull] rebase = true [color] ui

    = auto [color "branch"] … [color "diff"] … [color "status"] …