Version Control

What is version control?

  • Keeps track of changes to your code.
  • You don’t have to worry about accidentally losing or deleting code.
  • You can experiment and reset to a known good state.
  • Makes collaborating with others easier.

What is git?

  • 'the stupid content tracker'
  • Distributed - everything is kept on your local machine.
  • 'Repository' - a collection of code and history.
  • 'Commit' - a chunk of saved changes.

Getting Started

Some Basic Commands

  • git init Makes a new empty git repository.
  • git add FILE Stages changes in FILE for the next commit.
  • git status Shows the status of the repository.

  • git config --global user.name NAME
  • git config --global user.email EMAIL
  • git config --global core.editor jpico

Committing

# After you've staged some stuff

git commit

Conceptual Demo

Branching

Option 1

# Create a new branch
git branch newBranch

# Check it out
git checkout newBranch

Option 2

# Create and checkout a new branch in one step
git checkout -b newBranch

Merging

Merge changes from another branch into this branch.

If we wanted to merge changes from newBranch into master...

# Switch to master
git checkout master

# Merge changes from newBranch into the current branch (master)
git merge newBranch

If you're lucky, git will do everything for you!

If not...

Merge Conflicts

CONFLICT (content): Merge conflict in some-file.txt Automatic
merge failed; fix conflicts and then commit the result.

In some-file.txt, we see...

<<<<<<< HEAD
The current branch’s contents
=======
Stuff from the branch you’re merging
>>>>>>> newBranch

Resolving a Merge Conflict

  1. Check git status for a list of files with conflicts.
  2. Edit each conflicting file (some-file.txt), so that it looks the way it's supposed to.
  3. Stage all of your changes (git add) and commit (git commit).

Looking at Stuff

  • git log Show a log of commits
    • --graph Neat ASCII graph
    • -p Show what changed in each commit
  • git diff Show uncommitted changes

  • gitk Graphical log
    • --all Show all branches
  • git gui Graphical tool for committing

Remotes

Remotes

It is often (usually) useful to have a central place where you (and your teammates) can store your repository.

By setting up a remote repository, you can more easily backup your work and share it with others.

Working with Remotes

  • git clone REPO downloads a copy of a repository.
  • git push Pushes changes from your current branch to the remote branch it tracks.
  • git pull Pulls changes from the remote branch and merges them into your current branch.
  • git remote allows you to edit git's information about remote repositories

Git Tips

Git Tips

  • Make your commit messages descriptive!
    • Or else!
  • Don’t add generated files (like a.out) to your repo.
  • You can ignore certain files by putting their names in a .gitignore file in your repo.
  • When collaborating, work on separate branches and merge as you go along.
  • git help <command> will show you documentation.