How to Replace Local Branch With Remote Branch Entirely in Git?
If you want to replace a local branch with a remote branch entirely in Git, effectively discarding all local changes and making your local branch exactly match the remote branch, you can follow these steps:
1. Fetch the Latest Changes from Remote
Before performing any operations, ensure your local repository is up-to-date with the remote repository:
git fetch origin
This command updates your remote-tracking branches without modifying your working directory.
2. Check Out the Local Branch You Want to Replace
Switch to the local branch you want to replace:
git checkout <local-branch>
Replace <local-branch>
with the name of your local branch.
3. Reset the Local Branch to Match the Remote Branch
To make your local branch exactly match the remote branch, use the git reset
command. This will reset the local branch to the state of the remote branch and discard any local changes:
git reset --hard origin/<remote-branch>
Replace <remote-branch>
with the name of the remote branch you want to use to replace the local branch.
Example:
git reset --hard origin/main
This command will:
- Reset the local branch to exactly match the
origin/main
branch. - Discard all local changes in your working directory and staging area.
4. Clean Up Untracked Files (Optional)
If you have untracked files or directories that are not part of the Git repository but exist in your working directory, you can clean them up using git clean
:
Preview What Will Be Deleted:
git clean -n
Remove Untracked Files and Directories:
git clean -fd
f
: Force the clean operation.d
: Remove untracked directories as well.
5. Push Changes to Remote (Optional)
If you want to push this new state of your local branch to the remote repository (if the remote branch has diverged or if you want to overwrite the remote branch), you can force-push the changes:
git push origin <local-branch> --force
Example:
git push origin main --force
Warning: Be cautious with force-pushing as it can overwrite changes on the remote branch and affect other collaborators.
Summary of Commands
Fetch Latest Changes:
git fetch origin
Check Out Local Branch:
git checkout <local-branch>
Reset Local Branch to Match Remote:
git reset --hard origin/<remote-branch>
Clean Up Untracked Files (Optional):
git clean -fd
Push Changes to Remote (Optional):
git push origin <local-branch> --force
By following these steps, you will effectively replace your local branch with the remote branch, ensuring that the local branch exactly matches the remote state and discarding any local changes.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github