Skip to content

Instantly share code, notes, and snippets.

@hbillings
Last active December 12, 2016 15:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbillings/aa44f82fbd0b8c3a56ce70cd4da46aab to your computer and use it in GitHub Desktop.
Save hbillings/aa44f82fbd0b8c3a56ce70cd4da46aab to your computer and use it in GitHub Desktop.
Basic git commands

#GitHub vs git

  • git (lowercase) is a version control system. There are many like it, but this one is ours. It is free and available for anyone to use. git is usually used from a command prompt.
  • GitHub is a company that allows you to save your code to their servers so that anyone can access it from anywhere. GitHub relies on git to manage the state of your code. GitHub has created an app that you can use to issue git commands, so you don't have to use the command line if you don't want.

This is a getting-started guide for the command line.

#Terminology repo: Short for "repository." This is your code's home. All of your code's change history is kept here, too.

branch: A copy of the repo. One repo will have many branches. Ever made copies of a document to save prior states, like "essay-edits.doc," "essay-v2.doc", "essay-final-final.doc"? Those are basically all branches of a document called "essay.doc." git branches are similar, except they make it easy to fold your edits back into the main branch (which is usually called something like master or develop).

commit: A granular change that is ready to become part of your branch. You will select files to commit, write a message describing the change, and save the change to the branch.

#Common tasks This is far from a complete list of things you can do or need to do with git, but my goal is to make the general workflow simpler to understand for those new to git and the command line.

##Getting code from GitHub onto your computer git clone your-repo-url

Go to the repo on GitHub (for instance, https://github.com/18F/calc). Click the "Clone or Download" button and use the SSH URL. This will create a folder named, in this instance, calc. (You only need to do this once per repository.)

##See which branches are available locally git branch -l

(The -l is called a flag in command-line speak and it stands for "list.")

##Make new branches available locally git fetch

Say you know someone has pushed up a new branch, and you want to work on that. This will make your local codebase aware of new branches.

##Change which branch you're on git checkout a-branch-name

##Making a new branch git checkout -b your-branch-name

You do this if you are making changes that might break the main repo, experimenting, or otherwise creating things that need to be approved and reviewed.

##See which files you've changed git status (Shows a list of files changed.)

##Choosing changes to save with git git add your-file-name

-or-

git add directory/*

(adds everything in a directory)

-or-

git add .

(adds everything that has changed)

##Saving changes with git git commit -m "a message describing the changes made"

##Getting changes that other people may have pushed to the branch you're on git pull origin your-branch-name

##Making your changes available to other people working on your branch git push origin your-branch-name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment