Git: Common Workflows
- Muhammad Abdullah
- Jul 30
- 2 min read
This post outlines some of the most common git workflows I've come across while working as a software engineer.
Move current changes to new branch and revert parent branch to a previous commit
Suppose you have finished a feature in a feature branch and created a PR to merge the feature into main. While the PR is being reviewed (it could be days), you forgot to checkout to a new branch for the next work and kept committing further changes in the same branch. First of all, the PR reviewer will see those new changes in the PR automatically. Second, if the reviewer returns something back from the PR, how will you send him back just that fix and none of the new changes you've accidentally added to the branch.
You need a way to move all of the new changes to a separate branch (as they should have been from the beginning), revert the branch that's connected with the PR to the point when you requested the review, and then add to it, the fix the reviewer requested. So that in the end, your PR contains work that's only related to the feature it's for and you can also continue on the new changes in a separate branch going forward.
Here’s how you can create a new branch with the new changes and revert the parent branch to a previous commit using Git:
1. Create a New Branch from Current State
First, make sure you’re on the parent branch (e.g., main or develop):
git checkout main
Create a new branch (replace feature-branch with your desired branch name):
git checkout -b feature-branch
This new branch will contain all the current changes from main.
2. Revert Parent Branch to a Previous Commit
Switch back to the parent branch:
git checkout main
Find the commit hash you want to revert to (use git log to see commit history):
git log
Reset the parent branch to the desired commit (replace <commit-hash> with the actual hash):
git reset --hard <commit-hash>
Warning: --hard will discard all changes after the specified commit. If you want to keep the changes as uncommitted files, use --soft instead.
3. Push Changes to Remote (if needed)
If you want to update the remote branch:
git push origin main --force
Warning: Force pushing will overwrite the remote branch history. Make sure this is what you want, and coordinate with your team if necessary.
Comments