- Open the command line (e.g.
/Applications/Utilities/Terminal
on a mac) - To check to see if you have git installed
git --version
- To install on a Mac, type (this will install a pared down version of XCode that includes git)
xcode-select --install
- For other operating systems, following these directions
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 'dani.cosme@gmail.com'
git config --global core.editor 'sublime'
- Go to GitHub to create an account
- Go to UO Data Science
- Add username to list on Slack to be added to the organization
- Accept email invitation
- 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:
- 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
- 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
- Make changes to a file
- Stage the file
- Choose to take a snapshot of the changes
git add [file]
- Commit the changes
- Take a snapshot of the file
git commit -m "I made these changes.." [file]
- Rinse, repeat.
- Make directory on your desktop
mkdir ~/Desktop/git-test
cd ~/Desktop/git-test
- Initialize the repository
git init
- Check what's in the directory
ls -a
- Check status
git status
- Create file with some text
printf "I <3 git" > test.txt
- List files and check status
ls
git status
- Add test.txt file as a tracked file in the repository
git add test.txt
- Check status and check what's different
git status
git diff
- Add a new line of text
printf "\nWhy hello there" >> test.txt
- Check what's different
git diff test.txt
- Check status again
git status
- Stage the changes made to the file
git add test.txt
- Check status again
git status
- Save snapshot of the file by commiting changes
git commit -m "added test file"
- Check version history
git log
- Pull recent changes from remote repository
- Get most up to date version of the repository
- This will update your local files
git pull
- Make changes to a file
- Stage the file
- Choose to take a snapshot of the changes
git add [file]
- Commit the changes
- Take a snapshot of the file
git commit -m "I made these changes.." [file]
- Push changes to remote repository
- Apply your local changes to the remote repository
git push
- Rinse, repeat.
- 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
- Change directories to the git tutorial folder
cd git_tutorial
- Check status
git status
- Get most up to date version of the repo
git pull
- 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
- Add your favorite R package and save file
# add txt directly from command line
printf "\ndplyr" >> favs.txt
- Add favs.txt to the staging area
git add favs.txt
- Commit changes
git commit -m "added fav package"
- Push changes to github repo
# single master branch
git push
# multiple branches
git push origin [branch name]
- Pull newest version from the github repo
# single master branch
git pull
# multiple branches
git pull origin [branch name]
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
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
# 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