Push after rebase

In this post, you’ll know how to perform a successful git push after rebase. Pushing changes to the remote origin is nothing new. But there are some situations where the push may be rejected. Or it can override someone else’s progress.

After you rebase, the commit history changes. So you need to push with the “–force” flag. If you encounter a “git push rejected” error after rebasing a feature branch, here are some steps you can follow to fix it.

git push rejected because remote contains work

Double-check your changes

After rebasing a feature branch, ensure you have resolved conflicts and staged the changes using the git add -A command.

Update your local branch

Ensure that your local branch is up-to-date with the latest changes from the remote repository. You can do this by running git pull command to fetch. And you can merge the latest changes from the remote repository into your local branch.

If it helped you, please share this post on your website.

Perform a forceful push after git rebase

This is the advice that I gave you at the very beginning of this post. Since you have rebased your feature branch, the commit history changed. So you need to force-push your changes to the remote repository.

You can do this using git push command with the “-f” or “--force” flag. See the example below:

git push origin branch_name --force

Or git push -f origin branch_name

git push with --force flag that worked after previous push was rejected

You have to replace the branch_name with the actual name.

Communicate with your team

If you are working in a team, it’s very important to communicate with other members before you do a forceful push. Because it can affect others who have cloned or pulled the repository.

Make sure everyone is aware of the changes you are making and coordinate accordingly.

Instead of the “--force“, there is a better alternative which is “--force-with-lease.” The difference is you’ll get a warning if anyone else made a commit to the same branch.

Resolve any further issues

Only pushing with a “--force” flag is not always the solution. Sometimes it can be dangerous if you’re not sure what you’re doing.

Not only after rebase but also every time you perform a git push with the “--force” flag, you have to make sure that you’re not bringing everyone (team members) to the same day when they get started.

After you confirm these factors and if the push is still rejected, double-check for any other issues, such as:

If necessary, consult with your team or repository administrator to resolve any further issues.

Learn more about Git

Conclusion

If you’re the only person who is working on a branch, then it’s okay to rebase a feature branch and perform a force push to the master.

If you know what exactly you’re doing and if you’re sure that you’re not going to override someone else’s work, don’t hesitate to perform a forceful git push after the rebase (--force). But if you have confusion, make the push with the --force-with-lease flag.