From 3b60c65fa638eb41cf8bbfb73b8f613b61c4c8dd Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Wed, 7 Dec 2022 12:06:03 -0700 Subject: [PATCH] Add 3 Rs of git notes --- bin/open_in_browser.sh | 30 ++++ bin/table_of_contents_markdown.awk | 5 +- notes/shell/git_restore_reset_revert.md | 188 ++++++++++++++++++++++++ systemd/{ => system}/xremap.service | 0 vifm/vifmrc | 3 + 5 files changed, 223 insertions(+), 3 deletions(-) create mode 100755 bin/open_in_browser.sh create mode 100644 notes/shell/git_restore_reset_revert.md rename systemd/{ => system}/xremap.service (100%) diff --git a/bin/open_in_browser.sh b/bin/open_in_browser.sh new file mode 100755 index 0000000..064e5fa --- /dev/null +++ b/bin/open_in_browser.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +print_help() { + cat < +HELP +} + +declare -r page="$1" + +browser_open() { + if command -v chromium &>/dev/null; then + chromium --incognito "$page" &>/dev/null + elif command -v firefox &>/dev/null; then + firefox --private-window "$page" &>/dev/null + else + echo "Supported browser not found" + fi +} + +if [[ -z "$page" ]]; then + print_help +elif ! [[ -e "$page" ]]; then + echo "Requested file does not exist" + print_help + exit 1 +else + browser_open "$page" & disown +fi diff --git a/bin/table_of_contents_markdown.awk b/bin/table_of_contents_markdown.awk index 052d9de..b942ab8 100755 --- a/bin/table_of_contents_markdown.awk +++ b/bin/table_of_contents_markdown.awk @@ -17,7 +17,7 @@ # HERE # # EXAMPLE OUTPUT: -# # Table of Contents +# ## Table of Contents # 1. [Main](#main) # - [Hello](#hello) # * [Deep header](#deep-header) @@ -76,8 +76,7 @@ BEGIN { if (heading == 0) heading = 1; } { file[lines++] = $0 } END { - for (i = 0; i < heading; i++) printf "#"; - print " Table of Contents" + print "## Table of Contents" for (t in table_of_contents) print table_of_contents[t] diff --git a/notes/shell/git_restore_reset_revert.md b/notes/shell/git_restore_reset_revert.md new file mode 100644 index 0000000..47f07b6 --- /dev/null +++ b/notes/shell/git_restore_reset_revert.md @@ -0,0 +1,188 @@ +## Table of Contents + 1. [Git Checkout](#git-checkout) + - [Checkout](#checkout) + - [Switch](#switch) + 2. [The 3 Rs of git](#the-3-rs-of-git) + - [Restore](#restore) + - [Reset](#reset) + * [Soft](#soft) + * [Mixed](#mixed) + * [Hard](#hard) + - [Revert](#revert) + - [Further reading](#further-reading) + +# Git Checkout +`git checkout` was, and still is, an overly ubiquitous command with syntax that +wasn't nearly verbose enough. With git v2.23 (2019), this sub-command is now +split into `switch` and `restore` + +We also covert `reset` and `revert`, since they're sort of similar + +## Checkout +Checkout was/is a way to control the state of the git working tree. It chose the +state of your files, which roughly is their branch and commit. However, this +worked way too broadly, consider these examples + +```bash +# Remove all current edits from a file, -- is optional +# It bring back the version at HEAD +git checkout +git checkout -- +git checkout HEAD +git checkout HEAD -- +# Bring back files from an older state +git checkout +git checkout -- +# Shift HEAD over to another branch. Stash uncommitted changes +git checkout +# Shift HEAD over to an old commit (detached HEAD). Stash uncommitted changes +git checkout +``` + +## Switch +Git switch is an all-inclusive command to move the HEAD. It requires uncommitted +changes to be stashed + +```bash +# Set HEAD to another branch +git switch +# Set HEAD to an old commit. It's a detached HEAD +git switch --detach +``` + +It can also create a new branch like `checkout -b` used to do + +```bash +# Create a new branch from HEAD +git switch --create +git switch --create HEAD +# Create a new branch off an old commit +git switch --create +``` + +Managing remote branches is simple. The below renames a local and remote branch, +which showcases deleting and pushing to remotes + +```bash +git branch -m # Rename the branch locally +git push --delete +git push --set-upstream +``` + +# The 3 Rs of git +The general idea of "undoing" is spread between 3 subcommands. Generally given +commit tree + +``` +A - B - C - worktree +``` + +`restore` can make sepecific files look like they looked in commits `A`, `B`, or +`C`. `restore` is the only one that won't modify the commit history and a good +default + +`revert` attempts to undo commits backwards, attaching a new commit `D`, in +which files look similar to themselves from `A`, `B`, or `C`, except for merge +conflicts which are manually resolved + +`reset` can pop off commits, like `B` and `C` and either add them to an +accumulating worktree, or discard them entirely. This is the most raw "undo", +where we can end up with commit history `A - B` + + +## Restore +This is the file-only portion of `checkout`. It operates exclusively on files +and will never shift your HEAD + +Location can be one of `--worktree` or `--staged`. `--source` default to HEAD + +```bash +# Remove unstaged changes from files +git restore +git restore --source HEAD +git restore --worktree --source HEAD +# Unstage a file +git restore --staged +# Bring back files from an older state +git restore --source +``` + +## Reset +This moves back in the commit history, changing different things based on its +"hardness". All examples here assume we have commit tree + +``` +A - B - C <-HEAD +Index: fileA.rs +Worktree: fileB.rs +``` + +The only difference between hardness really is where the files from the previous +commits go. In `soft` it's the index, in `mixed` it's the worktree, and in +`hard` it's essentially `/dev/null` + +### Soft +By softly resetting, all the changes from previous commits go into the index, +alongside current staged changes + +``` + $ git reset --soft + +A - B <-HEAD +Index: fileA.rs fileModifiedInC.rs +Worktree: fileB.rs +``` + +### Mixed +This is the default option. It's identical to a soft reset, except all changes +from previous commits are moved into the worktree + +``` + $ git reset --mixed + +A - B <-HEAD +Index: fileA.rs +Worktree: fileB.rs fileModifiedInC.rs +``` + +### Hard +This option is the most destructive and effectively wipes out any evidence of +the previous commits. Files are moved into the void. Use a `git reflog` to get +them back + +``` + $ git reset --hard + +A - B <-HEAD +Index: fileA.rs +Worktree: fileB.rs +Deleted (not there anymore): fileModifiedInC.rs +``` + +## Revert +This is a sort of inverse to reset. Like reset it undoes entire commits. Unlike +reset, it does this by making a new commit that's the exact opposite of the old +commit + +For example with commits + +``` +A - B - C <-HEAD +``` + +We can `git revert B` to get + +``` +A - B - C - B^-1 <-HEAD +``` + +Which puts us at commit `A` with changes from commit `C`, so it sort of +functions like the below, except our history still clearly shows commit `B` + +``` +A - C <-HEAD +``` + +## Further reading +[How checkout became switch and +restore](https://tanzu.vmware.com/developer/blog/git-switch-and-restore-an-improved-user-experience/) diff --git a/systemd/xremap.service b/systemd/system/xremap.service similarity index 100% rename from systemd/xremap.service rename to systemd/system/xremap.service diff --git a/vifm/vifmrc b/vifm/vifmrc index 9a967e4..ffbb686 100644 --- a/vifm/vifmrc +++ b/vifm/vifmrc @@ -155,6 +155,9 @@ nnoremap oadd :!git add %c " Make and navigate to a directory in the other pane command! cddir :sync! | exe 'wincmd w' | mkdir "%d/%a" | cd "%d/%a" | exe 'wincmd w' +nnoremap bf :!~/.configs_pointer/bin/open_in_browser.sh %f & +nnoremap br :!~/.configs_pointer/bin/open_in_browser.sh %d & + "command! pngcopy :!osascript -e 'set the clipboard to (read (POSIX file "'.%c.'") as PNG picture)' " ===================================================================