-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-undoworkingdir
More file actions
executable file
·40 lines (30 loc) · 1.67 KB
/
git-undoworkingdir
File metadata and controls
executable file
·40 lines (30 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
# c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t
# vi: set shiftwidth=4 tabstop=4 expandtab:
# :indentSize=4:tabSize=4:noTabs=false:
set -o nounset
set -o errexit
set -o pipefail
# based on https://gist.github.com/brookinc/e2589a8c5ca33f804e4868f6bfc18282
# That original script stashes the currently staged changes, and leaves everything else in the working directory as-is.
# (source: https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible/39644782#39644782)
# this script extends this as to basically throw away all unstaged changes
# and also makes referencing stashes more robust
function stashref() {
local stashname=$1
git stash list | grep "${stashname}" | head -n1 | cut -d: -f1
}
# Stash everything temporarily. Keep staged files, discard everything else after stashing.
git stash save --quiet --keep-index _tmp_all
# Stash everything that remains (only the staged files should remain). This is the stash we want to keep, so give it a name.
git stash save --quiet _tmp_index
# Apply the original stash to get us back to where we started, then drop that stash
git stash pop --quiet "$(stashref _tmp_all)"
# Create a temporary patch to reverse the stash with just the index and apply it
git stash show -p "$(stashref _tmp_index)" | git apply --index --reverse
# Now we have just the working dir changes without an index, and keep that stash as a backup of the real change here
TS="$(date +%H%M%S)"
git stash save "unstaged@${TS}" | sed 's/and index //'
git stash show -p "$(stashref unstaged@${TS})"
# Now re-apply the index, as index, then drop that stash
git stash pop --quiet --index "$(stashref _tmp_index)"