From f25e14b03d3c54a970d7561c47334305b8277005 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 15 Apr 2024 22:06:34 +0300 Subject: [PATCH] Storify force-update-remote-branch --- .../git/s/force-update-remote-branch.md | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/content/snippets/git/s/force-update-remote-branch.md b/content/snippets/git/s/force-update-remote-branch.md index 0fba3fb058e..fab4ce0988a 100644 --- a/content/snippets/git/s/force-update-remote-branch.md +++ b/content/snippets/git/s/force-update-remote-branch.md @@ -1,26 +1,35 @@ --- -title: Update remote branch after rewriting history -type: snippet +title: How can I update a remote branch after rewriting Git history? +shortTitle: Update remote branch after rewriting history +type: question language: git tags: [branch] cover: compass -dateModified: 2021-04-13 +excerpt: Learn how to force update a remote branch after rewriting the Git history locally. +dateModified: 2024-04-15 --- -Forces an update of the remote branch after rewriting the history locally. +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. -- Use `git push -f` to force update the remote branch, overwriting it using the local branch's changes. -- This operation is necessary anytime your local and remote repository diverge. +> [!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. ```shell -git push -f +# Usage: git push -f -# Examples 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 +git push -f +# Force update the remote `patch-1` branch ``` + +> [!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.