Git Cheat Sheet

Its hard to memorize all the important Git commands by heart, so I thought it’s better to keep a personal Git cheat sheet for myself. So I don’t want to Google it every time whenever I need to check some syntax. So here it is.

Git Basics

  • Initialize an empty Git repository
    git init

  • Create a local copy of a remote repository
    git clone <repo>

  • Check status of the files are stages, unstaged, and untracked.
    git status

  • Add a file to the staging area.
    git add [file-name.txt]

  • Add all new and changed files to the staging area.
    git add -a

  • Commit changes.
    git commit -m "[commit message]"

  • Remove a file (or folder).
    git rm -r [file-name.txt]

Undoing Changes

  • Create new commit that undoes all of the changes made in , then apply it to the current branch.
    git revert <commit>

  • Remove from the staging area, but leave the working directory unchanged.
    git reset <file>

  • Remove a file (or folder)
    git rm -r [file-name.txt]

Git Config

To set your global username/email configuration:

  • Set your username:
    git config --global user.name "FIRST_NAME LAST_NAME"

  • Set your email address:
    git config --global user.email "MY_NAME@example.com"

To set repository-specific username/email configuration:

  • Set your username:
    git config user.name "first_name last_name"

  • Set your email address:
    git config user.email "my_name@example.com"

Verify your configuration by displaying your configuration file: cat .git/config

Sharing & Updating Projects

  • git push origin [branch name]
    Push a branch to your remote repository

  • git push -u origin [branch name]
    Push changes to remote repository (and remember the branch)

  • git push
    Push changes to remote repository (remembered branch)

  • git push origin --delete [branch name]
    Delete a remote branch

  • git pull
    Update local repository to the newest commit

  • git pull origin [branch name]
    Pull changes from remote repository

  • git remote add origin ssh://git@github.com/[username]/[repository-name].git
    Add a remote repository

  • git remote set-url origin ssh://git@github.com/[username]/[repository-name].git
    Set a repository’s origin branch to SSH

Branching & Merging

  • git branch
    List branches (the asterisk denotes the current branch)

  • git branch -a
    List all branches (local and remote)

  • git branch [branch name]
    Create a new branch

  • git branch -d [branch name]
    Delete a branch

  • git push origin --delete [branch name]
    Delete a remote branch

  • git checkout -b [branch name]
    Create a new branch and switch to it

  • git checkout -b [branch name] origin/[branch name]
    Clone a remote branch and switch to it

  • git branch -m [old branch name] [new branch name]
    Rename a local branch

  • git checkout [branch name]
    Switch to a branch

  • git checkout -
    Switch to the branch last checked out

  • git checkout -- [file-name.txt]
    Discard changes to a file

  • git checkout [branchYouWantToReceiveBranch]
    git merge [branchYouWantToMergeIntoBranch]
    Merge a branch into the active branch

  • git merge [source branch] [target branch]
    Merge a branch into a target branch

  • git stash
    Stash changes in a dirty working directory

  • git stash clear
    Remove all stashed entries

Inspection & Comparison

  • git log
    View changes

  • git log --summary
    View changes (detailed)

  • git log --oneline
    View changes (briefly)

  • git diff [source branch] [target branch]
    Preview changes before merging

 

References: