Select Page

Overwriting a branch could be dangerous if you have other changes on that branch that you want to keep. So always make sure you have a backup of your work before proceeding. There are various approaches using which we can overwrite a branch and depending upon the approach you choose your previous branch commit history for the branch to be overwritten will be affected.

Approaches

Hard reset

  1. Ensure that you are on the branch that you want to overwrite. If you are not on the branch, use the command below to switch to it.

git checkout <branch-to-be-overwritten>

  1. Use the command below to set the current branch to the same state as the other branch, discarding any changes that are not in the other branch.

git reset --hard <branch-to-overwrite-with>

  1. Use the command below to force-push the updated branch to the remote repository.

git push -f

This will overwrite the branch on the remote repository with the updated branch.

It’s important to note that using git push -f can be dangerous, as it can potentially overwrite changes made by other people. So, it’s recommended to use this command with caution, and communicate with your team members before doing so.

Merge

  1. Make sure you have the latest version of the branch you want to overwrite by pulling any changes from the remote repository.
  1. Check out the branch you want to overwrite

git checkout <branch-to-be-overwritten>

  1. Merge the branch you want to overwrite into the current branch

git merge -s ours <branch-to-overwrite-with>

This will merge the other branch into the current branch but ignore all changes from the other branch.

  1. Push the changes to the remote repository:

git push origin <branch-to-be-overwritten>

This will update the remote repository with the changes you made.

If you want to delete the other branch, you can do so with the following command:

git push origin --delete <branch-to-overwrite-with>

This will delete the other branch from the remote repository. Make sure you have a backup of your work before proceeding as this would permanently delete the overwriting branch.

Rebase

This approach is useful when you want to also apply the existing commits on the branch to be overwritten on top of the overwriting branch so the changes on the branch are not completely lost. In order to apply this approach you need to follow the steps below:

  1. Rebase the remote branch that would overwrite with the branch to be overwritten

git rebase <remote-alias>/<branch-to-overwrite-with> <branch-to-be-overwritten>

  1. Force overwrite the branch with the other remote branch.

git branch -f <branch-to-be-overwritten> <remote-alias>/<branch-to-overwrite-with>

  1. Use the command below to force-push the updated branch to the remote repository.

git push -f

This will overwrite the branch on the remote repository with the updated branch.

Recreate

If you do not care about the branch and are only concerned with having a separate space to duplicate the contents from any one of your existing branches then you can also recreate a new branch.

  1. Delete the existing branch you want to overwrite if you need to have the same branch name

git branch -d <branch-to-be-overwritten>

  1. Check out the branch you want to overwrite with

git checkout <branch-to-overwrite-with>

  1. Pull the latest content from remote if the local branch is behind the remote

git pull

  1. Re-create a new branch with the same name from this branch we want to overwrite with

git checkout -b <branch-to-be-overwritten>

  1. Use the command below to force-push the updated branch to the remote repository.

git push -f

This will overwrite the branch on the remote repository with the updated branch.

Things to Note

It is important to note that overwriting a branch with another branch will lose any uncommitted changes that you have made to the original branch. Therefore, it is important to make sure that you have committed your changes before you overwrite a branch.

Here are some additional things to keep in mind when overwriting a branch with another branch:

  • If you are overwriting a branch that is currently checked out, you will be prompted to confirm the operation.
  • If you are overwriting a branch that is not currently checked out, you will not be prompted to confirm the operation.
  • If you are overwriting a branch that is shared with other users, they will see the changes that you have made when they next pull from the repository.
  • If you are overwriting a branch that is protected, you will need to have the appropriate permissions to do so.