A Gentle Introduction To Using Git

A Gentle Introduction To Using Git

Terminology

working directory
This is the top-level directory (folder) that contains your project files. Once you initialize git (git init), your working directory will have a new .git directory in it. Everything you do related to your project will be done in this directory.
repository
A collection of changes to the files which make up the project. A repository may be copied to another computer. With git everybody working on a project has a separate, complete copy of the repository. The main point of a repository is to track changes over time.
commit
A set of changes bundled with a message and some metadata (author, timestamp, id et cetera). Changes can include file additions/deletions/renames and/or edits to one or more files. Typically you should try to avoid a single large commit that covers a bunch of unrelated changes. Instead try to make several smaller commits which "evolve" your project incrementally. Each commit has a pointer to its parent commit.
index
The index is a staging area where you can add changes prior to committing them. This way you can commit some changes while leaving other changes for a later commit.
remote
A reference to another copy of your local repository. Services like BitBucket, Github or GitLab provide a place to store and interact with remotes.
origin
When you configure a remote, you may optionally give it a name. If you don't provide a name, git will use the name origin. You may have more than one remote, but only one may be named origin.

Workflow

Create a project directory (<project-name>) and initialize git. You will only need to do this once per project.

cd ~/Code
mkdir <project-name>
cd <project-name>
git init

Create/edit some files, then commit the changes. git add will add files to the index. git commit will open an editor (for a commit message) and then commit whatever is in the index (after saving and closing). Do this anytime you have some changes you need to record.

git add <some-files>
git commit

Once you are finished creating commits, you should push your changes to your remote. Note that if your remote is called origin, you can leave it off.

git push <name-of-remote>

Sometimes you need to get changes that somebody else pushed to the remove. Many tutorials say to use git pull, but I prefer to review the changes first.

git fetch
# pause here to review the changes...
git rebase <name-of-remote>/<name-of-branch>

git pull essentially is a shortcut for git fetch; git rebase .... If you're just getting started with git, just use git pull.

Useful Tools

  • My git aliases, specifically la and l. git is a command-line tool, and it is really useful to see a list of recent commits. For instance use git l -30 to see the thirty most recent commits.
  • tig is a command line tool for browsing git repositories.
  • GitX is a graphical tool for browsing git repositories. Additionally it allows you to stage (add to the index) files, chunks or even individual lines. It also lets you easily make a commit.