Created
December 29, 2025 18:13
-
-
Save Daggerpov/9106a952fca26004b2631e741573977a to your computer and use it in GitHub Desktop.
Revert Certain File Changes On a 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 | |
| # Base path to prepend to each file | |
| base_path={PATH HERE} | |
| # Check if any file paths were provided | |
| if [ "$#" -eq 0 ]; then | |
| echo "Usage: $0 <file paths>" | |
| exit 1 | |
| fi | |
| # Iterate over each file path supplied as arguments | |
| for file_path in "$@"; do | |
| # Convert any backslashes (\) to forward slashes (/) | |
| sanitized_path=$(echo "$file_path" | sed 's|\\|/|g') | |
| # Remove any leading slash (/) | |
| sanitized_path=$(echo "$sanitized_path" | sed 's|^/||') | |
| # Prepend the base path for both source and destination paths | |
| full_source_path="$base_path$sanitized_path" | |
| full_dest_path="$base_path$sanitized_path" | |
| # IMPORTANT: modify `main` to your choosing if that isn't the branch you're comparing against | |
| git show main:"$sanitized_path" > "$full_dest_path" | |
| # Check if the command succeeded | |
| if [ $? -eq 0 ]; then | |
| echo "Processed: $full_dest_path" | |
| else | |
| echo "Error processing: $full_dest_path" | |
| fi | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Purpose
This script’s purpose is to revert any file(s) to the state they were in before all of your branch’s changes, as compared to a given branch (I supplied
main, but you can change this accordingly).At work, I found that it was often useful to revert certain files' changes I had made in pull requests, without going through rebasing or switching branches to copy the original ones over -> this came to be the easiest way to do it, and I found it very useful on a regular basis. I even started using this script when working on personal projects and developing pull requests.
Usage:
Two important lines for you to edit:
/Users/daggerpov/Documents/GitHub/{repo_name})mainif that isn't the branch name you'd like to compare againstbash /path/to/revert.sh {files, separated by spaces}Example:
bash /Users/daggerpov/Documents/GitHub/random_code_files/scripts/revert_branch_changes_to_files.sh /Users/daggerpov/Documents/GitHub/Spawn-App-iOS-SwiftUI/Spawn-App-iOS-SwiftUI/Services/DataService/Config/WriteOperationConfig.swift