Sometimes, while working with Git, you may accidentally commit changes to a feature branch that were meant for a sub-task branch. This mistake can clutter the main feature branch and make your Git history harder to follow. In this post, we’ll explore how to recover from such a scenario and establish a cleaner workflow using Git rewinding and branching techniques.
The Problem
Imagine you’re working on a feature branch named feature/add-user-authentication
. You’ve realized mid-development that some of the changes you made were specific to a sub-task—for example, implementing a new API endpoint—and should have been on a separate branch. You want to:
- Rewind the feature branch to a commit before the accidental changes.
- Create a new branch for the sub-task, based on those changes.
- Maintain clear separation between your feature and sub-task branches for better code review and team collaboration.
The Solution: Step-by-Step
1. Identify the Problematic Commit(s)
Start by determining which commits on the feature/add-user-authentication
branch contain the sub-task changes. Use Git’s log command to review the history:
git log --oneline
For example, let’s say the commit history looks like this:
abc1234 (HEAD -> feature/add-user-authentication) Add API endpoint for user authentication
xyz5678 Refactor database logic
lmn9101 Setup initial authentication module
Here, abc1234
is the commit containing sub-task changes.
2. Create a New Sub-Task Branch
To isolate the sub-task changes, create a new branch starting from the current state of feature/add-user-authentication
:
git checkout -b subtask/add-api-endpoint
This new branch will preserve all the changes, including the problematic commit (abc1234
).
3. Rewind the Feature Branch
Switch back to the feature branch and use Git’s reset
command to rewind its state to the commit just before the accidental changes. In our example, that would be xyz5678
:
git checkout feature/add-user-authentication
git reset --hard xyz5678
Now, feature/add-user-authentication
no longer includes the sub-task changes.
4. Push Changes to Remote
To update the remote repository and synchronize with your team, force-push the rewound feature branch:
git push --force
Next, push the newly created sub-task branch:
git push -u origin subtask/add-api-endpoint
5. Open a Pull Request for the Sub-Task
Now that the sub-task has its own branch, you can open a pull request (PR) from subtask/add-api-endpoint
to feature/add-user-authentication
. This ensures that your changes remain transparent, undergo proper code review, and keep your team’s workflow organized.
Summary
Mistakes happen, but Git provides powerful tools to recover and maintain a clean workflow. Here’s a recap of the steps:
- Identify the problematic commit(s) using
git log
. - Create a new branch for the sub-task from the current state.
- Rewind the feature branch to a clean state with
git reset --hard
. - Push the updated branches to the remote repository.
- Open a pull request for the sub-task branch.
This approach ensures that your Git history remains clear and structured while keeping changes transparent and reviewable for your team. By isolating sub-tasks, you’ll foster better collaboration and avoid future confusion.