a beginner’s guide to git dr duncan forgan and github · a beginner’s guide to git and github...
Post on 03-Jun-2020
12 Views
Preview:
TRANSCRIPT
A Beginner’s Guide to Git and GitHub
Dr Duncan Forgan@dh4gan
dhf3@st-andrews.ac.uk http://duncanforgan.wordpress.com
What is Version Control?
An efficient way of tracking changes to files Instead of saving entire directory, save changesets or commits
Centralised Version Control
e.g. CVS, Subversion
Distributed Version Control
Distributed Version Control
Each Computer stores a copy of the version database
e.g. git, mercurial
Installing git
Configuring git
> git config --global user.name “John Doe” > git config --global user.email “johndoe@example.com”
This ensures that your commits are correctly labelled (Crucial if you are collaborating on code)
Settings stored in ~/.gitconfig
How git creates a new version of code
Make changes to
files
Stage changed files
Commit staged files
git add <files> git commit
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…
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)
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)
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
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
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
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
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
Forking
Make a copy of someone else’s repository to work on
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
Pull Requests
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
top related