Git Cheat Sheet

Install Git

Or you could install with command line:

  • For Mac OS
1
$ git --version
  • For Linux/Unix
1
2
3
4
5
$ sudo dnf install git-all

or

$ sudo dnf install git-all

Configure Tooling

Configure user information for all local repositories

  • Sets the name you want attached to your commit transactions
1
$ git config --global user.name "[name]"
  • Sets the email you want attached to your commit transactions
1
$ git config --global user.email "[email address]"
  • Enables helpful colorization of command line output
1
$ git config --global color.ui auto

Create Repositories

Start a new repository or obtain one from an existing URL

  • Creates a new local repository with the specified name
1
$ git init [project-name]
  • Downloads a project and its entire version history
1
$ git clone [url]

Make Changes

Review edits and craft a commit transaction

  • Lists all new or modified files to be committed
1
$ git status
  • Shows file differences not yet staged
1
$ git diff
  • Snapshots the file in preparation for versioning
1
$ git add [file]
  • Shows file differences between staging and the last file version
1
$ git diff --staged
  • Unstages the file, but preserve its contents
1
$ git reset [file]
  • Records file snapshots permanently in version history
1
$ git commit -m "[descriptive message]"

Group Changes

Name a series of commits and combine completed efforts

  • Lists all local branches in the current repository
1
$ git branch
  • Creates a new branch
1
$ git branch [branch-name]
  • Switches to the specified branch and updates the working directory
1
$ git checkout [branch-name]
  • Combines the specified branch’s history into the current branch
1
$ git merge [branch]
  • Deletes the specified branch
1
$ git branch -d [branch-name]

Refactor Filenames

Relocate and remove versioned files

  • Deletes the file from the working directory and stages the deletion
1
$ git rm [file]
  • Removes the file from version control but preserves the file locally
1
$ git rm --cached [file]
  • Changes the file name and prepares it for commit
1
$ git mv [file-original] [file-renamed]

Suppress Tracking

Exclude temporary files and paths

  • A text file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns
1
2
3
$ *.log
$ build/
$ temp-*
  • Lists all ignored files in this project
1
$ git ls-files --other --ignored --exclude-standard

Save Fragments

Shelve and restore incomplete changes

  • Temporarily stores all modified tracked files
1
$ git stash
  • Restores the most recently stashed files
1
$ git stash pop
  • Lists all stashed changesets
1
$ git stash list
  • Discards the most recently stashed changeset
1
$ git stash drop

Review History

Browse and inspect the evolution of project files

  • Lists version history for the current branch
1
$ git log
  • Lists version history for a file, including renames
1
$ git log --follow [file]
  • Shows content differences between two branches
1
$ git diff [first-branch]...[second-branch]
  • Outputs metadata and content changes of the specified commit
1
$ git show [commit]

Redo Commits

Erase mistakes and craft replacement history

  • Undoes all commits after [commit], preserving changes locally
1
$ git reset [commit]
  • Discards all history and changes back to the specified commit
1
$ git reset --hard [commit]

Synchronize Changes

Register a repository bookmark and exchange version history

  • Downloads all history from the repository bookmark
1
$ git fetch [bookmark]
  • Combines bookmark’s branch into current local branch
1
$ git merge [bookmark]/[branch]
  • Uploads all local branch commits to GitHub
1
$ git push [alias] [branch]
  • Downloads bookmark history and incorporates changes
1
$ git pull
Would you mind buy me a cup of coffee?