Skip to content

Instantly share code, notes, and snippets.

@csmoore
Last active July 23, 2020 23:39
Show Gist options
  • Save csmoore/d9b6b62e998c4a5ea2b7 to your computer and use it in GitHub Desktop.
Save csmoore/d9b6b62e998c4a5ea2b7 to your computer and use it in GitHub Desktop.
Git Cheat Sheet - list of git commands & examples

Just a list of git commands & examples

CLONE A REPO

git clone

CHECK REPO STATUS

git status

Remotes / Remote Merges

View Remotes

git remote -v

Add remote

git remote add upstream https://github.com/Esri/....

Set a remote to a bogus URL so you don't accidentally push to (if you have write permissions)

git remote set-url upstream --push no_push

Merge from upstream

git fetch upstream git branch -va # only if you want to see the remote branches available git checkout master git merge upstream/master

Abort a merge if there are problems

git merge --abort

STAGE A FILE

git add

CHECK WHAT'S MODIFIED

Check what is modified but not staged

git diff

git diff master

Check what is modifed in staging

git diff --cached git diff --staged

COMMIT CHANGES

Commit without a message

git commit

Commit using a commit message

git commit -m

Skipping the staging area

git commit -a -m

Undo/change a commit

git commit --amend

REMOVE A FILE FROM STAGING AREA BUT NOT FROM LOCAL DISK

git rm --cached

COMMIT HISTORY (USEFUL FOR CODE REVIEW)

git log git log origin/master..master (compare local to remote)

Check the diff in each version and limit to entries

git log -p -

Look at lots of commits at once

git log --pretty= ##For all options look at http://git-scm.com/book/en/Git-Basics-Viewing-the-Commit-History

#BRANCHING

Show current branch

git branch --show-current

Create a branch

git branch

Create a branch based on a previous commit

git branch branchname git branch branchname HEAD~3 (using a symbolic ref)

Switch to the branch

git checkout

Checkout and switch at once

git checkout -b

View Branches

git branch -a (list) git branch -v git branch -va (with remote tracking branches)

git branch -r (view remote branches) git remote update (if for some reason the remote branches don't refresh/aren't current)

Merge a branch

git merge

Delete a branch

git branch -d git branch -d hotfix git branch -D hotfix (force delete even if not merged)

Submodules

git submodule init git submodule update git submodule foreach git pull master

Reseting repo state (warning)

git clean --dry-run git clean -f git clean -f -d (dirs also) git clean -f -X (ignored files also)

git reset --soft HEAD^1 undo last commit, but leave the changes in the working tree git reset --hard origin/master undo (delete) all commits since last time one was pulled from upstream (Watch out !). This should be done while in the master branch: git reset --soft origin/master undo all commits since the last time one was pulled from upstream but leave the changes in the working tree. This should be done while in the master branch:

Stashing changes temporarily

git stash git stash pop git stash list git stash apply git stash apply stash@{2} (If > 1 stash) git stash drop stash@{0}

Submodules

fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule

git submodule update --remote --merge

Cherry Pick Commits

git cherry-pick commit-SHA

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