Crash Course in Git

Here’s a crash course in git.

What is git?

Git is a tool for managing your code.

  • You can create new commits from an existing commit plus changes.

  • You can share those commits with others.

  • Branches are names of commits.

Configuring your info

email and name

Starting a new project

cd $PROJECT
git init

You probably want to setup a .gitignore, README, etc…

Cloning a Project

Find the git URL.

git clone <URL>

You’ll get the master branch from the repo checked out, and it will be named as the origin repo.

Making a Commit

Get the code into the state you want.

Add the files you want to add.

git add <filename>

You can add entire directories as well.

Once you have all the files added (git status can help you see what changes you have made), commit it.

git commit -m "Commit message"

Managing Multiple Repos

Starting a new Branch

git checkout -b <branch name>
git push -u <repo> <branch name>

Checking out a remote branch

git fetch <repo>
git checkout -b <branch name> <repo>:<branch name>

Synchronizing your Code

git pull --rebase

If there are merge conflicts, you’ll have to resolve them. git status will tell you which files are in conflict. Open them up, find the <<<<< and >>>> sections, and change the code to what you want it to be. git add the file(s) and then git rebase --continue. Repeat this at most once per commit since you last synced.

If it is a big mess and you want to start over, git rebase --abort.

Merging Commits

If you have too many changes since your last sync, rebasing can be quite painful. Merge your commits since your last sync into one massive commit:

git log # Find the commit ID of the last sync
git rebase -i # commit ID

In the text editor, change everything but the top commit into ‘s’. The consolidate the commit messages.

Sharing your Commits

You’ll need to set up the remote repository.

git push -u <repo>:<branch name> <branch name>

If someone else has already changed the code since you last synchronized, there will be an error message. Pull down the code with --rebase