4/26/2015 - 10:06 AM

Git Command Reference

Git Command Reference

GIT STATUS =====================================================================

  # Show the working tree status

git status [<options>…] [--] [<pathspec>…]

  # Displays paths that: 
  #   have differences between the index file and the current HEAD commit, 
  #   have differences between the working tree and the index file, 
  #   are not tracked by Git but are in the working tree (and are not ignored by gitignore(5)). 
  # The first are what you would commit by running git commit; the second and 
  # third are what you could commit by running git add before running git commit.

# Staged:   Files are ready to be committed.
# Unstaged: Files with changes that have not been prepared to be committed.
# Untracked:Files aren't tracked by Git yet. 
#          This usually indicates a newly created file.
# Deleted:  File has been deleted and is waiting to be removed from Git.

# It's healthy to run git status often. Sometimes things change and you don't notice it.
GIT STASH ======================================================================

  # Stash the changes in a dirty working directory away
git stash list [<options>]
git stash show [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
             [-u|--include-untracked] [-a|--all] [<message>]]
git stash clear
git stash create [<message>]
git stash store [-m|--message <message>] [-q|--quiet] <commit>

  # Use git stash when you want to record the current state of the working directory 
  # and the index, but want to go back to a clean working directory. 
  # The command saves your local modifications away and reverts the working directory 
  # to match the HEAD commit.

  #The modifications stashed away by this command can be listed with git stash list, 
  #inspected with git stash show, and restored (potentially on top of a different 
  #commit) with git stash apply. Calling git stash without any arguments is equivalent 
  #to git stash save. A stash is by default listed as "WIP on branchname …", 
  #but you can give a more descriptive message on the command line when you create one.

  #The latest stash you created is stored in refs/stash; older stashes are found in 
  #the reflog of this reference and can be named using the usual reflog syntax (e.g. 
  #stash@{0} is the most recently created stash, stash@{1} is the one before it, 
  # stash@{2.hours.ago} is also possible).
GIT REMOTE =====================================================================

  #Manage set of tracked repositories

git remote [-v | --verbose]
  #With no arguments, shows a list of existing remotes. 
git remote add  [-t <branch>] [-m <master>] [-f] [--[no-]tags] 
                [--mirror=<fetch|push>] <name> <url>
  # Adds a remote named <name> for the repository at <url>. 
  # The command git fetch <name> can then be used to create and update 
  # remote-tracking branches <name>/<branch>.
git remote rename <old> <new>
  # Rename the remote named <old> to <new>. 
  # All remote-tracking branches and configuration settings for the remote are updated.

git remote remove <name>
  # Remove the remote named <name>. 
  # All remote-tracking branches and configuration settings for the remote are removed.

git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
  # Sets or deletes the default branch (i.e. the target of the symbolic-ref refs/remotes/<name>/HEAD) for the named remote.
git remote set-branches [--add] <name> <branch>…
  # Changes the list of branches tracked by the named remote. 
  # This can be used to track a subset of the available remote branches after the initial setup for a remote.

git remote set-url
  # Changes URL remote points to. 
  # Sets first URL remote points to matching regex <oldurl> 
  # (first URL if no <oldurl> is given) to <newurl>. 
  # If <oldurl> doesn’t match any URL, error occurs and nothing is changed.
git remote set-url [--push] <name> <newurl> [<oldurl>]
  #With --push, push URLs are manipulated instead of fetch URLs.
git remote set-url --add [--push] <name> <newurl>
  #With --add, instead of changing some URL, new URL is added.
git remote set-url --delete [--push] <name> <url>
  #With --delete, instead of changing some URL, all URLs matching regex <url> are deleted. 
  #Trying to delete all non-push URLs is an error.

git remote [-v | --verbose] show [-n] <name>…
  #Gives some information about the remote <name>.

git remote prune [-n | --dry-run] <name>…
  #Deletes all stale remote-tracking branches under <name>. 
  #These stale branches have already been removed from the remote repository 
  #referenced by <name>, but are still locally available in "remotes/<name>".
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)…]
  #Fetch updates for a named set of remotes in the repository as defined by remotes.<group>. 
GIT PUSH =======================================================================

  #Update remote refs along with associated objects
git push  [--all | --mirror | --tags] [--follow-tags] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
          [--repo=<repository>] [-f | --force] [--prune] [-v | --verbose] [-u | --set-upstream]
          [--no-verify] [<repository> [<refspec>…]]

  # Updates remote refs using local refs, while sending objects necessary to complete the given refs.
  # You can make interesting things happen to a repository every time you push into it, 
  # by setting up hooks there. See documentation for git-receive-pack(1).
  # For example, you can upload directly to a webserver whenever you push to your 
  # master remote instead of having to upload your site with an ftp client. 
  # Check out Customizing Git - Git Hooks for more information.

  # When the command line does not specify where to push with the <repository> argument, 
  # branch.*.remote configuration for the current branch is consulted to determine where to push. 
  # If the configuration is missing, it defaults to origin.

  # When the command line does not specify what to push with <refspec>... 
  # arguments or --all, --mirror, --tags options, the command finds the default 
  # <refspec> by consulting remote.*.push configuration, and if it is not found, 
  # honors push.default configuration to decide what to push 
  # (See gitlink:git-config[1] for the meaning of push.default).
-u <dir> <dir>
  #The -u tells Git to remember the parameters, so that next time we can simply 
  #run git push and Git will know what to do. Go ahead and push it. 
GIT PULL =======================================================================

  #Fetch from and integrate with another repository or a local branch
git pull [options] [<repository> [<refspec>…]]

  # Incorporates changes from a remote repository into the current branch. 
  # In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
  # More precisely, git pull runs git fetch with the given parameters and calls 
  # git merge to merge the retrieved branch heads into the current branch. 
  # With --rebase, it runs git rebase instead of git merge.

  # <repository> should be the name of a remote repository as passed to git-fetch(1). 
  # <refspec> can name an arbitrary remote ref (for example, the name of a tag) 
  # or even a collection of refs with corresponding remote-tracking branches 
  # (e.g., refs/heads/*:refs/remotes/origin/*), but usually it is the name of a 
  # branch in the remote repository.

  # Default values for <repository> and <branch> are read from the "remote" and 
  # "merge" configuration for the current branch as set by git-branch(1) --track.
GIT LOG ========================================================================

  #Show commit logs
git log [<options>] [<revision range>] [[--] <path>…]

  # The command takes options applicable to the git rev-list command to control 
  # what is shown and how, and options applicable to the git diff-* commands to 
  # control how the changes each commit introduces are shown.
  # Output a condensed summary of extended header information such as creations, 
  # renames and mode changes.
GIT INIT =======================================================================

  #Create an empty Git repository or reinitialize an existing one
git init  [-q | --quiet] [--bare] [--template=<template_directory>]
          [--separate-git-dir <git dir>]
          [--shared[=<permissions>]] [directory]

  # This command creates an empty Git repository - basically a .git directory 
  # with subdirectories for objects, refs/heads, refs/tags, and template files. 
  # An initial HEAD file that references the HEAD of the master branch is also created.
  # If the $GIT_DIR environment variable is set then it specifies a path to use 
  # instead of ./.git for the base of the repository.

  # If the object storage directory is specified via the $GIT_OBJECT_DIRECTORY 
  # environment variable then the sha1 directories are created underneath - 
  # otherwise the default $GIT_DIR/objects directory is used.
GIT COMMIT =====================================================================

  #Record changes to the repository
git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
           [--dry-run] [(-c | -C | --fixup | --squash) <commit>]
           [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
           [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
           [--date=<date>] [--cleanup=<mode>] [--[no-]status]
           [-i | -o] [-S[<key-id>]] [--] [<file>…]
  # Stores the current contents of the index in a new commit along with a log 
  # message from the user describing the changes.
  # The content to be added can be specified in several ways:
  #   by using git add to incrementally "add" changes to the index before using 
  #     the commit command (Note: even modified files must be "added");
  #   by using git rm to remove files from the working tree and the index, again 
  #     before using the commit command;
  #   by listing files as arguments to the commit command, in which case the 
  #     commit will ignore changes staged in the index, and instead record the 
  #     current content of the listed files (which must already be known to Git);
  #   by using the -a switch with the commit command to automatically "add" 
  #     changes from all known files (i.e. all files that are already listed in 
  #     the index) and to automatically "rm" files in the index that have been 
  #     removed from the working tree, and then perform the actual commit;
  #   by using the --interactive or --patch switches with the commit command to 
  #   decide one by one which files or hunks should be part of the commit, 
  #   before finalizing the operation. 
  # If you make a commit and then find a mistake immediately after that, you can 
  # recover from it with git reset.   
-m <msg>
  # Use the given <msg> as the commit message. If multiple -m options are given, 
  # their values are concatenated as separate paragraphs.
$ git commit -m "Add cute octocat story"
GIT ADD ========================================================================

  # Add file contents to the index

git add   [-n] [-v] [--force | -f] [--interactive | -i] [--patch | -p]
          [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
          [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing]
          [--] [<pathspec>…]
  # updates the index using the current content found in the working tree, to 
  # prepare the content staged for the next commit. 
  # It typically adds the current content of existing paths as a whole, 
  # This command can be performed multiple times before a commit. 
  # The git add command will not add ignored files by default. 

git add -f / git add --force 

  # Allow adding otherwise ignored files.
  # Example:
$ git add Documentation/\*.txt
  # Adds content from all *.txt files under Documentation directory and its subdirectories
  # Wildcard *
$ git add '*.txt'
git add -A
  # You can also type git add -A . where the dot stands for the current directory, 
  # so everything in and beneath it is added. The -A ensures even file deletions are included.
git reset:
You can use git reset <filename> to remove a file or files from the staging area.
Directory:    A folder used for storing multiple files.
Repository:   A directory where Git has been initialized to start version controlling your files.

The .git directory: It is usually hidden. It has all sorts of directories and files inside it. 
              We rarely ever need to do anything inside here 

Staging Area: A place where we can group files together before we COMMIT them to Git.
Commit:       A COMMIT is a snapshot of our repository. 
              This way if we ever need to look back at the changes we have made 
              (or if someone else does), we will see a nice timeline of all changes.

git init    #Create an empty Git repository or reinitialize an existing one

git status  #Show the working tree status

git add     # Add file contents to the index

git commit  #Record changes to the repository

git log     #Show commit logs

git remote  #Manage set of tracked repositories

git push    #Update remote refs along with associated objects

git pull    #Fetch from and integrate with another repository or a local branch

git stash   #Stash the changes in a dirty working directory away