Skip to content

Instantly share code, notes, and snippets.

@srebalaji
Created April 10, 2023 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srebalaji/1f523031bf015bc80d548ccc593285ec to your computer and use it in GitHub Desktop.
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
#!/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