Skip to content

Instantly share code, notes, and snippets.

@nexdrew
Last active February 22, 2017 16:36
Show Gist options
  • Select an option

  • Save nexdrew/252f1f923a5d85f238b057feb586239a to your computer and use it in GitHub Desktop.

Select an option

Save nexdrew/252f1f923a5d85f238b057feb586239a to your computer and use it in GitHub Desktop.

Git Basics

Git is a system for storing changes to files in a repository consisting of one or more branches.

Let's briefly define what each of these terms mean.

Repository

A repository is basically just metadata for a directory (or folder) on your local file system. Just like you can have many directories on your file system, you can have many git repositories on your computer. You can think of a repository as a way to store information about the contents of a particular directory (and the changes made to those contents over time).

Use git init to create an empty repository under the current working directory.

Changes to Files

When you make changes to files (including creating them), you have to add the changes and commit the changes to store them in the local git repository.

You add changes (or stage them) to prepare them for committing via the git add <files> command.

Changes that have been added can be committed for permanent storage in the current branch (more on this later) via the git commit command. When you commit a change, you should include a message to describe the change and why it was made. Include a message using the -m "This is my message" option for the git commit command.

Branches

Changes are stored in a "timeline" or "history" called a branch.

A branch? Why a branch? Well, picture a tree. It has a trunk that extends out to many branches, and each branch can extend out to many other branches. In fact, we can even think of the trunk as just a big, central branch. It's the same in git. A branch is a timeline of committed changes. By default, every repository starts with a single branch called master, which you can think of as the tree trunk, and you can easily create branches off of master.

But why create branches? What's the point? Branches allow us to capture a set of changes (one or more) and keep them organized and separate from any other changes. Any time you commit file changes to a git repository, those changes belong to a branch. If you create a repository and never create any branches, all changes will be stored under the default master branch.

Branching has many uses:

  • It allows us to easily switch or compare between different versions of the same file
  • It allows us to organize changes per intent
  • It allows us to merge a whole group of changes from one branch to another all at once
  • It allows us to propose and review a group of changes together before merging/accepting them

How to create a branch.

How to switch between branches.

Use git log to view the history of a branch.

Remotes

By default, a git repository only exists on your local file system, which doesn't do a whole lot of good if your hard disk crashes. Fortunately, it's really easy to copy your local repository to a remote location for redundant storage and even for sharing your work.

Copying changes from a local repository to a remote repository is called pushing. Copying changes from a remote repository to a local repository is called pulling (which uses fetching and merging behind the scenes).

If you're ok with sharing your repository for the whole world to see, you can use GitHub to store your repositories remotely for free! To do so, create a repository on GitHub (typically using the same name as the directory on your local file system), add the new remote to your local repository (named origin by default) via the git remote add <url> command, and then git push your changes to the remote destination.

@DantehL
Copy link
Copy Markdown

DantehL commented Feb 22, 2017

Rlly helpful.

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