Version control system


Published on

A simple tutorial on git

1 Like
  • Be the first to comment

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

No notes for slide

Version control system

  1. 1. Version Control System Git Andrew Liu
  2. 2. What Is Version Control?Manage data by systematicallykeeping previous versionUsed in word processing, wiki system,software developmentPopular solutions: CVS Subversion (SVN) Git
  3. 3. Why Use Version Control?To collaborate with othersOrderly vs. chaoticTo keep track of historyEasy to debugEasy to rollback
  4. 4. Why Git?
  5. 5. TerminologyRepository The repository is where files current and historical data are storedCommit 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.
  6. 6. TerminologyBranch 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.
  7. 7. TerminologyConflict 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.
  8. 8. TerminologyMerge 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.
  9. 9. branchescommit merge
  10. 10. ABOUT GIT
  11. 11. Cheat Sheet Create Change Update From existing data Using your favorite editor / IDE Fetch latest changes from origin cd ~/my_project_dir git fetch git init this does not merge them git add . Pull latest changes from origin From existing repo Revert git pull git clone ~/existing/repo ~/new/repo does a fetch followed by a merge git clone Apply a patch that someone sent you Return to the last committed state default protocol is ssh git checkout -f | git reset --hard git am -3 patch.mbox you cannot undo a hard reset In case of conflict, resolve the conflict and Revert the last commit git am --resolve Browse git revert HEAD Creates a new commit Revert specific commit Files changed in working directory git revert $id Commit git status Creates a new commit Changes to tracked files Fix the last commit git diff Commit all local changes git commit -a --amend Changes between ID1 and ID2 git commit -a after editing the broken files git diff <ID1> <ID2> Checkout the ID version of a file History of changes git checkout <ID> <file> git log Who changed what and when in a file git blame <file> Publish A commit identified by ID Branch git show <ID> Prepare a patch for other developers A specific file from a specific ID git format-patch origin List all branches Push changes to origin git diff <ID>:<FILE> git branch git push [origin] [branch] Search for patterns Switch to the BRANCH branch Make a version or milestone git grep <pattern> [path] git checkout <BRANCH> git tag <version_name> Merge branch B1 into branch B2 git checkout <B2> git merge <B1> Useful tips Create branch based on HEAD git branch <BRANCH> Create branch based on another Get help git checkout <new> <base> Configuration git help [command] Delete a branch Create empty branch git branch -d <branch> git config [--global] git symbolic-ref HEAD refs/heads/newbranch global is stored in ~/.gitconfig rm .git/index user git clean -fdx Resolve merge conflicts $name <do work> $email git add your files View merge conflicts color git commit -m Initial commit git diff color.ui auto Graphical log View merge conflicts against base file github git log --graph git diff --base <FILE> github.user $user git log --graph --pretty=oneline -- github.token $token View merge conflicts against other changes abbrev-commit git diff --theirs <FILE> optimisation Push branch to remote pack.threads 0 View merge conflicts against your changes git push <origin> <branch> git diff --ours <FILE> diff.renamelimit 0 Delete remote branch and locally After resolving conflicts, merge with do not use on low memory p git push <origin> :<branch> git add <CONFLICTING_FILE> windows git branch -d <branch> git rebase --continue core.autocrlf true This work is licensed under a Creative Commons Attribution‐Share Alike 3.0 Unported License
  12. 12. Install GitWindows
  13. 13. Get startedClone a repository git clone <git‐repository>Check current branch git branchShowing current status git status
  14. 14. clean • editing the filesgit commit • new files staged dirty git add
  15. 15. Clean to DirtyEditing filesCreating new filesDeleting files Use git to remove a file git rm git mvFiles to ignore Account/password, log … etc .gitignore
  16. 16. Dirty to stagedAdd particular changed file or new file git add <filename>Add all changed or new files git add .Add interactively git add –iPick particular changes git add ‐p
  17. 17. Staged to cleanCommit a version and open a texteditor for commit message git commitSpecify commit message git commit –m “<message>”Commit all changes git commit ‐a
  18. 18. cleangit revert HEAD git checkout <filename> staged dirty git rm –cached <filename>
  19. 19. Dirty to CleanRemove the changes Note: this is not revertible git checkout <filename>Reset all if messed up git reset ‐‐hard HARD
  20. 20. Staged to DirtyRemoving files from the staged status git rm –cached <filename>
  21. 21. Clean to StagedCreate new commit for reverting git revert HEAD
  22. 22. Naming CommitsHashed by SHA-1 e05db0fd4f31dde7005f075a84f96b360d05984 b e05db0fdBranch nameTag nameHEAD HEAD HEAD^ HEAD^^ HEAD~4
  24. 24. LogShowing all logs: git logCommits since a version: git log <version>..Commits from a version to another: git log <version‐a>..<version‐b>Commits to a certain file git log <filename>
  25. 25. Diff and ShowDifference between HEAD and HEAD^ git diffDifference between HEAD and staged file git diff ‐‐cachedDifference between versions git diff <version‐a>..<version‐b>Showing most current commit git showShow a file at a certain version git show <version>:<filename>
  26. 26. TagsCreating tags git tag <tag‐name> <version>Get a list of tags git tag –l
  27. 27. BisectFind by binary search the change thatintroduced a bug git bisect start git bisect good <good‐version> git bisect bad <bad‐version>HEAD is now point to the commitwhich is reachable from <bad-version>but not from <good-versoin>
  28. 28. BisectIf it does crash, then: git bisect badIf it is working, then: git bisect goodFinally find the guilty commit: git bisect reset
  30. 30. A Clean TreeSome operations must be applied on aclean tree (i.e. no dirty or staged file)Git provides a stack for unclean files git stash git stash pop
  31. 31. More on BranchesSwitching to another branch Note: the tree should be clean git checkout <branch>Create a new branch git branch <new‐branch> git branch <new‐branch> <start‐point>Create and switch to the new branch git checkout –b <new‐branch> git checkout –b <new‐branch> <start‐ point>
  32. 32. D E Possible ConflictOriginal A B C D EMerge A B C F D ERebase A B C D’ F’
  33. 33. MergeMerge the current branch with<another-branch> git merge <another‐branch>Conflicts may occurred if modificationsof a same file are in both branches
  34. 34. HelpersShowing common ancestor git show :1:<filename>Showing the version of HEAD git show :2:<filename>Showing the version of MERGE_HEAD git show :3:<filename>Give up a merge git reset ‐‐hard HEAD
  35. 35. RebaseStart rebase procedure git rebase <another‐branch>Rebase would stop if conflictsoccurred To continue the rebase process: git rebase ‐‐continueStop the rebase procedure git rebase ‐‐abort
  36. 36. Resolve Conflicts1. Use git diff to find out the conflicted files2. Resolve the conflict by your favorite editor3. git add <resolved‐file>4. git commit (not needed for rebase)
  38. 38. Setup remoteListing all remotes git remoteAdding new remote git remote add <git‐path> <remote>git clone will automatically setupremote “origin”
  39. 39. Working with remoteGet information from remote git fetchPulling a branch git pull <remote> <branch> git pull <remote> <local>:<target>git pull <remote> <branch> is equal to: git fetch git merge <remote>/<branch>
  40. 40. Pushing to remoteThe push command git push <remote> <branch> git push <remote> <local>:<target>Push command may fail if conflictsoccurred on remoteTo solve the problem:Pull down and merge then push
  42. 42. Get a RepositorySet a server Possible, but it requires lots of effortsUse provided service Github: The most popular solution Free for public projects Codaset: Provides a single free private project for each account
  43. 43. Github for example
  44. 44. Creating Project
  45. 45. Setup a RepositoryFollow instructions:
  47. 47. TipsEach commit includes a single logicalchangeThe code should be tested beforecommit (NOT RECOMMEND) Mark “untested” if the commit is not testedRebase rather than merge whendealing with local branches
  48. 48. For Web Application DevelopmentTwo branches: master Mapped to the production site dev Mapped to the test siteWorkflow Develop in dev or other branches except master Push to dev for testing Push to master for production