Skip to content

Instantly share code, notes, and snippets.

@chrismarksus
Created June 7, 2017 13:20
Show Gist options
  • Save chrismarksus/f274cb046efc33d2b5b5a31b9ed64a91 to your computer and use it in GitHub Desktop.
Save chrismarksus/f274cb046efc33d2b5b5a31b9ed64a91 to your computer and use it in GitHub Desktop.
Git Command Session Sheet

Git Terminology:

master default branch we develop in

origin default upstream repo (like Github)

HEAD current branch

remote repository stored on another computer

staging (adding) changed files to index tree to be committed

hooks git commands have hooks that can be used to trigger code (aka script) to run

Common commands

Used rarely but need to know

git init
git clone

Use most frequently

git pull
git push
git checkout
git add
git stash
git log

Helpful to know

git diff
git blame
git reset
git cherry-pick
git merge
git fetch
git rm
git mv
git bisect

Starting a Repo init/clone/remote

git init Create a repo from existing data

git clone (repo_url) Clone a current repo (into a folder with same name as repo)

git clone (repo_url) . Clone a repo into current directory (should be an empty directory)

git clone (repo_url) (folder_name) Clone a repo into a specific folder name

Showing Changes status/diff/log/blame

git status Show the files changed

git diff Show changes to files compared to last commit

git diff (filename) Show changes in single file compared to last commit

git diff (commit_id) Show changes between two different commits.

git log Show history of changes

git blame (filename) Show who changed each line of a file and when


Commit ID: This can be that giant long SHA-1 hash. You can call it many different ways. I usually just use the first 4 characters of the hash.

Undoing Changes reset/revert

git reset --hard HARD^ Go back to the last commit (will not delete new unstaged files)

git revert HEAD Undo/revert last commit AND create a new commit

git revert (commit_id) Undo/revert a specific commit AND create a new commit

Staging Files add/rm

git add (filename) Add a targeted file name

git add -A Stage all files (new, modified, and deleted)

git add . Stage new and modified files (not deleted)

git add -p Stage modified code is patches

git add -i Interactive add

git add -u Stage modified and deleted files (not new)

git rm (filename) Remove a file and untrack it

git rm (filename) --cached Untrack a file only. It will still exist. Usually you will add this file to .gitignore after rm


Git Workflow Trees: How adding and committing moves files between the different git trees.

Working Tree The "tree" that holds all our current files.

Index (after adding/staging file) The "staging" area that holds files that need to be committed.

HEAD Tree that represents the last commit.

Publishing commit/stash/push

git commit -m "message" Commit the local changes that were staged

git commit -am "message" Stage files (modified and deleted, not new) and commit

git stash Take the uncommitted work (modified tracked files and staged changes) and saves it

git stash list Show list of stashes

git stash apply Reapply the latest stashed contents

git stash apply (stash_id) Reapply a specific stash. (stash id = stash@{2})

git stash drop (stash_id) Drop a specific stash

git push Push your changes to the origin

git push origin (local_branch_name) Push a branch to the origin

git tag (tag_name) Tag a version (ie v1.0). Useful for a releases.

Updating and Getting Code fetch/pull

git fetch Get the latest changes from origin (don't merge)

git pull Get the latest changes from origin AND merge

git checkout -b (new_branch_name) origin/(branch_name) Get a remote branch from origin into a local branch (naming the branch and switching to it)

Branching branch/checkout

git branch Show all branches (local)

git branch -a Show all branches (local and remote)

git branch (branch_name) Create a branch from HEAD

git checkout -b (branch_name) Create a new branch and switch to it

git checkout (branch_name) Switch to an already created branch

git checkout -b (new_branch_name) origin/(branch_name) Get a remote branch from origin into a local branch (naming the branch and switching to it)

git push origin --delete (branch_name) Delete a branch locally and remotely

Integrating Branches merge/rebase

git checkout master Switch back to master branch

git merge (branch_name) Merge a specific branch into the master branch.

git rebase (branch_name) Take all the changes in one branch and replay them on another. Usually used in a feature branch. Rebase the master to the feature branch so you are testing your feature on the latest main code base. Then merge to the master.

git cherry-pick (commit_id) Merge just one specific commit from another branch to your current branch.

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