Skip to content

Instantly share code, notes, and snippets.

@JakubTesarek
Created August 1, 2016 09:38
Show Gist options
  • Save JakubTesarek/5a757dc421594472211fabfaf9e553ab to your computer and use it in GitHub Desktop.
Save JakubTesarek/5a757dc421594472211fabfaf9e553ab to your computer and use it in GitHub Desktop.
Git Cheatsheet

Configuration

Display configuration

git config --list

Set global name used for commiting

$ git config --global user.name "[name]"

Set global email used for commiting

$ git config --global user.email "[email address]"

Create repository

Create new local repository

git init [project-name]

Copy repository from remote

$ git clone [url]

Making changes

Lists all files to be commited

$ git status

Snapshots the file for commit

$ git add [file]

Unstages the file, but preserve its contents

$ git reset [file]

Shows file differences not yet staged

$ git diff

Shows file differences between staging and the last file version

$ git diff --staged

Commit all staged changes

$ git commit -m "[message]"

Branching

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]

Merge branch with current branch

$ git merge [branch]

Deletes branch

$ git branch -d [branch-name]

Refactoring

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]

Renames prepares it for commit

$ git mv [file-original] [file-renamed]

History

Lists version history for the current branch

$ git log

Display history tree

$ git log --graph

Lists version history for a file, including renames

$ git log --follow [file]

Display author of all changes in file

$ git blame [file]

Shows content differences between two branches

$ git diff [first-branch]...[second-branch]

Display changes in the specified commit

$ git show [commit]

Reverting changes

Undoes all commits afer [commit], preserving changes locally

$ git reset [commit]

Discards all history and changes back to the specified commit

$ git reset --hard [commit]

Stashing

Temporarily stores all modified tracked files

$ git stash

Lists all stashed changesets

$ git stash list

Restores the most recently stashed files

$ git stash pop

Discards the most recently stashed changeset

$ git stash drop

Sync with remote

Downloads all history from the repository bookmark

$ git fetch [bookmark]

Combines bookmark’s branch into current local branch

$ git merge [bookmark]/[branch]

Uploads all local branch commits remote

$ git push [alias] [branch]

Downloads bookmark history and incorporates changes

$ git pull

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment