HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

How can I update a remote branch after rewriting Git history?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
updaterewritingafterremotehowgitcanhistorybranch

Problem

When you rewrite the Git history locally, the remote branch will not be updated automatically. This can lead to a divergence between the local and remote branches. In that case, the remote will refuse to accept the changes, as the history doesn't match. In such as scenario, you need to force update the remote branch.
> [!WARNING]
>
> Rewriting history is a potentially destructive operation. Make sure you understand the implications before proceeding, especially if you are force updating a shared branch.
Using git push -f is the solution to this problem. The -f flag forces the update of the remote branch, overwriting it with the local branch's changes. This operation is necessary anytime your local and remote repository diverge.

Solution

# Usage: git push -f

git checkout patch-1
git pull
git rebase master
# Local `patch-1` branch has been rebased onto `master`, thus diverging
# from the remote `patch-1` branch

git push -f
# Force update the remote `patch-1` branch


>
> Rewriting history is a potentially destructive operation. Make sure you understand the implications before proceeding, especially if you are force updating a shared branch.
Using git push -f is the solution to this problem. The -f flag forces the update of the remote branch, overwriting it with the local branch's changes. This operation is necessary anytime your local and remote repository diverge.
> [!NOTE]
>
> Force updating a remote branch may not be allowed for all users or all branches. Make sure you have the necessary permissions before proceeding.

Code Snippets

# Usage: git push -f

git checkout patch-1
git pull
git rebase master
# Local `patch-1` branch has been rebased onto `master`, thus diverging
# from the remote `patch-1` branch

git push -f
# Force update the remote `patch-1` branch

Context

From 30-seconds-of-code: force-update-remote-branch

Revisions (0)

No revisions yet.