. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Learning by Doing A Short Introduction to git Jianwen Wei [email protected]Omni-Lab, Shanghai Jiaotong University http://omnilab.sjtu.edu.cn March 9, 2015 Jianwen Wei [email protected](Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cnLearning by Doing A Short Introduction to git March 9, 2015 1 / 49
49
Embed
Learning by Doing A Short Introduction to git · Learning by Doing A Short Introduction to git ... changes between the staged and working files git diff --staged ... git tag -d TAG_NAME
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 1 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 2 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 3 / 49
Figure: VCS work flowJianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 4 / 49
Distributed VCSs support centralized work flow too.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 5 / 49
git is super fastFull repository cloneLocal history: no need to connect to servers when viewing the revisionhistoryCheap branch and easy mergeLots of git host choices: github, Google Code, gitbucket, gitlab,CodePlex…Other things: tidy working directory, better compression, multi workflow support, …
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 6 / 49
Try git and githubMost graphical tool/plug-ins1 suck. Please use the command-line git.Read git’s prompts, run git help to get help.Find “how-to” on Google, StackOverflow, git book.
1tortoisegit, gitk, EGit, Snow Octocat… But please, oh please use the command-linetool.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 7 / 49
“A clear development flow is worth thousands of VCSs.”One repo for one project. Use submodule to organize super projects.Modular design, avoid simultaneous source file editing by differentmembers.Head version at trunk is always ready to deploy.Modification is made on branches, then merged into trunk.Stay on your own branch.Write comment to each commit.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 8 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 9 / 49
git’s stand-alone work flowYou can use git on a stand-alone computer and easily integrate the codeinto a more sophisticated work flow (distributed or centralized) at a latertime.
Figure: git’s local work flow
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 10 / 49
Every collaborator keeps a full clone of the repository.All repositories are peers.Repositories are not necessarily consistent at all time. Use push/pullto exchange changes when necessary.
Figure: git’s distributed work flowJianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 11 / 49
git’s emulation to the centralized work flow(RECOMMENDED)
Pick a git repo as the central one with which other repos syncThe statement, “all repositories are peers.”, still holds.We pretend that we see the central repo only, unaware of each other’speer repo.
Figure: git’s centralized work flow for John and Jessica
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 12 / 49
Please follow github’s nice tutorials to set up git on Windows, Linuxor Mac.Must-known things about SSH keys: private key, public key, the passphrase to access the private key, key fingerprint.Don’t forget to set user.name and user.email before your very firstgit command-line commit.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 13 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 14 / 49
helpinitstatusaddcommitdifftagWorking with branchWorking with remotesubmoduleOh, there is a conflict!!!Time Machine
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 15 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 16 / 49
init: Initialize a local git repo for your project
cd YOUR_PROJ_DIRgit init .
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 17 / 49
status: Show the status of your repogit status also tells you how to undo the last operation on git.
git status
File status in git: untracked, unstaged, staged (indexed), committed.
Figure: File status life cycle
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 18 / 49
For untracked files: add them to git’s control.For unstaged changes: add them to the staged area.For conflicted files: add marks them as “resolved”.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 19 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 20 / 49
Each commit is identified by a UNIQUE SHA-1 ID of 40 ASCIIcharacters.
commit dd5f924c40096b9cda27ffd1cfd1205822ab3c70Author: Github Support <[email protected]>Date: Sun Apr 1 19:38:37 2012 +0800
Restart the git-tutorial project.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 21 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 22 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 23 / 49
Repo in RepoManage other repos as “submodules” in your project
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 24 / 49
1 Create a branch2 Switch to the newly-created branch3 Modify and commit on the branch4 Merge branch’s changes into trunk.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 25 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 26 / 49
Switch to a branch. The working files will change.
git checkout -f BRANCH_NAME
Force switch to a branch
git checkout master
Go back to trunk, named master in git.
git checkout -b BRANCH_NAME
Create a branch then switch to it.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 27 / 49
Merge branch_a’s and branch_b’s changes into current branch
git checkout master, git merge master BRANCH_NAME
Merge changes into trunk, the master branch.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 28 / 49
git clone REPO_URL Full clone of a repo.URL can be in forms of local dir (~/proj), git (git://xxx), SSH(ssh://xxx), https (http://xxx)…
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 29 / 49
git remote add REPO_NAME REPO_URLAdd a remote repo to tracked list.
git remote rm REPO_NAMERemove a remote repo from the tracked list.
git remote rename REPO_OLD REPO_NEWRename a repo.
git help remoteShow remote help doc
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 30 / 49
Merge remote branch’s changes into current branch.
git push REPO_NAME REMO_BRANCH
Push current branch’s changes to the remote branch.
git push REPO_NAME :REMO_BRANCH
Delete a remote branch.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 31 / 49
Conflicts arise when git cannot automatically merge changes at mergeor pull operations.Don’t panic. Conflicts are no big deal, sometimes even inevitable.What you should do: merge the conflicts, mark the files as “resolved”,then commit the changes.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 32 / 49
1 You can edit the conflicted files, merge conflicts MANUALLY. Or,git checkout --theirs FILES replace the conflicted files withtheirs.git checkout --ours FILES replace the conflicted files with ours.
2 git add CONFLICT_FILES mark the file as resolved.3 git commit -m "YOUR_COMM" commit changes to the repo.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 33 / 49
“Time Machine”: stash, checkoutstash saves your temporary work and resets the files to HEAD version. Youcan handle some emergency fix first then continue to hack at a latter time.
1 Save the temp changes.
git stash
2 Check the stash list.
git stash list
3 EDIT and COMMIT your emergency fix.4 Continue to hack.
git stash pop
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 34 / 49
checkout enable you to go backward and forward in the revision history.
1 git checkout COMMITID_OR_TAGNAME
Time Machine starts up.2 You are on a unnamed branch with file status dating back. Do
anything you want.3 git checkout master
Come back to master.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 35 / 49
git checkout COMMIT_ID -- FILE_LIST check out the file list at thespecified commit.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 36 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 37 / 49
1 Set up git on your computer, and sign up a github account.2 Initialize a local project as git repo, make your first git.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 38 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 39 / 49
1 Create a branch.2 Checkout to that branch.3 Merge the changes into trunk (master).4 Delete the branch.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 40 / 49
1 Create a local branch called brA, modify a text file.2 Create a local branch called brB, modify the text file on the same line
as brA.3 Merge brA into master, then merge brB into master. So a conflict
arises.4 Resolve the conflict, then add, commit.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 41 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 42 / 49
1 Register a github account and leave your email address public on yourhomepage.
2 Open an issue in GitForBeginners to say hello.3 Fork GitForBeginners.4 Now go to your github homepage, you will find a clone of
GitForBeginnerss there.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 43 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 44 / 49
1 Create a local branch with your full name, such as branch-zhangsan.2 Switch to that branch, write something into README.mkd. Then
push this branch to your github repo.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 45 / 49
Send me a merge request on github. That is, ask me to merge from yourYOUR_NAME branch in your GitForBeginners repo, into the masterbranch in my GitForBeginners repo.Congratulations! You will get your gitlab account after thischallenge. Please check your mailbox.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 46 / 49
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 47 / 49
“Git Tutorials” by Li Yanruigithub:helpPro Git On lineVideo: “Git the basics” by Bart TrojanowskiO’Reilly Book: Version Control With Git, 2nd Edition“Git Reference”“GitforBeginners” by Jianwen Wei, hosted on github
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 48 / 49
The slides are composed with Markdown language, and converted tolatex beamer with pandoc.XeTeX is a nice typesetting system. latexmk helps to hide thecomplexity of compilation.The slides, along with the project, is hosted on github.Feedback is always welcomed. Write me or open an issue on theproject homepage.
Jianwen Wei [email protected] (Omni-Lab, Shanghai Jiaotong Universityhttp://omnilab.sjtu.edu.cn)Learning by Doing A Short Introduction to git March 9, 2015 49 / 49