Intro to Git

Brad Rentz

3/21/2017

What is Git?

Git is…

  • A distributed version control system
  • Used to allow multiple people to collaborate on the same code
  • Also useful for managing your own code and larger projects

Applications for language researchers?

  • Coauthoring papers
  • Working on R scripts or other code
  • Sharing code with others (via github or bitbucket)
  • Managing large projects like a dissertation, thesis, or article

What does Git do?

  • Git repositories store the history of files contained in it
  • Changes to files are tracked and can also be undone
  • Also allows for multiple branches which store multiple versions of the same file (production versions vs development versions vs experimental approaches or various draft verions)
  • Git stores this information locally but can also save it remotely on github or other platforms
  • Mainly accessed through terminal or command line

How do we use Git?

The Basic Local Git Workflow

  1. Create Git repository
  2. Add files so that Git tracks them (staging files)
  3. After done editing commit changes (committing files)

Git config

Basic Git configuration if haven’t done so already

# configure the user which will be used by Git
# this should be not an acronym but your full name
git config --global user.name "Firstname Lastname"

# configure the email address
git config --global user.email "your.email@example.org"

Creating a Git Repository

  • Create a folder for the project (or use an existing one)
  • Navigate via terminal/command line to that folder
  • Then run git init
cd ~/Desktop/sample_git
git init
## Reinitialized existing Git repository in /Users/brad_rentz/Desktop/sample_git/.git/

Check Git Status

  • Now that the repository is created we can check the status of the repository
  • You can run this at any point too
cd ~/Desktop/sample_git
git status
## On branch experimental
## Untracked files:
##   (use "git add <file>..." to include in what will be committed)
## 
##  .DS_Store
##  sample/
##  sample2.R
## 
## nothing added to commit but untracked files present (use "git add" to track)

Staging files

  • We have to tell Git which files to track
  • We add them with git add FILENAME
  • Add sample.R
cd ~/Desktop/sample_git
git add sample.R
# too add all R files in directory
# use 'git add *.R'
git status
## On branch experimental
## Untracked files:
##   (use "git add <file>..." to include in what will be committed)
## 
##  .DS_Store
##  sample/
##  sample2.R
## 
## nothing added to commit but untracked files present (use "git add" to track)

Commit files

  • Staging files only tell Git to look for change but not to remember them
  • Commit tell Git to remember how the files are at that moment (it commits all staged files)
  • Use git commit -m "Commit message"
  • -m adds a message argument
cd ~/Desktop/sample_git
git commit -m "First commit"
# the message added should explain the changes
git status
## On branch experimental
## Untracked files:
##  .DS_Store
##  sample/
##  sample2.R
## 
## nothing added to commit but untracked files present
## On branch experimental
## Untracked files:
##   (use "git add <file>..." to include in what will be committed)
## 
##  .DS_Store
##  sample/
##  sample2.R
## 
## nothing added to commit but untracked files present (use "git add" to track)

Modifying a file

  • Let’s change the file to see what happens
  • Make some changes to sample.R then check the git status
cd ~/Desktop/sample_git
# changed the sample.R file
git status
  • What does the git status say?

Commit changes

  • After you modify a file, you can commit it again by first doing git add filename then git commit -m "message" or
  • You can use git commit -a -m "message"
  • -a tells git to commit all changes to previously staged files
cd ~/Desktop/sample_git
git commit -a -m "made some changes"

Summary (so far)

cd ~/Desktop/sample_git # go to the right folder
git init # create repository (only run first time)
git add *.R # add all .R files (staging)
git commit -a -m "made some changes" # commit the files

Branching

What is branching?

  • Branches are different versions of the same code
  • Git allows us to create branches instead of creating a completely new folder for changes
  • Git manages the changes between version for us and even changes what files appear depending on which branch we are in
  • Git can even merge branches back together if needed

Why is branching helpful?

  • Branching allows us to have multiple versions of the code each with separate histories so we can undo certain features on
  • This is useful if we want to experiment with our code with a new analysis while being able to keep code that we know works
  • Can even use this with non-code like text documents
  • Can have multiple versions of papers or dissertations (especially if using LaTeX or R markdown)
    • Modify an article for different purposes
    • Manage comments from committee members or reviewers

Creating new branch

  • To create a new branch use git branch NAME
  • Using git branch will give you a list of all branches
cd ~/Desktop/sample_git
git branch experimental
git branch
## fatal: A branch named 'experimental' already exists.
## * experimental
##   master

Switching between branches

  • To switch between branches use git checkout NAME
  • Notice after switching branches the file name change (even in Finder for macOS)
  • Note: I added a sample3.R to experimental
cd ~/Desktop/sample_git
git checkout master
# see file list before switching branches
ls 
git checkout experimental
# see file list afterward
ls
## Switched to branch 'master'
## sample
## sample.R
## sample2.R
## Switched to branch 'experimental'
## sample
## sample.R
## sample2.R
## sample3.R

Merging branches

  • Sometimes it is helpful to merge branches together
  • First checkout into the branch you want to keep (master)
  • Then use git merge NAME where name is the branch you want to merge into that branch
  • If there are problematic merges, Git won’t complete the merge. If so use git mergetool then git commit -a -m "message"
cd ~/Desktop/sample_git
git checkout master # switch to branch want to keep
git merge experimental # merge experimental into master
git mergetool # only if it won't merge
git commit -a -m "merged experiemental into master" # only if used diff tool

Deleting Merged Branches

  • After a branch is successfully merged into another branch, you can delete it with git branch -d NAME
  • -d will only delete the branch if it has been merged (this is a safe delete)
  • If you created a branch and you don’t like it and want to delete it without merging use git branch -D NAME
cd ~/Desktop/sample_git
git branch -d experimental
git branch crazy-idea # create this branch
git branch -D crazy-idea # nevermind, too crazy, delete it

Using Git with Github

Creating github repository

  • If you are using your own code, the first step is to make a local git repository, which we did already
  • Then on github make a new repository but an empty one
  • After creating it look for directions like these and run them in terminal (you need to cd to your local git repository first. You may need to enter your github username and password):