git for coding and writing - ucla department of · pdf filesetup • standard approach:...

25
Git for Coding and Writing Instructor: Wotao Yin (UCLA Math) Summer 2016 1 / 25

Upload: phungphuc

Post on 06-Feb-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

Git for Coding and Writing

Instructor: Wotao Yin (UCLA Math)

Summer 2016

1 / 25

Page 2: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 3: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 4: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

Create a new repo

• Goto an existing folder

• Command: git init

• SmartGit: Repository -> Add or Create and follow instructions

4 / 25

Page 5: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 6: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 7: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 8: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 9: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 10: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 11: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 12: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 13: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

File status

13 / 25

Page 14: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 15: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 16: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

Git data

16 / 25

Page 17: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 18: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 19: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 20: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 21: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 22: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 23: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

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

Page 24: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

.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

Page 25: Git for coding and writing - UCLA Department of · PDF fileSetup • Standard approach: • download git for Linux/OSX/Windows • learn git commands • Easy approach: download SmartGit

(other topics)

25 / 25