Skip to content

k8ywalsh/git-tutorial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 

Repository files navigation

Git Tutorial

Install git

  1. Open the command line (e.g. /Applications/Utilities/Terminal on a mac)
  2. To check to see if you have git installed
git --version
  1. To install on a Mac, type (this will install a pared down version of XCode that includes git)
xcode-select --install
  1. For other operating systems, following these directions

Modify your global configurations

Replace these details with your information and specify what you'd like your default text editor to be (e.g. sublime text, nano, vim)

git config --global --list
git config --global user.name 'Dani Cosme'
git config --global user.email '[email protected]'
git config --global core.editor 'sublime'

Create an account on GitHub and join UO Data Science organization

  1. Go to GitHub to create an account
  2. Go to UO Data Science
  3. Add username to list on Slack to be added to the organization
  4. Accept email invitation

Why version control?

  • To have a backup
  • To see what has changed in the document over time
  • To be able to roll back to any previous version
  • To work collaboratively and avoid conflicted copies
  • To document your code and improve reproducibility
  • To make it simple to share your code
  • To avoid this:

What can be version controlled with git?

  • Plain text
  • Code
  • Git can't version control binary files (e.g. Word docs, images); it tracks that they've changed, but not what's different

Key concepts and vocabulary

  • Snapshots = records of what files look like at a given point in time
    • You decide when to take snapshots
    • History of all snapshots is retained
    • Analogy – they're kind of like photos
  • Staging = which files to include in the snapshot
    • You decide which files you want to take snapshots of
    • Analogy – think of this step as deciding who's going to be in a group photo; you may want to include some people, but not others
  • Commit = the act of creating a snapshot
    • Info that's been changed
    • A reference to the commit that came brefore it (parent commit)
    • Analogy – think of this step as actually taking the photo with only those you chose to be in it
  • Repository = collection of files and file history
    • Local repository = exists only on your local machine
    • Remote repository = exists on a remote website (e.g. github.com, gitlab.com, bitbucket.org)
    • Also called a "repo" for short
    • Analogy – this is kind of like the photo album that stores all your snapshots
  • Cloning = copying a repository
  • Pulling = grabbing changes from a remote repository
  • Pushing = pushing changes to a remote repository
  • Branches = offshoots of the master branch
    • Master = typically the main branch
  • Merging = combining branch with master repository

Basic process for local use

Image from git-scm.com

  1. Make changes to a file
  2. Stage the file
    • Choose to take a snapshot of the changes
    • git add [file]
  3. Commit the changes
    • Take a snapshot of the file
    • git commit -m "I made these changes.." [file]
  4. Rinse, repeat.

Tutorial

Local use

  1. Make directory on your desktop
mkdir ~/Desktop/git-test
cd ~/Desktop/git-test
  1. Initialize the repository
git init
  1. Check what's in the directory
ls -a
  1. Check status
git status
  1. Create file with some text
printf "I <3 git" > test.txt
  1. List files and check status
ls 
git status
  1. Add test.txt file as a tracked file in the repository
git add test.txt 
  1. Check status and check what's different
git status
git diff
  1. Add a new line of text
printf "\nWhy hello there" >> test.txt
  1. Check what's different
git diff test.txt
  1. Check status again
git status
  1. Stage the changes made to the file
git add test.txt
  1. Check status again
git status
  1. Save snapshot of the file by commiting changes
git commit -m "added test file"
  1. Check version history
git log

Basic process for remote and collaborative use

Image from Dirk Dunn

  1. Pull recent changes from remote repository
    • Get most up to date version of the repository
    • This will update your local files
    • git pull
  2. Make changes to a file
  3. Stage the file
    • Choose to take a snapshot of the changes
    • git add [file]
  4. Commit the changes
    • Take a snapshot of the file
    • git commit -m "I made these changes.." [file]
  5. Push changes to remote repository
    • Apply your local changes to the remote repository
    • git push
  6. Rinse, repeat.

Remote use

  1. If you have not already done so, clone UO Data Science git tutorial repository.
cd ~/Desktop
git clone https://github.com/uodatascience/git-tutorial.git
  1. Change directories to the git tutorial folder
cd git_tutorial
  1. Check status
git status
  1. Get most up to date version of the repo
git pull
  1. Open favs.txt file with text editor
# open in text editor app
open /Applications/TextEdit.app favs.txt

# open in terminal using vim
vim favs.txt
  1. Add your favorite R package and save file
# add txt directly from command line
printf "\ndplyr" >> favs.txt
  1. Add favs.txt to the staging area
git add favs.txt
  1. Commit changes
git commit -m "added fav package"
  1. Push changes to github repo
# single master branch
git push

# multiple branches
git push origin [branch name]
  1. Pull newest version from the github repo
# single master branch
git pull

# multiple branches
git pull origin [branch name]

Ignoring files

Sometimes you don't want git to track your files (e.g. if you have data or binary files in the repo). To ignore specific files, create a file called .gitignore and list the files you want to ignore.

# create .gitignore file
touch .gitignore

# add files to ignore
printf ".DS_Store" > .gitignore

Merge conflicts

We're not going to go over merge conflicts in this tutorial, but they happen when two or more people change the file at the same time and the conflicts must be resolved.

More information about how to deal with merge conflicts can be found on the Software Carpentry tutorial

Commonly used commands

# initialize repository
git init

# add file(s) 
git add [file]  # single file
git add .       # all files

# remove file tracking
git rm

# make a snapshot of repository
git commit -m "[message text]"

# copy existing repository
git clone [repo address]

# get newest version of remote repository
git pull

# check newest version of remote repository without merging with your local repository
git fetch

# push changes to remote repository
git push

# view history
git log

# check changes that have been made to files in a repository
git diff

# create new branch
git checkout -b [name of new branch]

# switch branches
git checkout

# check configurations
git config --global --list

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published