Created
August 25, 2023 09:00
-
-
Save 71/afa8c8e5eeea6c9c016b53e971d9a1d5 to your computer and use it in GitHub Desktop.
`git checkout $branch`, but handles files that are assumed not to have changed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use cgo.nu | |
export def main [branch: string] { | |
let result = (do { git checkout $branch } | complete) | |
let stderr = $result.stderr | |
if $result.exit_code == 1 or ($stderr | str contains 'Your local changes to the following files would be overwritten by checkout') { | |
let files_to_save = ($stderr | rg '^\s+(.+)$' -r '$1' | lines) | |
let tmp_dir = (mktemp -d) | |
touch $"($tmp_dir)/EMPTY" | |
for f in $files_to_save { | |
let f_tmp_path = ($tmp_dir | path join $f) | |
mkdir ($f_tmp_path | path dirname) | |
cp $f $f_tmp_path | |
git checkout --ignore-skip-worktree-bits $f | |
} | |
git checkout $branch | |
for f in $files_to_save { | |
git merge-file $f $"($tmp_dir)/EMPTY" ($tmp_dir | path join $f) | |
} | |
rm -rf $tmp_dir | |
} else { | |
echo $stderr | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment