Git Commit Mistakes

Let’s say you are working on a feature improvement on the staging branch, and after you have completed your work you go to commit it and after writing your commit message and pressing enter, you remember that you can’t push to the staging branch directly and you need to create a feature branch to push your changes to the repository.

I have made this mistake way to often, and it is frustrating remembering what you need to do to fix it. Here is my approach at fixing the issue.

  1. Create the branch that you should have been working from in the first place.
git branch featureImprovement
  1. Checkout that branch, and push the changes to the repository.
git checkout featureImprovement

git push --set-upstream origin featureImprovement
  1. Your code is up, navigate back to the staging branch and remove the commit that we added.
git checkout staging

git reset --hard HEAD~1

What this does is reverts your changes that you made, and rolls back the head of your branch 1 commit.

I hope this provides help for someone else. If nothing else, I now have an easy place to go to jog my memory in how to fix this issue.