Git Cheat sheet

Git cheat sheet that you can use as a quick reference for common git commands.

Configuration

Configure user information for all local repositories.

  • Set the name you want attached to your commit transactions
    git config --global user.name "[name]"
    
  • Set the email you want attached to your commit transactions
    git config --global user.email "[email address]"
    

Create Repositories

Start a new repository or obtain one from an existing URL.

  • Create a new local repository with the specified name
    git init [project-name]
    
  • Download a project and its entire version history
    git clone [url]
    

Make Changes

Review edits and craft a commit transaction.

  • Lists all new or modified files to be committed
    git status
    
  • Shows file differences not yet staged
    git diff
    
  • Snapshots the file in preparation for versioning
    git add [file]
    
  • Records file snapshots permanently in version history
    git commit -m "[descriptive message]"
    

Group Changes

Name a series of commits and combine completed efforts.

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

Refactor Filenames

Relocate and remove versioned files.

  • Deletes the file from the working directory and stages the deletion
    git rm [file]
    
  • Removes the file from version control but preserves the file locally
    git rm --cached [file]
    
  • Changes the file name and prepares it for commit
    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
    • Example .gitignore content:
      *.log
      build/
      temp-*
      

Save Fragments

Shelve and restore incomplete changes.

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

Review History

Browse and inspect the evolution of project files.

  • Lists version history for the current branch
    git log
    
  • Shows content differences between two branches
    git diff [first-branch]...[second-branch]
    

Redo Commits

Erase mistakes and craft replacement history.

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

Subscribe to dadonk

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe