Git Quick Ref
December 14, 2017
To install (on Linux/Ubuntu):
sudo apt-get install git
For initial setup and global options, run:
To set user and email fields individually:
git config --global user.email <email>
git config --global user.name <name>
To list all of the global config variables:
git config --list
To create a local repository in a existing directory with files already in it:
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
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
.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 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 commit -a -m "<description message>"
To list what branch you’re on, untracked files, staged files:
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 (staging area) and working tree to a particular commit:
git reset --hard <commit>
--soft flag instead to combine several commits into one, by reverting to a commit but preserving your working tree and index (staging area).
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>.
To merge a branch fix with another branch, master:
git checkout master
git merge fix
git branch -d fix
With the last line deleting the fix branch.
Tags can be added to specific points in the history. This can be used for marking release points.
To create a tag of the current local branch:
git tag -a <tag_name>
-a flag signifies an annotated tag which could be used for a version release.
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>
To show the commit history (can add a
-n flag with a specified number to limit the number of commits):
git log -n 3
And to list tags:
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
git push origin --tags