GIT Cheatsheet - gecko-8/devwiki GitHub Wiki

Up

Commit Flow

Check Status of Files

git status

Add All Changes To Commit

git add .

Add Commit for Selected Changes

git commit -m "<commit message>"

Push Commits to Remote

Note: Branch name should match your currently checkout out branch.
Note: Remote name is typically "origin" but could be anything if you have multiple remotes setup.
git push <remote name> <branch name>

Repository Management

Clone Repository

git clone <url> <optional directory name>

Clone Repository at a Specific Branch

git clone --branch <branch name> <url> <optional directory name>

Commit Management

List Commits

Note: Listed from latest to oldest
git log

List Commits With Patch (Change) Data

git log -p

List Commits in Friendly Tree Structure

git log --all --decorate --oneline

Branch Management

List Branches

git branch

List All Branches Including Remote

git branch -a

Create New Branch

git branch <branch name>

Checkout Branch

git checkout <branch name>

Checkout Remote Branch

git checkout --track origin/<branch name> Note: This assumes your remote is called origin.

Checkout and Create Branch

Note: This basically combines the Create New Branch and Checkout Branch commands.

git checkout -b <branch name>

Checkout File from Another Branch

Note: This is useful for restoring a file to it's original content (say after accidental change)
git checkout origin/<other branch, e.g. master> <path to file>

Delete Branch

git branch -d <branch name>
git push origin --delete <branch name>

Merge Branch

Note: Make sure you have the destination branch checked out first.

git merge <source branch name>

Abort Merge

git merge --abort

Abort Rebase

git rebase --abort

Reset Branch

git reset --hard <commit hash>
git push -f origin <branch name>  // Only needed if commits were pushed to remote

IMPORTANT: Don't do this if any other team members have pulled the changes from remote.
NOTE: If the branch is protected, the push won't work.

Rename Branch

git branch -m <new name>

Squash Branch to Single Commit

This will create a single commit containing all changes from the branch

git reset --soft $(git merge-base <base branch e.g. master> <your branch>)
git push -f

To summarize what that command does (in my limited understanding):

  • "git merge-base master myBranch" finds the last commit that was common to both branches (most recent ancestor)
  • "git reset --soft " combines all commits after the common ancestor from above, ready for commit.

Remotes

Add a Remote

git remote add <remote name> <remote url>

Stashes

Create Stash With All Untracked Files

git stash --include-untracked

Create Stash With Name

git stash push -m "<name>"

List Stashes

git stash list

Apply Stash

git stash apply
or
git stash pop Applies stash and removes it

Apply Stash by Name

git stash apply stash^{/<name>}
or
git stash pop stash^{/<name>}

Remove Stash

git stash drop

Remove All Stashes

git stash clear

Tags

Create Tag

git tag <tag name>

Push Tag(s) to Remote

git push <remote> --tags

Checkout a Tag

git checkout tags/<tag> -b <branch>

Delete Local Only Tag

git tag -d <tag>

Delete Local and Remote Tag

git tag -d <tag>
git push --delete origin <tag>

Other Operations

Remove All Changes From a Repo

git reset –hard
git clean -fxd

Retrieve Commit History of a File

git log -p -- path/to/file
⚠️ **GitHub.com Fallback** ⚠️