undo merge that hasn't been pushed
git reset --merge HEAD~1
roll back hard
git reset --hard <commit/tag>
force push of a previous commit
git push -f origin <commit_id>:<branch>
delete remote branch
git push --delete origin <branch>
reset local branch after a forced-update (above)
git fetch
git reset origin/<branch> --hard
renaming branch and updating remote
git branch -m old-name new-name
git push origin --set-upstream new-name
git push origin :old-name
set username for single repo
git config user.username 'name'
set signing key for local repo
git config user.signingkey <id>
signing commits
git commit -S -m 'msg'
compare diff between two commits
git diff <commit>...<commit>
compare file diff between two branches
git diff newbranch main -- file.txt
git diff newbranch..main -- file.txt
using the latter syntax, if either side is HEAD it may be ommitted (e.g., main.. compares main to HEAD)
adding separate hunks from file
git add --patch <filename>
git add -p <filename>
options:
-
ystage this hunk for the next commit -
ndo not stage this hunk for the next commit -
qquit; do not stage this hunk or any of the remaining hunks -
astage this hunk and all later hunks in the file -
ddo not stage this hunk or any of the later hunks in the file -
gselect a hunk to go to -
/search for a hunk matching the given regex -
jleave this hunk undecided, see next undecided hunk -
Jleave this hunk undecided, see next hunk -
kleave this hunk undecided, see previous undecided hunk -
Kleave this hunk undecided, see previous hunk -
ssplit the current hunk into smaller hunks -
emanually edit the current hunk - You can then edit the hunk manually by replacing
+/-by# -
?print hunk help
stash
git stash
git stash show
unstash
git stash pop
add remote origin
git remote add origin git@gitserver/path/to/repo
add multiple push repos
git remote set-url --add --push origin git@gitserver/original/repo
git remote set-url --add --push origin https://gitserver/another/repo
archive branch
git archive --format zip --outpu /path/to/output.zip <branch>
using hub
pull requests
hub pr list
hub pr checkout <num>