Skip to content

Latest commit

 

History

History
306 lines (231 loc) · 6.09 KB

Git.md

File metadata and controls

306 lines (231 loc) · 6.09 KB

GIT CHEAT SHEET

This cheat sheet features the most important and commonly used Git commands for easy reference.

Index


SETUP

Configuring user information used across all local repositories

  • Set a name that is identifiable for credit when review version history

    git config --global user.name "[UserName]"
  • Set an email address that will be associated with each history marker

    git config --global user.email "[valid-email]"
  • Set automatic command line coloring for Git for easy reviewing

    git config --global color.ui auto

UNSET

Removes user information configuration across all local repositories

  • UnSet a name

    git config --global --unset user.name
  • UnSet an email address

    git config --global --unset user.email

SETUP & INIT

Configuring user information, initializing and cloning repositories

  • Initialize an existing directory as a Git repository

    git init
  • Retrieve an entire repository from a hosted location via URL

    git clone [url]

STAGE & SNAPSHOT

Working with snapshots and the Git staging area

Git status

Shows modified files in working directory, staged for your next commit

git status

Git add

It adds changes to staging area. This command stages the changes made to files and prepares them for the next commit.

  • To add a specific file

    git add [fileName]
  • To add all the files that are new or changed in the current directory

    git add .
  • To add all changes in the entire working tree, from the root of the repository, regardless of where you are in the directory structure

    git add -A

Git reset

It resets changes in the working directory.

git reset [file]
  • When used with –hard HEAD, this command discards all changes made to the working directory and staging area and resets the repository to the last commit (HEAD).
git reset --hard HEAD

Git diff

It shows changes between commits, commit and working tree, etc. It also compares the branches.

  • Shows the difference the working directory and the last commit

    git diff
  • Shows the difference between the last and second last commits

    git diff HEAD-1 HEAD
  • Compares the specified branches

    git diff [branch-1] [branch-2]
  • Shows the difference of what is staged but not yet committed

    git diff --staged

BRANCH & MERGE

Isolating work in branches, changing context, and integrating changes

Git branch

It lists, creates or deleyes branches in a repository. To delete the branch using git checkout and then run command to delete the branch locally

  • To list branches

    git branch
  • To create a new branch

    git branch [branch-name]
  • To delete a branch

    git branch -d [branch-name]

Git checkout

Switch to another branch and check it out into your working directory

git checkout [branch-name]

Git merge

Merge the specified branch's history into the current one

git merge [branch]

INSPECT & COMPARE

Examining logs, diffs and object information

  • Show all commits in the current branch's history

    git log
  • Show the commits on branchA that are not on branchB

    git log [branchB]..[branchA]
  • Show the commits that changed file, even across renames

    git log --follow [file]
  • Show the diff of what is in branchA that is not in branchB

    git diff [branchB]...[branchA]
  • Show any object in Git in human-readable format

    git show [SHA]

SHARE & UPDATE

Retrieving updates from another repository and updating local repos

  • Add a git URL as an alias

    git remote add [alias] [url]
  • Fetch down all the branches from that Git remote

    git fetch [alias]
  • Merge a remote branch into your current branch to bring it up to date

    git merge [alias]/[branch]
  • Transmit local branch commits to the remote repository branch

    git push [alias] [branch]
  • Fetch and merge any commits from the tracking remote branch

    git pull

TRACKING PATH CHANGES

Versioning file removes and path changes

  • Delete the file from project and stage the removal for commit

    git rm [file]
  • Change an existing file path and stage the move

    git mv [existing-path] [new-path]
  • Show all commit logs with indication of any paths that moved

    git log --stat -M

REWRITE HISTORY

Rewriting branches, updating commits and clearing history

  • Apply any commits of current branch ahead of specified one

    git rebase [branch]
  • Clear staging area, rewrite working tree from specified commit

    git reset --hard [commit]

TEMPORARY COMMITS

Temporarily store modified, tracked files in order to change branches

  • Save modified and staged changes

    git stash
  • List stack-order of stashed file changes

    git stash list
  • Write working from top of stash stack

    git stash pop
  • Discard the changes from top of stash stack

    git stash drop

IGNORING PATTERNS

Preventing unintentional staging or commiting of files

  • Save a file with desired patterns as .gitignore with either direct string matches or wildcard globs.

    logs/
    *.notes
    pattern*/
  • System wide ignore pattern for all local repositories

    git config --global core.excludesfile [file]