+ All Categories
Home > Documents > Version control system

Version control system

Date post: 07-Nov-2014
Category:
Upload: andrew-liu
View: 1,827 times
Download: 0 times
Share this document with a friend
Description:
A simple tutorial on git
Popular Tags:
48
Version Control System Git Andrew Liu
Transcript
Page 1: Version control system

Version Control SystemGit

Andrew Liu

Page 2: Version control system

What Is Version Control?Manage data by systematically keeping previous versionUsed in word processing, wiki system, software developmentPopular solutions:

CVSSubversion (SVN)Git

Page 3: Version control system

Why Use Version Control?To collaborate with othersOrderly vs. chaoticTo keep track of historyEasy to debugEasy to rollback

Page 4: Version control system

Why Git?http://whygitisbetterthanx.com

Page 5: Version control system

TerminologyRepository

The repository is where files' current and historical data are stored

CommitA 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.

Page 6: Version control system

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.

Page 7: Version control system

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.

Page 8: Version control system

TerminologyMerge

A merge is an operation in which two sets of changes are applied to a file or set of files.

TagA tag refers to an important snapshot in time.

HeadThe most recent commit.

Page 9: Version control system

commit

branches

merge

Page 10: Version control system

ABOUT GIT

Page 11: Version control system

Create

From existing datacd ~/my_project_dirgit initgit add .From existing repogit clone ~/existing/repo ~/new/repogit clone [email protected]:dir/project.git

default protocol is ssh

Browse

Files changed in working directorygit statusChanges to tracked filesgit diffChanges between ID1 and ID2git diff <ID1> <ID2>History of changesgit logWho changed what and when in a filegit blame <file>A commit identified by IDgit show <ID>A specific file from a specific IDgit diff <ID>:<FILE>Search for patternsgit grep <pattern> [path]

Change

Using your favorite editor / IDE

Revert

Return to the last committed stategit checkout -f | git reset --hard

you cannot undo a hard resetRevert the last commitgit revert HEAD

Creates a new commitRevert specific commitgit revert $id

Creates a new commitFix the last commitgit commit -a --amend

after editing the broken filesCheckout the ID version of a filegit checkout <ID> <file>

Update

Fetch latest changes from origingit fetch

this does not merge themPull latest changes from origingit pull

does a fetch followed by a mergeApply a patch that someone sent yougit am -3 patch.mbox

In case of conflict, resolve the conflict andgit am --resolve

Commit

Commit all local changesgit commit -a

Branch

List all branchesgit branchSwitch to the BRANCH branchgit checkout <BRANCH>Merge branch B1 into branch B2git checkout <B2>git merge <B1>Create branch based on HEADgit branch <BRANCH>Create branch based on anothergit checkout <new> <base>Delete a branchgit branch -d <branch>

Publish

Prepare a patch for other developersgit format-patch originPush changes to origingit push [origin] [branch]Make a version or milestonegit tag <version_name>

Cheat S

heet

This work is licensed under a Creative Commons Attribution‐Share Alike 3.0 Unported License

Useful tips

Get helpgit help [command]Create empty branchgit symbolic-ref HEAD refs/heads/newbranchrm .git/indexgit clean -fdx<do work>git add your filesgit commit -m 'Initial commit'Graphical loggit log --graphgit log --graph --pretty=oneline --abbrev-commitPush branch to remotegit push <origin> <branch>Delete remote branch and locallygit push <origin> :<branch>git branch -d <branch>

Resolve merge conflicts

View merge conflictsgit diffView merge conflicts against base filegit diff --base <FILE>View merge conflicts against other changesgit diff --theirs <FILE>View merge conflicts against your changesgit diff --ours <FILE>After resolving conflicts, merge withgit add <CONFLICTING_FILE>git rebase --continue

Configuration

git config [--global]global is stored in ~/.gitconfig

useruser.name $nameuser.email $emailcolorcolor.ui autogithubgithub.user $usergithub.token $tokenoptimisationpack.threads 0diff.renamelimit 0

do not use on low memory pwindowscore.autocrlf true

http://github.com/AlexZeitler/gitcheatsheet

Page 12: Version control system

Install GitWindows

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/

Page 13: Version control system

Get startedClone a repository

git clone <git‐repository>

Check current branchgit branch

Showing current statusgit status

Page 14: Version control system

clean

dirtystaged

• editing the files• new files

git add

git commit

Page 15: Version control system

Clean to DirtyEditing filesCreating new filesDeleting files

Use git to remove a filegit rmgit mv

Files to ignoreAccount/password, log … etc.gitignore

Page 16: Version control system

Dirty to stagedAdd particular changed file or new file

git add <filename>

Add all changed or new filesgit add .

Add interactivelygit add –i

Pick particular changesgit add ‐p

Page 17: Version control system

Staged to cleanCommit a version and open a text editor for commit message

git commit

Specify commit messagegit commit –m “<message>”

Commit all changesgit commit ‐a

Page 18: Version control system

clean

staged dirty

git revert HEAD

git rm –cached <filename>

git checkout <filename>

Page 19: Version control system

Dirty to CleanRemove the changes

Note: this is not revertiblegit checkout <filename>

Reset all if messed upgit reset ‐‐hard HARD

Page 20: Version control system

Staged to DirtyRemoving files from the staged status

git rm –cached <filename>

Page 21: Version control system

Clean to StagedCreate new commit for reverting

git revert HEAD

Page 22: Version control system

Naming CommitsHashed by SHA-1

e05db0fd4f31dde7005f075a84f96b360d05984be05db0fd

Branch nameTag nameHEAD

HEADHEAD^HEAD^^HEAD~4

Page 23: Version control system

TRACKING HISTORY

Page 24: Version control system

LogShowing all logs:

git log

Commits since a version:git log <version>..

Commits from a version to another:git log <version‐a>..<version‐b>

Commits to a certain filegit log <filename>

Page 25: Version control system

Diff and ShowDifference between HEAD and HEAD^

git diff

Difference between HEAD and staged filegit diff ‐‐cached

Difference between versionsgit diff <version‐a>..<version‐b>

Showing most current commitgit show

Show a file at a certain versiongit show <version>:<filename>

Page 26: Version control system

TagsCreating tags

git tag <tag‐name> <version>

Get a list of tagsgit tag –l

Page 27: Version control system

BisectFind by binary search the change that introduced a bug

git bisect startgit bisect good <good‐version>git bisect bad <bad‐version>

HEAD is now point to the commit which is reachable from <bad-version> but not from <good-versoin>

Page 28: Version control system

BisectIf it does crash, then:

git bisect bad

If it is working, then:git bisect good

Finally find the guilty commit:git bisect reset

Page 29: Version control system

BRANCH AND MERGE

Page 30: Version control system

A Clean TreeSome operations must be applied on a clean tree (i.e. no dirty or staged file)Git provides a stack for unclean files

git stashgit stash pop

Page 31: Version control system

More on BranchesSwitching to another branch

Note: the tree should be cleangit checkout <branch>

Create a new branchgit branch <new‐branch>git branch <new‐branch> <start‐point>

Create and switch to the new branchgit checkout –b <new‐branch>git checkout –b <new‐branch> <start‐point>

Page 32: Version control system

A B C

D E

A B C

D E

F

A B C

D E

D’ F’

Original

Merge

Rebase

Possible Conflict

Page 33: Version control system

MergeMerge the current branch with <another-branch>

git merge <another‐branch>

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

Page 34: Version control system

HelpersShowing common ancestor

git show :1:<filename>

Showing the version of HEADgit show :2:<filename>

Showing the version of MERGE_HEADgit show :3:<filename>

Give up a mergegit reset ‐‐hard HEAD

Page 35: Version control system

RebaseStart rebase procedure

git rebase <another‐branch>

Rebase would stop if conflicts occurred

To continue the rebase process:git rebase ‐‐continue

Stop the rebase proceduregit rebase ‐‐abort

Page 36: Version control system

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)

Page 37: Version control system

WORKING WITH OTHERS

Page 38: Version control system

Setup remoteListing all remotes

git remote

Adding new remotegit remote add <git‐path> <remote>

git clone will automatically setup remote “origin”

Page 39: Version control system

Working with remoteGet information from remote

git fetch

Pulling a branchgit pull <remote> <branch>git pull <remote> <local>:<target>

git pull <remote> <branch> is equal to:git fetchgit merge <remote>/<branch>

Page 40: Version control system

Pushing to remoteThe push command

git push <remote> <branch>git push <remote> <local>:<target>

Push command may fail if conflicts occurred on remoteTo solve the problem:Pull down and merge then push

Page 41: Version control system

LET’S DOWN TO EARTH

Page 42: Version control system

Get a RepositorySet a server

Possible, but it requires lots of effortsUse provided service

Github: http://github.com/The most popular solutionFree for public projects

Codaset: http://codaset.com/Provides a single free private project for each account

Page 43: Version control system

Github for example

Page 44: Version control system

Creating Project

Page 45: Version control system

Setup a RepositoryFollow instructions:

Page 46: Version control system

BEST PRACTICES

Page 47: Version control system

TipsEach commit includes a single logical changeThe 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

Page 48: Version control system

For Web Application Development

Two branches:master

Mapped to the production sitedev

Mapped to the test site

WorkflowDevelop in dev or other branches except masterPush to dev for testingPush to master for production


Recommended