a beginner’s guide to git dr duncan forgan and github · a beginner’s guide to git and github...

19
A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan [email protected] http://duncanforgan.wordpress.com

Upload: others

Post on 03-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

A Beginner’s Guide to Git and GitHub

Dr Duncan Forgan@dh4gan

[email protected] http://duncanforgan.wordpress.com

Page 2: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

What is Version Control?

An efficient way of tracking changes to files Instead of saving entire directory, save changesets or commits

Page 3: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Centralised Version Control

e.g. CVS, Subversion

Page 4: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Distributed Version Control

Distributed Version Control

Each Computer stores a copy of the version database

e.g. git, mercurial

Page 5: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Installing git

Page 6: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Configuring git

> git config --global user.name “John Doe” > git config --global user.email “[email protected]

This ensures that your commits are correctly labelled (Crucial if you are collaborating on code)

Settings stored in ~/.gitconfig

Page 7: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

How git creates a new version of code

Make changes to

files

Stage changed files

Commit staged files

git add <files> git commit

Page 8: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

A Simple Recipe for Local Version Control

> cd dir/where/code/is

> git init

> git add <files>

> git commit

# Hopefully obvious

# Create a git repository

# Select files to be committed

# Takes you to an editor screen # (write a commit message)

> git status # Check status of all files

> git log # Lists all your commits

> git diff # All changes in code since last commit

Modify code, git add, git commit, modify code, git add, git commit…

Page 9: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Undoing Things

> git checkout <file> # Erase file changes since last commit

> git reset HEAD~<n> # Go back n commits

> git rm --cached <file> # Stop tracking a file (without deleting it)

Page 10: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Using Remote Code Repositories

GitHub vs Bitbucket

Accounts Free Public Repos Free

Limited Private Repos (subs)

Accounts Free Private Repos Free

Limited Public Repos (subs)

Page 11: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Setting up a Remote Code RepositoryFirst - go online and create remote repository at website

Obtain its URL

> git remote add origin <url> > git push origin master > git pull origin master

# Create repo with alias ‘origin’ # Send code to remote repo # Receive code from remote repo

master refers to the branch of code you are working on

Page 12: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

GitHub Tools

Every GitHub repository should have a README.md and a LICENSE file

Issues is probably the most important tool Best way to encourage others to help you with a problem

Page 13: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Tagging

Tags are pointers to a specific commit (Nice way of identifying which commit is v1.0, 1.1 etc)

> git tag - lists all tags > git tag <taglabel> - tags HEAD commit with label <taglabel> > git tag <taglabel> <commithash> - tags a specific commit

> git tag -a <taglabel> -m “message” - creates an annotated tag > git show <taglabel> - show tag data and corresponding commit

> git push origin --tags - push tags to remote

Page 14: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Branching And Merging

You should always have a stable, working version of code (master) If you want to add a feature, do this on a branch Once the feature works, you can merge it into master

> git checkout -b hotfix > git add, commit etc > git checkout master > git merge hotfix

Page 15: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Rebasing

HEALTH WARNING: Only rebase if you know what you’re doing (if you use git correctly, you should never have to rebase)

master

develop

master

develop

> git checkout develop > git rebase master

Page 16: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Forking

Make a copy of someone else’s repository to work on

Page 17: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Pull RequestsAsk a developer to pull branch into their repo’s master branch

How it works depends on whether you’re collaborating on same repo or different repos

Same repo: 1. Create a branch 2. Do some coding on that branch 3. Submit pull request

Different Repos 1. Fork repo 2. Do coding in forked repo 3. Submit pull request

Once pull request submitted, this opens up a discussion system on github/bitbucket

Page 18: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Pull Requests

Page 19: A Beginner’s Guide to Git Dr Duncan Forgan and GitHub · A Beginner’s Guide to Git and GitHub Dr Duncan Forgan @dh4gan dhf3@st-andrews.ac.uk ... Only rebase if you know what you’re

Summary

Version control is useful for any project with text files

git is the most popular version control system

Use GitHub and Bitbucket to store code online

Write READMEs and LICENSEs

Never develop code on the master branch