How to Replace Local Branch With Remote Branch Entirely in Git?

Better Stack Team
Updated on August 12, 2024

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

  1. Fetch Latest Changes:

     
    git fetch origin
    
  2. Check Out Local Branch:

     
    git checkout <local-branch>
    
  3. Reset Local Branch to Match Remote:

     
    git reset --hard origin/<remote-branch>
    
  4. Clean Up Untracked Files (Optional):

     
    git clean -fd
    
  5. 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.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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 us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build 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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github