While working on a feature or code change, we may need to commit debugging statements or PR comments to the branch where the changes are made.
However, once all the changes are completed and approved, we want to clean up the commit history to help keep the destination branch neat and with helpful commit messages.
While working with a coworker, they cleaned up their comments before merging them into the final branch using the git reset—-soft strategy.
Steps
First, make sure the
<destintion branch>
is up to date git pull originConfirm the source branch is checkout out (
git checkout <branch name>
). If the branch was checked out locally, ensure all changes are synced by git call.Next we are going reset the ref pointer to
<destintion branch>
by runninggit reset --soft <destintion branch>
.Running git status will show what has changed.
Note: This is optional.Depending on what makes a scene, we can now commit our changes in the logical group(s) or one commit.
Please review the git commit command to see the different options.
- Once all the changes are committed, we will not push them back to the origin branch
git push origin <branch name> --force-with-lease
That is all that is needed, and now we have a clean and neat commit(s).
Example
working branch -> feat/adg-01-sample
destination branch -> master
git pull origin master
git checkout feat/adg-01-sample
git reset --soft master
git commit -am "ADG-O2 Sample reset"
git push origin feat/adg-01-sample --force-with-lease
reset --soft master
-> A soft reset in Git allows you to undo changes to your commit history while keeping the working directory intact
-force-with-lease
-> is a safer option that will not overwrite any work on the remote branch if a team member adds more commits. It ensures you do not overwrite someone else's work by force-pushing.
Top comments (0)