Member-only story
Navigating Git Challenges
Resolving Non-Fast-Forward Errors: A Guide to Syncing Your Git Branches
From Rebasing to Force Pushing: Strategies to Align Local and Remote Git Branches Without Losing Your Work
Even if no one else is working on or has pushed changes to the remote branch, there are a few scenarios where your local branch can appear to be behind its remote counterpart, leading to a non-fast-forward error when you try to push:
Local Rebase or Amend: If you have rebased your local branch or amended commits after your last push, this will alter the commit history. Since the commit IDs change in the process, Git sees your branch as diverged from the remote branch, even if the content changes are the same.
Local Reset: If you have performed a git reset to an earlier commit and made new commits from there, your local branch’s history has diverged from what is on the remote. Even if the remote hasn’t changed, this divergence requires reconciliation.
Force Push to Remote: If you or someone with access to the repository performed a force push (git push — force) to the remote branch after your last pull or clone, the remote history might have changed without adding new commits in a traditional sense. This could make your local branch behind or diverged.
Changes Made Directly on the Remote Repository: Some changes (like editing a file or merging a pull request) can be made directly on GitHub or another remote repository hosting service. These changes update the remote branch without a traditional push from a local repository.
Resolving the Issue
Before pushing again, you’ll need to incorporate the remote changes into your local branch.
This can be done in three ways :
Merging: You can merge the remote changes into your local branch. This will maintain both histories and create a new merge commit:
$ git pull origin feature/BDP-111
After resolving any conflicts and committing them, you should be able to push your changes:
$ git push origin feature/BDP-111