Initializing a Repository in an Existing Directory

$ cd C:/Users/user/my_project
$ git init

Start Tracking Files

$ git add *.c
$ git add LICENSE
$ git commit -m 'Initial project version'

Cloning an Existing Repository

$ git clone
$ git clone mylibgit  #renames the original clone

Checking the Status of Your Files

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

Tracking New File

$ git add README

Short Status

$ git status -s 

MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt

New files that aren’t tracked have a ?? next to them, new files that have been added to the staging area have an A,
modified files have an M and so on. There are two columns to the output — the left-hand column indicates the status of
the staging area and the right-hand column indicates the status of the working tree. So for example in that output, the
README file is modified in the working directory but not yet staged, while the lib/simplegit.rb file is modified and
staged. The Rakefile was modified, staged and then modified again, so there are changes to it that are both staged and


You can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:
$ cat .gitignore
*.[oa] *~

# ignore all .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory and any of its subdirectories doc/**/*.pdf

Viewing Your Staged and Unstaged Changes

$ git diff
$ git diff --staged

Committing Your Changes

$ git commit
$ git commit -m "Story 182: fix benchmarks for speed" # -m flag makes an inline commit
$ git commit -a -m 'Add new benchmarks' #-a flag skips git add and commits all tracked files

Removing Files

$ git rm # next commit deletes file and removes tracking. add -f flag for force
$ git rm --cached README # keep the file in your working tree but remove it from your staging area
$ git rm log/\*.log # glob commands are valid

Moving Files

$ git mv file_from file_to


Viewing the Commit History

$ git log
$ git log -p -2 # -p or --patch, which shows the difference, use -2 to show only the last two entries.
$ git log --stat # some abbreviated stats for each commit
$ git log --pretty=format #

Limiting Log Output

$ git log --since=2.weeks
$ git log -S function_name # last commit that added or removed a reference to a specific function
$ git log -- path/to/file # limit the log output to commits that introduced a change to those files

Undoing Things

$ git commit --amend # make the additional changes you forgot, stage them, and commit again
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend

Unstaging a Staged File

$ git reset HEAD  # The file is modified but once again unstaged.

Showing Your Remote

$ git remote -v

Adding Remote Repositories

git remote add pb
git remote add  :

Fetching and Pulling from Your Remotes

git fetch #pulls down all the data from that remote project that you don’t have yet  Does not perform any merge operations
git fetch pb # all the information other user has but that you don’t yet have in your repository

git pull             # automatically fetch and then merge that remote branch into your current branch

Pushing to Your Remotes

$ git push origin master #to push your master branch to your origin server

Inspecting a Remote

git remote show      # It lists the URL for the remote repository as well as the tracking branch information

$ git remote show origin
* remote origin
  Fetch URL:
  Push  URL:
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

Renaming and Removing Remotes

$ git remote rename pb paul # rename pb to paul $ git remote
$ git remote remove paul # all remote-tracking branches and configuration settings associated with that remote are also deleted $ git remote

Listing Your Tags

$ git tag
$ git tag -l "v1.8.5*"

Creating Tags

Annotated Tags
$ git tag -a v1.4 -m "my version 1.4"  #The -m specifies a tagging message, which is stored with the tag
$ git show v1.4  # see the tag data along with the commit that was tagged
Lightweight Tags
$ git tag v1.4-lw # don’t supply any of the -a-s, or -m options

Tagging Later

$ git tag -a v1.2 9fceb02

Sharing Tags

git push origin
$ git push origin --tags # transfer all of your tags to the remote server that are not already there 

Deleting Tags

$ git tag -d v1.4-lw # this does not remove the tag from any remote servers. 
$ git push origin --delete  # preferred method

$ git push origin :refs/tags/v1.4-lw              #null value before the colon is being pushed to the remote tag name, effectively deleting it.

Checking out Tags

$ git checkout v2.0.0 #puts your repository in “detached HEAD” state, which has some ill side effects

Git Aliases

$ git config --global checkout
$ git config --global branch
$ git config --global commit # to git commit, you just need to type git ci $ git config --global status
$ git config --global alias.visual '!gitk' # useful if you write your own external tools that work with a Git repository

Creating a New Branch

$ git branch testing # This creates a new pointer to the same commit you’re currently on.


$ git log --oneline --decorate
f30ab (HEAD -> master, testing) Add feature #32 - ability to add new formats to the central interface
34ac2 Fix bug #1328 - stack overflow under certain conditions
98ca9 Initial commit
$ git log --oneline --decorate --graph --all
* c2b9e (HEAD, master) Made other changes
| * 87ab2 (testing) Made a change

git switch -c new-branch #Create a new branch and switch to it. full flag: –create
git switch -
# Return to your previously checked out branch

git checkout -b <newbranchname> # Creating a new branch and switching to it at the same time


$ git checkout master
$ git merge hotfix


$ git branch -d hotfix
Deleted branch hotfix (3a0874c).