Talk to git


Published on

A summarize and re-categorize of the git tutorial.

Published in: Self Improvement, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • How to collaboration?
  • Talk to git

    1. 1. Talk to Git Jim Chen
    2. 2. VCS and Git
    3. 3. Why Version Control? •History is important. •Time travel is possible! o Roll back to old version •Collaboration o Contributors can work at the same time.
    4. 4. Local Version Control System •Only on your PC
    5. 5. Centralized Version Control System •Server owns all versions. •Clients only have one version.
    6. 6. Distributed Version Control System •Every one has a full copy •Recovery friendly
    7. 7. History In 2005, relationship between Linux Kernel community and commercial company of BitKeeper broken down for reasons.
    8. 8. Goals of Git •Speed •Simple design •Strong support for non-linear development (branches over the world) •Fully distributed •Able to handle large projects efficiently (Linux kernel is really huge)
    9. 9. Git Basics
    10. 10. Snapshots, Not Differences
    11. 11. Nearly Operations are local •Full copy in local computer •Make changes but can keep update with remote •Network independent
    12. 12. Integrity •Check-sum based tracking •SHA-1 Hash function •40-characters
    13. 13. Generally Only Adds Data •Everything committed will be a snapshot in Git history •Even another commit remove the file.
    14. 14. Three States •Committed •Modified •Staged
    15. 15. Welcome Aboard
    16. 16. Get Repository •Initialize in existing directory $ git init $ git add *.sh $ git commit -m "Initial project version" •Clone from existing repository $ git clone <url>
    17. 17. File Status untracked unmodified modified staged add the file remove the file git add <file> git rm <file> commit stage the file edit the file git add <file> git commit git reset HEAD <file> git checkout <file>
    18. 18. Checking Status $ git status
    19. 19. Tracking New File $ touch README $ git status
    20. 20. Tracking New File $ git add README $ git status
    21. 21. Staging modfied Make some change in $ git status
    22. 22. Staging modfied $ git add $ git status
    23. 23. Viewing Unstaged Changes $ vim $ git diff
    24. 24. Viewing Staged Changes $ git diff --cached
    25. 25. Discard modified $ git status $ git checkout $ git status
    26. 26. Unstage staged $ git reset HEAD
    27. 27. Commit It $ git commit
    28. 28. Guidelines to Commit No whitespaces $ git diff --check • Try to make each commit a logically separate change set. • Getting in the habit of creating quality commit messages. o This would help others to review.
    29. 29. More commands •Removing files $ git rm <file> •Moving files $ git mv <file>
    30. 30. Viewing History in CLI $ git log
    31. 31. Viewing History in GUI $ gitk
    32. 32. Oops! Something missed. But committed already...
    33. 33. Change Last Commit $ git commit --amend
    34. 34. Working with Remote
    35. 35. Clone Repository $ git clone <url> <folder>
    36. 36. Showing Remotes $ git remote -v
    37. 37. Adding and Removing Remotes $ git remote add <remote-name> <url> $ git remote rm <remote-name>
    38. 38. Fetching and Pulling from Remotes $ git fetch <remote-name> $ git pull <remote-name> = git fetch + git merge
    39. 39. Pushing changes to Remote $ git push <remote-name> <branch-name>
    40. 40. Branching
    41. 41. Why branches? •Stabilize stables •Make topics no bother
    42. 42. Data Structure •Blob o Storing file data •Tree o Structure of project, just like directory •Commit o A pointer to a single tree, with some meta-data •Tag o Mark a commit as special
    43. 43. Commit
    44. 44. Versions
    45. 45. A lightweight movable pointer to one of the commits. Branch in Git is HEAD is a special pointer to the current working branch you are.
    46. 46. I want to resolve a topic
    47. 47. $ git branch topic1 $ git checkout topic1 C0 C1 C2 master HEAD topic1 HEAD
    48. 48. $ git checkout -b topic1 C0 C1 C2 master HEAD topic1 HEAD
    49. 49. Commit new change on topic1 C0 C1 C2 master topic1 C3 topic1
    50. 50. Urgent!! Bug fix for master
    51. 51. Create hotfix branch from master for fix bug (C4) $ git checkout master $ git checkout -b hotfix Fix bug and commit change C0 C1 C2 master topic1 C3 C4 hotfix Quiz: Where is the HEAD? HEAD
    52. 52. Merge branches hotfix is good for release
    53. 53. Merge Mechanism •Fast-forward Branches are in the same history flow so Git can only change the pointer to the new one. •Three-way merge – Locate the common ancestor – Git automatically calculate how to merge – Create a merge commit recording new snapshot
    54. 54. Fast-forward $ git checkout master $ git merge hotfix Updating ... Fast forward C0 C1 C2 master topic1 C3 C4 hotfix master
    55. 55. Three-way merge $ git checkout master $ git merge topic1 Merge made by recursive C0 C1 C2 topic1 C3 C4 master C5 Snapshot to merge into Snapshot to merge into Common Ancestor C6 master
    56. 56. Gosh... Conflict happens
    57. 57. What will you see when merge conflict
    58. 58. Resolve and commit the change
    59. 59. Rebasing Merge is good but I want a clearer history
    60. 60. Merge $ git checkout master $ git merge topic1 Merge made by recursive C0 C1 C2 topic1 C3 C4 C5 C6 master
    61. 61. Basic Rebase $ git checkout topic1 $ git rebase master C0 C1 C2 C4 topic1 C3 C5 master topic1 C3' C5'
    62. 62. Rebasing pushed commits confuses reviewers. Do not rebase commits have pushed to a public repository!
    63. 63. Example of rebasing a pushed commit
    64. 64. What's more?
    65. 65. Where is the commit lost? Look into commit updated history
    66. 66. $ git reflog SHA-1 id you can use Cherry-pick commit d6297c2 $ git cherry-pick d6297c2
    67. 67. Stashing New request comes but I am working on something else.
    68. 68. $ git stash $ git stash list/pop/apply
    69. 69. Interactive Rebase Change multiple commits Reordering
    70. 70. Interactive rebase C0 C1 C2 master C3 C4 $ git rebase -i HEAD~2 Successfully rebased and updated refs/heads/master. master C4 C3
    71. 71. git official sites: commands: zh book: Git and Linux Kernel: Git Immersion: (Practices) Revision Control: Distributed VCS: References