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

Version Control System - Git

Version Control System - Git

2011 CCSP

Andrew Liu

March 26, 2012
Tweet

More Decks by Andrew Liu

Other Decks in Programming

Transcript

  1. Version Control System
    Git
    Andrew Liu

    View full-size slide

  2. What Is Version Control?
    Manage data by systematically
    keeping previous version
    Used in word processing, wiki system,
    software development
    Popular solutions:
    CVS
    Subversion (SVN)
    Git

    View full-size slide

  3. Why Use Version Control?
    To collaborate with others
    Orderly vs. chaotic
    To keep track of history
    Easy to debug
    Easy to rollback

    View full-size slide

  4. Why Git?
    http://whygitisbetterthanx.com

    View full-size slide

  5. Terminology
    Repository
    The repository is where files' current and
    historical data are stored
    Commit
    A commit is the action of writing or
    merging the changes made in the
    working copy back to the repository.
    The terms commit can also used in noun
    form to describe the new revision that is
    created as a result of committing.

    View full-size slide

  6. Terminology
    Branch
    A set of files under version control may
    be branched or forked at a point in time
    so that, from that time forward, two
    copies of those files may develop at
    different speeds or in different ways
    independently of each other.

    View full-size slide

  7. Terminology
    Conflict
    A conflict occurs when different parties
    make changes to the same document,
    and the system is unable to reconcile the
    changes. A user must resolve the conflict
    by combining the changes, or by
    selecting one change in favor of the
    other.

    View full-size slide

  8. Terminology
    Merge
    A merge is an operation in which two sets
    of changes are applied to a file or set of
    files.
    Tag
    A tag refers to an important snapshot in
    time.
    Head
    The most recent commit.

    View full-size slide

  9. commit
    branches
    merge

    View full-size slide

  10. Create
    From existing data
    cd ~/my_project_dir
    git init
    git add .
    From existing repo
    git clone ~/existing/repo ~/new/repo
    git clone [email protected]:dir/project.git
    default protocol is ssh
    Browse
    Files changed in working directory
    git status
    Changes to tracked files
    git diff
    Changes between ID1 and ID2
    git diff
    History of changes
    git log
    Who changed what and when in a file
    git blame
    A commit identified by ID
    git show
    A specific file from a specific ID
    git diff :
    Search for patterns
    git grep [path]
    Change
    Using your favorite editor / IDE
    Revert
    Return to the last committed state
    git checkout -f | git reset --hard
    you cannot undo a hard reset
    Revert the last commit
    git revert HEAD
    Creates a new commit
    Revert specific commit
    git revert $id
    Creates a new commit
    Fix the last commit
    git commit -a --amend
    after editing the broken files
    Checkout the ID version of a file
    git checkout
    Update
    Fetch latest changes from origin
    git fetch
    this does not merge them
    Pull latest changes from origin
    git pull
    does a fetch followed by a merge
    Apply a patch that someone sent you
    git am -3 patch.mbox
    In case of conflict, resolve the conflict and
    git am --resolve
    Commit
    Commit all local changes
    git commit -a
    Branch
    List all branches
    git branch
    Switch to the BRANCH branch
    git checkout
    Merge branch B1 into branch B2
    git checkout
    git merge
    Create branch based on HEAD
    git branch
    Create branch based on another
    git checkout
    Delete a branch
    git branch -d
    Publish
    Prepare a patch for other developers
    git format-patch origin
    Push changes to origin
    git push [origin] [branch]
    Make a version or milestone
    git tag
    Cheat Sheet
    This work is licensed under a Creative Commons Attribution‐Share Alike 3.0 Unported License
    Useful tips
    Get help
    git help [command]
    Create empty branch
    git symbolic-ref HEAD
    refs/heads/newbranch
    rm .git/index
    git clean -fdx

    git add your files
    git commit -m 'Initial commit'
    Graphical log
    git log --graph
    git log --graph --pretty=oneline --
    abbrev-commit
    Push branch to remote
    git push
    Delete remote branch and locally
    git push :
    git branch -d
    Resolve merge conflicts
    View merge conflicts
    git diff
    View merge conflicts against base file
    git diff --base
    View merge conflicts against other changes
    git diff --theirs
    View merge conflicts against your changes
    git diff --ours
    After resolving conflicts, merge with
    git add
    git rebase --continue
    Configuration
    git config [--global]
    global is stored in ~/.gitconfig
    user
    user.name $name
    user.email $email
    color
    color.ui auto
    github
    github.user $user
    github.token $token
    optimisation
    pack.threads 0
    diff.renamelimit 0
    do not use on low memory p
    windows
    core.autocrlf true
    http://github.com/AlexZeitler/gitcheatsheet

    View full-size slide

  11. Install Git
    Windows
    http://help.github.com/win-set-up-git/
    Mac
    http://help.github.com/mac-set-up-git/
    Linux
    http://help.github.com/linux-set-up-git/

    View full-size slide

  12. Get started
    Clone a repository
    git clone
    Check current branch
    git branch
    Showing current status
    git status

    View full-size slide

  13. clean
    dirty
    staged
    • editing the files
    • new files
    git add
    git commit

    View full-size slide

  14. Clean to Dirty
    Editing files
    Creating new files
    Deleting files
    Use git to remove a file
    git rm
    git mv
    Files to ignore
    Account/password, log … etc
    .gitignore

    View full-size slide

  15. Dirty to staged
    Add particular changed file or new file
    git add
    Add all changed or new files
    git add .
    Add interactively
    git add –i
    Pick particular changes
    git add ‐p

    View full-size slide

  16. Staged to clean
    Commit a version and open a text
    editor for commit message
    git commit
    Specify commit message
    git commit –m “”
    Commit all changes
    git commit ‐a

    View full-size slide

  17. clean
    staged dirty
    git revert HEAD
    git rm –cached
    git checkout

    View full-size slide

  18. Dirty to Clean
    Remove the changes
    Note: this is not revertible
    git checkout
    Reset all if messed up
    git reset ‐‐hard HARD

    View full-size slide

  19. Staged to Dirty
    Removing files from the staged status
    git rm –cached

    View full-size slide

  20. Clean to Staged
    Create new commit for reverting
    git revert HEAD

    View full-size slide

  21. Naming Commits
    Hashed by SHA-1
    e05db0fd4f31dde7005f075a84f96b360d05984
    b
    e05db0fd
    Branch name
    Tag name
    HEAD
    HEAD
    HEAD^
    HEAD^^
    HEAD~4

    View full-size slide

  22. TRACKING HISTORY

    View full-size slide

  23. Log
    Showing all logs:
    git log
    Commits since a version:
    git log ..
    Commits from a version to another:
    git log ..
    Commits to a certain file
    git log

    View full-size slide

  24. Diff and Show
    Difference between HEAD and HEAD^
    git diff
    Difference between HEAD and staged file
    git diff ‐‐cached
    Difference between versions
    git diff ..
    Showing most current commit
    git show
    Show a file at a certain version
    git show :

    View full-size slide

  25. Tags
    Creating tags
    git tag
    Get a list of tags
    git tag –l

    View full-size slide

  26. Bisect
    Find by binary search the change that
    introduced a bug
    git bisect start
    git bisect good
    git bisect bad
    HEAD is now point to the commit
    which is reachable from
    but not from

    View full-size slide

  27. Bisect
    If it does crash, then:
    git bisect bad
    If it is working, then:
    git bisect good
    Finally find the guilty commit:
    git bisect reset

    View full-size slide

  28. BRANCH AND MERGE

    View full-size slide

  29. A Clean Tree
    Some operations must be applied on a
    clean tree (i.e. no dirty or staged file)
    Git provides a stack for unclean files
    git stash
    git stash pop

    View full-size slide

  30. More on Branches
    Switching to another branch
    Note: the tree should be clean
    git checkout
    Create a new branch
    git branch
    git branch
    Create and switch to the new branch
    git checkout –b
    git checkout –b point>

    View full-size slide

  31. A B C
    D E
    A B C
    D E
    F
    A B C
    D E
    D’ F’
    Original
    Merge
    Rebase
    Possible Conflict

    View full-size slide

  32. Merge
    Merge the current branch with

    git merge
    Conflicts may occurred if modifications
    of a same file are in both branches

    View full-size slide

  33. Helpers
    Showing common ancestor
    git show :1:
    Showing the version of HEAD
    git show :2:
    Showing the version of MERGE_HEAD
    git show :3:
    Give up a merge
    git reset ‐‐hard HEAD

    View full-size slide

  34. Rebase
    Start rebase procedure
    git rebase
    Rebase would stop if conflicts
    occurred
    To continue the rebase process:
    git rebase ‐‐continue
    Stop the rebase procedure
    git rebase ‐‐abort

    View full-size slide

  35. Resolve Conflicts
    1. Use git diff to find out the
    conflicted files
    2. Resolve the conflict by your favorite
    editor
    3. git add
    4. git commit (not needed for rebase)

    View full-size slide

  36. WORKING WITH OTHERS

    View full-size slide

  37. Setup remote
    Listing all remotes
    git remote
    Adding new remote
    git remote add
    git clone will automatically setup
    remote “origin”

    View full-size slide

  38. Working with remote
    Get information from remote
    git fetch
    Pulling a branch
    git pull
    git pull :
    git pull is equal to:
    git fetch
    git merge /

    View full-size slide

  39. Pushing to remote
    The push command
    git push
    git push :
    Push command may fail if conflicts
    occurred on remote
    To solve the problem:
    Pull down and merge then push

    View full-size slide

  40. LET’S DOWN TO EARTH

    View full-size slide

  41. Get a Repository
    Set a server
    Possible, but it requires lots of efforts
    Use provided service
    Github: http://github.com/
    The most popular solution
    Free for public projects
    Codaset: http://codaset.com/
    Provides a single free private project for each
    account

    View full-size slide

  42. Github for example

    View full-size slide

  43. Creating Project

    View full-size slide

  44. Setup a Repository
    Follow instructions:

    View full-size slide

  45. BEST PRACTICES

    View full-size slide

  46. Tips
    Each commit includes a single logical
    change
    The code should be tested before
    commit
    (NOT RECOMMEND) Mark “untested” if
    the commit is not tested
    Rebase rather than merge when
    dealing with local branches

    View full-size slide

  47. For Web Application
    Development
    Two branches:
    master
    Mapped to the production site
    dev
    Mapped to the test site
    Workflow
    Develop in dev or other branches except
    master
    Push to dev for testing
    Push to master for production

    View full-size slide