Setup

To install on Ubuntu:

$ sudo apt-get install git

For initial setup and global options, run:

$ git config

To set the user and email individually:

$ git config --user.email <email>
$ git config --user.name <name>

To create a local repository in a existing directory with files already in it:

$ cd /path/to/project
$ git init

To create a repository in an empty directory that you will subsequently add files via push or pull (useful when creating a repository on a server):

$ git init --bare

Usage

To add files to be tracked:

$ git add <file>
$ git add *.<ext>
$ git add <folder>

Run this command again to update a staged file after making changes to it after already adding it with git add.

To add all files that were previously tracked but have since been modified:

$ git add -u

To add all files (not including those filtered by .gitignore), including those deleted (thus making the snapshot match the working tree):

$ git add -A

To add only those new and modified files but not delete:

$ git add .

Finally, to commit the staged files:

$ git commit -m "<description message>"

Alternatively you can use the -a flag to stage and commit all files modified or deleted (but not add new files) in one step – allowing you to skip having to do git add.

$ git commit -a -m "<description message>"

Tags

To create a tag of the current local branch:

$ git tag -a <tag_name>

To create a tag at a certain snapshot, get the first several characters of the commit checksum, and run:

$ git tag -a <tag_name> <commit_id>

The -a flag signifies an annotated tag which could be used for a version release.

To list what branch you’re on, untracked files, staged files:

$ git status

To show the commit history (can add a -n flag with a specified number to limit the number of commits):

$ git log
$ git log -n 3

And to list tags:

$ git tag

Add a .gitignore file in the top level of the repository. In it put on separate lines all of the files, extensions, or folders that should not be tracked. See here for good examples.

To create a new branch:

$ git branch <branch_name>

To create a new branch and check it out out immediately:

$ git checkout -b name

To checkout an existing branch:

$ git checkout <branch_name>

To reset the index and working tree to a particular commit:

$ git reset --hard <commit>

Use the --soft flag instead to combine several commits into one, by reverting to a commit but preserving your working tree and index.

Use git commit --amend to add or remove files or change the message to the last commit.

To unstage a file from the index, use git reset <file>.

Remote repositories

To clone an existing repository from somewhere else:

$ git clone <url_to_repository> [local project name]

To create a remote repository on a server for pushing and pulling to, create a bare repository using the --bare option. Then on the local machines set the remote server configuration using:

$ git remote add origin <user@server_ip:/path/to/repository.git>

Then to push to the server:

$ git push origin master

which pushes the master branch to remote origin.

To push a tag to a remote repository, run:

$ git push origin <tag_name>

Or to push all tags use the --tags flag.

$ git push origin --tags

References

  1. Chacon, S. and Straub, B. (2014). Pro Git.
  2. Git documentation. https://git-scm.com/docs/.