git for coding and writing - ucla department of · pdf filesetup • standard approach:...
TRANSCRIPT
Git for Coding and Writing
Instructor: Wotao Yin (UCLA Math)
Summer 2016
1 / 25
What is it for?
• Version control: record the changes, restore changes, compare versions,branching/merging
• Collaborative, distributed co-authoring
• Fast and very robust
It is used by tens of thousands of projects
2 / 25
Setup
• Standard approach:• download git for Linux/OSX/Windows• learn git commands
• Easy approach: download SmartGit (free for personal use) and use the GUI
3 / 25
Create a new repo
• Goto an existing folder
• Command: git init
• SmartGit: Repository -> Add or Create and follow instructions
4 / 25
Clone an existing repo
• Find its local path or remote URL
• Goto where you want to save the clone
• Clone a local repo: git clone /path/to/repo
• Clone a remote repo: git clone username@URL
• SmartGit: Repository -> Clone
5 / 25
If the repo has multiple branches
• Ensure no uncommitted changes or stash them (will explain later)
• Suppose that you want to check out a branch called “dev” (so that thefolders will have the files in that branch)
• Command: git checkout dev
• SmartGit: Right Click a branch and select Check Out, or Branch ->Checkout and select one from the log
6 / 25
Typical branches
• master: the stable, shareable versions
• dev: versions with changes in progress, merge to master once stable
• dev partname: versions with changes only in a part, merge to dev atmilestones
• dev feature: versions for a specific feature, merge to dev once stable
• dev yourname: versions in charge by a person, merge to dev once stable
7 / 25
Add a branch
• Check out the branch your new branch will be based on: git checkoutdev
• Add a new branch: git checkout dev yourname, or add it and check itout git checkout -b dev yourname
• SmartGit: Branch -> Add Branch
8 / 25
Delete a branch
• Checkout a branch other than the branch to be deleted
• Delete branch (say dev): git branch -d dev
• SmartGit: checkout another branch and Branch -> Delete
9 / 25
Workflow
• Working direction: actual files
• Index (staging area): record changes temporarily, stage for commit
• Repo: stores all the commits
(from git-scm.com)
10 / 25
Track a new file or record changes (stage)
• Command: git add <filename>, git add *
• SmartGit: select file(s) and click on the Stage button
• You can unstage file(s) using the git reset command or the Unstagebutton
11 / 25
Commit
• Permanently1 record the changes
• Command: git commit -m "Commit message"
• SmartGit: click on the Commit button
1not really: you can use git reset in a dangerous manner to undo it.12 / 25
File status
13 / 25
Pushing to a remote repo
• Well-known remot repo hosts: GitHub, BitBucket, ...
• If you did not clone a repo from a remote repo, then origin is not set.• Command: git remote add origin URL• SmartGit: Remote -> Add
otherwise, origin is already set
• Command to push a branch (say “dev”)• Command: git push origin dev• SmartGit: click on the Push button if dev is checked out; otherwise,
right-click on the dev branch and select Push
14 / 25
Fetch and Pull
• Fetch: fetch remote changes, and bring my local repo up to date; nochange to the working files
• Pull: fetch and then bring (see next slide) the changes all the way to yourworking directly
• Command:• Fetch (say origin): git fetch origin• Pull: git pull origin
• SmartGit: click on the Pull button and select Pull or Fetch Only
15 / 25
Git data
16 / 25
Pull: Merge vs Rebase• Two main ways to integrate changes from one branch to another
• Merge: integrate diverged work history and keep work history unchanged(fast-forward, three-way merging, see GitPro)
17 / 25
Pull: Merge vs Rebase (cont’d)
• Merge Command (in the above example):git checkout mastergit merge iss53git branch -d iss53 (delete merged branch)
• Merge SmartGit: Branch -> Merge -> Select branch to be mergedinto current branch
18 / 25
Pull: Merge vs Rebase (cont’d)
• Rebase: takes all the changes that were commited on the current branchbelow the HEAD and reapply them on another one
• Changes work history, making it ’linear’
• Note: Do not rebase commits that exist outside your repository
19 / 25
Pull: Merge vs Rebase (cont’d)
• Rebase Command (in the above example):git checkout experimentgit rebase mastergit checkout mastergit merge experiment (fast-forward merge)
20 / 25
Pull: Merge vs Rebase (cont’d)
• Rebase SmartGit: Branch -> Rebase
• Rebase HEAD to rebases (’moves’) the commits below the HEAD tothe selected commit
• Rebase to HEAD duplicates commits from a separate branch to theHEAD
• See here for illustrations
21 / 25
Fork
• A fork is a copy of a repository
• Most commonly, forks are used to either propose changes to someoneelse’s project or to use someone else’s project as a starting point for yourown idea.
• Two-step (say on GitHub):• Navigate to the repository to be forked• In the top-right corner of the page, click Fork
22 / 25
Merge and Pull Request
• Merge a pull request into the upstream branch when work is completed.Anyone with push access to the repository can complete the merge
• If the merge will not have any conflicts, you can merge the pull requestonline
23 / 25
.gitignore
• Specifies intentionally untracked files to ignore
• Rules for patterns you can put in the .gitignore file are as follows:• Blank lines or lines starting with # are ignored• Standard glob patterns work• You can start patterns with a forward slash to avoid recursivity• You can end patterns with a forward slash to specify a directory• You can negate a pattern by starting it with an exclamation point (!)
• For more examples, see GitPro
24 / 25
(other topics)
25 / 25