version control system
DESCRIPTION
A simple tutorial on gitTRANSCRIPT
Version Control SystemGit
Andrew Liu
What Is Version Control?Manage data by systematically keeping previous versionUsed in word processing, wiki system, software developmentPopular solutions:
CVSSubversion (SVN)Git
Why Use Version Control?To collaborate with othersOrderly vs. chaoticTo keep track of historyEasy to debugEasy to rollback
Why Git?http://whygitisbetterthanx.com
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.
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.
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.
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.
commit
branches
merge
ABOUT GIT
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
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/
Get startedClone a repository
git clone <git‐repository>
Check current branchgit branch
Showing current statusgit status
clean
dirtystaged
• editing the files• new files
git add
git commit
Clean to DirtyEditing filesCreating new filesDeleting files
Use git to remove a filegit rmgit mv
Files to ignoreAccount/password, log … etc.gitignore
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
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
clean
staged dirty
git revert HEAD
git rm –cached <filename>
git checkout <filename>
Dirty to CleanRemove the changes
Note: this is not revertiblegit checkout <filename>
Reset all if messed upgit reset ‐‐hard HARD
Staged to DirtyRemoving files from the staged status
git rm –cached <filename>
Clean to StagedCreate new commit for reverting
git revert HEAD
Naming CommitsHashed by SHA-1
e05db0fd4f31dde7005f075a84f96b360d05984be05db0fd
Branch nameTag nameHEAD
HEADHEAD^HEAD^^HEAD~4
TRACKING HISTORY
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>
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>
TagsCreating tags
git tag <tag‐name> <version>
Get a list of tagsgit tag –l
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>
BisectIf it does crash, then:
git bisect bad
If it is working, then:git bisect good
Finally find the guilty commit:git bisect reset
BRANCH AND MERGE
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
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>
A B C
D E
A B C
D E
F
A B C
D E
D’ F’
Original
Merge
Rebase
Possible Conflict
MergeMerge the current branch with <another-branch>
git merge <another‐branch>
Conflicts may occurred if modifications of a same file are in both branches
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
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
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)
WORKING WITH OTHERS
Setup remoteListing all remotes
git remote
Adding new remotegit remote add <git‐path> <remote>
git clone will automatically setup remote “origin”
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>
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
LET’S DOWN TO EARTH
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
Github for example
Creating Project
Setup a RepositoryFollow instructions:
BEST PRACTICES
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
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