Forked from f-honcharenko/git-hook-post-checkout.sh
          
        
    
          Created
          October 22, 2025 03:21 
        
      - 
      
- 
        Save officialmofabs/d8e979e25dfa9b1ce172ca71105c95fc to your computer and use it in GitHub Desktop. 
    This Bash script dynamically updates the VS Code `settings.json` file with the current Git branch name. It ensures that any previous dynamically added instructions are removed before appending the new branch name. This is useful for tools like GitHub Copilot Chat to generate commit messages based on the active branch.
  
        
  
    
      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 | |
| # Run the custom script whenever a branch is checked out | |
| "/Users/$USER/Library/Application Support/Code/User/update-copilot-branch-instructions.sh" | |
| # Check if the custom script executed successfully | |
| if [ $? -eq 0 ]; then | |
| # If the script was successful, print the success message | |
| echo "Successfully updated Copilot commit message generation instructions with the current Git branch name." | |
| else | |
| # If the script failed, print an error message | |
| echo "Failed to update Copilot commit message generation instructions." | |
| fi | 
  
    
      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 | |
| # Get current Git branch name | |
| BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD | tr '[:lower:]' '[:upper:]') | |
| # Path to the VS Code settings.json file (update this if necessary) | |
| SETTINGS_FILE="$HOME/Library/Application Support/Code/User/settings.json" # For macOS | |
| # SETTINGS_FILE="$HOME/.config/Code/User/settings.json" # For Linux | |
| # SETTINGS_FILE="$APPDATA\\Code\\User\\settings.json" # For Windows | |
| # Check if settings.json exists | |
| if [[ ! -f "$SETTINGS_FILE" ]]; then | |
| echo "settings.json not found!" | |
| exit 1 | |
| fi | |
| # Format the text with branch name | |
| FORMATTED_TEXT="Current branch name: ${BRANCH_NAME}" | |
| # Clean up existing dynamic branch instruction if it exists | |
| TMP_FILE_CLEAN=$(mktemp) | |
| jq 'if .["github.copilot.chat.commitMessageGeneration.instructions"] | type == "array" then | |
| .["github.copilot.chat.commitMessageGeneration.instructions"] |= map(select(.isItDynamicBranchInstruction != true)) | |
| else . end' "$SETTINGS_FILE" > "$TMP_FILE_CLEAN" | |
| mv "$TMP_FILE_CLEAN" "$SETTINGS_FILE" | |
| # Use jq to update the array property while preserving existing elements | |
| TMP_FILE=$(mktemp) | |
| if ! jq --arg bn "$FORMATTED_TEXT" ' | |
| if .["github.copilot.chat.commitMessageGeneration.instructions"] | type == "array" | |
| then .["github.copilot.chat.commitMessageGeneration.instructions"] += [{"text": $bn, "isItDynamicBranchInstruction": true}] | |
| else .["github.copilot.chat.commitMessageGeneration.instructions"] = [{"text": $bn, "isItDynamicBranchInstruction": true}] | |
| end | |
| ' "$SETTINGS_FILE" > "$TMP_FILE"; then | |
| echo "Error: Failed to update settings.json" | |
| rm "$TMP_FILE" | |
| exit 1 | |
| fi | |
| mv "$TMP_FILE" "$SETTINGS_FILE" | |
| echo "Updated settings.json with the next branch name: ${BRANCH_NAME}." | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment