Skip to content

Instantly share code, notes, and snippets.

@fubar
Last active May 9, 2022 23:44
Show Gist options
  • Save fubar/8ee31f626a1c4d18dce7 to your computer and use it in GitHub Desktop.
Save fubar/8ee31f626a1c4d18dce7 to your computer and use it in GitHub Desktop.
git cheat sheet

Branches

list branches
git branch [-a to include remote branches]

create branch [from master]
git checkout -b HS-1234 [master]

update master, rebase and push
git checkout master
git pull
git checkout HS-1234
git rebase master
git push -f origin HS-1234

(2-step merge from master)
git fetch
git merge origin/master

change root of a branch
git rebase oldroot --onto newroot

update fork with upstream changes
git fetch upstream
git merge upstream/master

delete branch
local: git branch -D HS-1234
remote: git push origin --delete HS-1234

rename branch
git branch -m HS-4321

Remote Repositories

show repo origin URL (if referential integrity has been broken):
git config --get remote.origin.url

show repo origin URL (if referential integrity is intact):
git remote show origin

set remote repo URL
git remote set-url origin https://github.com/fubar/foo.git

list all remotes
git remote -v

remove remote
git remote rm origin

use a different certificate to authenticate with remote (current repo only):
git config http.sslKey ~/.ssh/my_other_id_rsa
Note: Don't forget to register a newly created key via:
ssh-add ~/.ssh/my_other_id_rsa
See https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

Stage

stage only tracked files
git add -u

Undo

undo git add (unstage)
git reset file.txt

revert changes
git checkout file.txt

undo last commit (back to stage)
git reset --soft HEAD~

Stash

stash and un-stash
git stash
git stash pop
git stash list

show diff of a stash
git stash show -p stash@{0}

apply a specific stash
git stash apply stash@{0}

remove most recent stash
git stash drop

remove all stashes
git stash clear

Patches

apply a single commit (cherry-picking)
git cherry-pick COMMITHASH

create and apply diff file
git diff COMMITHASH^ COMMITHASH > diff.patch
git apply --stat diff.patch
git apply --check diff.patch
git apply diff.patch

Diffs of commits

diff a single commit
git diff COMMITHASH^ COMMITHASH

diff between two commits
git diff COMMITHASH1 COMMITHASH2

view a single commit with commit message
git show COMMITHASH

Diffs of uncommitted changes

show unstaged, uncommitted changes
git diff

show staged changes
git diff --cached

show staged and uncommitted changes
git diff HEAD

for more details, see http://stackoverflow.com/a/1587952/5339584

Change logs

simple list of commits between two hashes/tags
git log FOO..BAR --oneline --no-merges

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