Featured image of post Git Revert Multiple Commits of Protected Branches with Git Restore

Git Revert Multiple Commits of Protected Branches with Git Restore

Master the art of reverting to a specific point with a single command using git restore.

Introduction Link to this section

Protected branches, such as develop and main, are critical for maintaining the integrity of your repository. These branches cannot be reset or force-pushed due to restrictions, making it essential to create new commits to undo changes.

This post explores a simple method to revert changes, using the git restore command, especially when dealing with large or complex modifications.

Why Reverting Matters Link to this section

Reverting changes is a common task in software development, especially when debugging or rolling back unintended modifications. For protected branches, reverting must be done carefully to preserve the branch’s history and avoid conflicts.

The Common Solution Link to this section

A typical approach involves using git revert to create a commit that reverses changes from a range of commits:

1
2
# Revert a range of commits
git revert 4dbcaf5419d5b5aa19776d7ed733a163cacd95fe..dbffeb4da345fd0c66b0b7a8193a6582b409cd4a

However, this method requires analyzing the log to identify commits to revert, which is error-prone. It becomes even more challenging when merge commits are involved, as they have two parents, and git revert requires specifying one parent. Since protected branches often include merge commits, this adds complexity.

The Naive Solution Link to this section

A simpler but less efficient solution involves manually checking out the desired branch or commit in a separate folder:

1
2
# Clone the repository and checkout the branch
git clone {repo} -b main ./mainBranch

Manually delete all files except the .git directory (to remove files added in the commits being reverted) and copy the contents from the target branch, ensuring the .git directory is not copied. Then commit the changes:

1
2
# Commit the reverted changes
git commit -am "Revert changes"

⚠️ DO NOT copy the .git directory to prevent altering the Git index.

The Simpler Solution Link to this section

The git restore command simplifies this process by copying files from the source branch into the current branch without modifying the Git index (unlike git checkout or git reset).

Example: Restore Entire Branch Link to this section

1
2
3
4
5
# Update local branch references
git fetch --all

# Restore files from the source branch
git restore --source=origin/main .

Example: Restore Specific Files Link to this section

You can also restore specific files or use a GLOB pattern to filter files:

1
2
3
4
5
# Update local branch references
git fetch --all

# Restore a specific file
git restore --source=origin/main index.html

This approach is efficient, minimizes errors, and works seamlessly with protected branches.

References Link to this section

💬 Like or have something to add? Leave a comment below.
Ko-fi
GitHub Sponsor
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy