Skip to content
Oliver Steele edited this page Feb 16, 2018 · 1 revision

Git Reference

Learn Git

Git Tutorials From Past Semesters

Git & GitHub presentation (thanks: Chris)

One-page cheat sheet (PDF, thanks: Celine)

Git Recipes

.gitignore: Ignoring Files

Get stuff from my computer to GitHub

$ git add <filename>
$ git commit -m "<commit-message>"
$ git push

You can alternatively use git commit -am '<commit-message>' to both add all of the files in your folder and put a message on them, all in one command!

If you forget to include -m in your commit, git will open a text-editor called vim so that you can enter a commit message there. To write your commit message in vim, first press “i”, then write your message, then type :wq for write- quit. Alternatively, if you just want to escape from vim's interface without saving a message, just enter “:q” for quit.

Pull changes from GitHub

$ git pull

-or-

$ git pull origin master

If you get merging errors telling you that you need to merge or 'stash' before you can pull, see the 'stashing' section below. Also, a quick note on pulling, what pulling means is that you're taking the code that others have pushed to your repository and matching what you have on your computer with that, so it incorporates their changes.

How to stash (and what is stashing?)

$ git stash

Stashing stores the copy of your current version of the repository on your computer, so you can keep that copy there before you pull changes that others have made. Often, you'll be prompted to stash before pulling or merging with others. Now that you've stashed, how do you get your stuff back? You'll probably do the following:

$ git stash
$ git pull
$ git stash pop

Git stash will store your changes locally, git pull will download the changes other have made to the repository, and git stash pop puts the changes that you made locally that conflict directly in the code. If there's anything to merge, do it in the file and then commit and push your changes.

Fix a Git detached head

$ git checkout master

If this doesn't work, try:

$ git checkout origin/master

What is branching on Git?

Branches on Git are very similar to branches on trees. The tree trunk is represented as the master branch and the branches that come off of the master branch you can name whatever you want. These branches will start at whatever state master was at the time you made the branch. The master branch can continue changing independently of the branch that you created. You can combine the changes that you make in separate branches by 'merging' them together. For more on merging, look at the sections that will follow. Also, you can view all of the branches in your repository by doing:

$ git branch -a

And you can tell what branch you're on by doing the command:

$ git branch

Make a new branch

$ git fetch origin

$ git checkout -b <your-branch-name> origin/master

This branch will exist on your computer, in order to push it to git and have it be visible by others, you'll have to push your local branch to be a remote branch (see below)

Push your local branch to be a remote branch

$ git push -u origin <your-branch-name>

Check out a branch

'Checking out' a branch is when you pull another person's branch (or teleport to another branch), you do it by doing:

$ git fetch origin
$ git checkout <branch-name>

Resolve merge conflicts

See Resolving a Merge Conflict Using the Command Line on GitHub.

Merge one branch into another

Let's say that you've been working on a branch called dev and you have also been modifying the master branch. You want to merge your changes in the master branch into the dev branch, so that your dev branch is up to date. What you want to do is first get into the dev branch, then:

$ git fetch origin
$ git merge origin/master

If you have any conflicts, deal with them, commit and push your changes, and you're good to go!