Issue
This article provides a step-by-step guide for recovering and resetting a broken Git branch to a previous stable commit while ensuring that the repository state can be restored if something goes wrong. This solution is useful if an Appsmith app is inaccessible due to a broken git branch and helps recover it.
Resolution
-
Clone the repository and create a local branch to preserve the original broken state of the branch.
git clone <ssh_url> # Clone the repository git checkout -b test/recovery # Create a local recovery branch for the broken branch state
The
test/recovery
branch now serves as a backup of the broken branch in its original state. -
Switch back to the original branch and perform the reset.
git checkout <branch_name> # Switch to the original broken branch git reset --hard <sane_commit_id> # Reset to the specified stable commit
-
Optional: Push the recovery branch to the remote repository to preserve the backup beyond your local machine.
git push origin test/recovery # Push recovery branch to remote
-
Force-push the reset branch to update the remote repository.
Warning: A force push will overwrite the remote branch. Ensure you have communicated with your team about this.git push origin <branch_name> -f # Force push changes to remote
Reverting to the Original State (If Needed)
If the reset does not work as expected, follow these steps to revert to the original state using the recovery branch.
git checkout <branch_name> # Switch to the branch requiring recovery
git merge test/recovery # Merge changes from recovery branch
git push origin <branch_name> # Push merged changes to remote
Clean Up Local and Remote States
Once the debugging session is complete and the issue is resolved, clean up any extra branches to maintain a tidy repository state.
# Delete the recovery branch locally and remotely
git branch -d test/recovery # Delete local recovery branch
git push -d origin test/recovery # Delete remote recovery branch
Additional Useful Git Commands
-
Push the Current Branch to Remote
git push origin <branch_name>
-
Delete a Remote Branch
git push -d origin <branch_name>
-
Delete a Local Branch
git branch -D <branch_name>
Best Practices
- Always create a recovery branch to preserve the original state before making irreversible changes.
- Communicate with your team when force-pushing changes.
- Keep the remote repository clean and only push recovery branches when necessary.
- Clean up extra branches after resolving the issue to avoid confusion.
By following these steps and safety measures, you can confidently reset a broken branch while retaining the ability to restore it if necessary.