Created
April 10, 2023 08:15
-
-
Save srebalaji/1f523031bf015bc80d548ccc593285ec to your computer and use it in GitHub Desktop.
Bash script to checkout to new branch if you have any files with updated index
This file contains hidden or 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
#!/bin/bash | |
# Set the branch name as the first argument passed to the script | |
branch_name=$1 | |
# Cancel the skip-worktree changes for all files | |
git ls-files -v | grep ^S | cut -c 3- | xargs git update-index --no-skip-worktree | |
# Stash all files | |
git stash save --keep-index --include-untracked "Stashing skip-worktree files" | |
# Checkout to the specified branch | |
git checkout $branch_name | |
# Unstash the changes | |
git stash pop | |
# Update the skip-worktree status for the files | |
git ls-files -v | grep ^S | cut -c 3- | xargs git update-index --skip-worktree | |
echo "Done" | |
# To run | |
# ./checkout <branch_name> | |
# Put this file in the alias to use it with ease |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment