Git - Deal with rejected changes over protected branches
Scenario: main(*) integration branch rejects changes when try to pushing into remote repository. This is a common practice to follow Gitflow (or other branching strategies) to enforce merge reviews into the into the main branch.
So, how to push correctly your changes into main integration branch?
Summary:
- Create a feature branch
- Reset your local main integration branch and sync up with remote main branch
- Push your feature branch and create a Pull Request
Create a feature branch
Create a feature branch from your main integration branch, remember that branches are just named pointers to actual commits, so, new branch will be pointing out to your latest changes, which are the ones youβre trying to push correctly into the integration branch.
git checkout -b [feature-branch-name]
Remember to follow your defined naming conventions to name the new feature branch.
Reset main
Letβs move the local main branch pointer to the same commit that remote branch is referencing.
In this example we assume that is just one commit behind the last commit, in that case we just need to run:
git reset --hard HEAD~1
Where:
- Reset will move the pointer
--hard
just move the pointer, discard all the changesHEAD~1
one commit before current HEAD pointer
The result is something like this:
Push your feature branch and create a PR
Now you can push your changes under the new branch:
git push origin [feature-branch]
And after that you will be able to create a PR to merge your changes with the main integration branch.