⚠ Warning:

Any uncommitted local change to tracked files will be lost, even if staged.

But any local file that's not tracked by Git will not be affected.


First, update all origin/<branch> refs to latest:

git fetch --all

Backup your current branch (e.g. main):

git branch backup-main

Jump to the latest commit on origin/main and checkout those files:

git reset --hard origin/main

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/main.


Maintain current local commits

[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from main before resetting:

git checkout main
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/main

After this, all of the old commits will be kept in new-branch-to-save-current-commits.

Uncommitted changes

Uncommitted changes, even if staged (with git add), will be lost. Make sure to stash or commit anything you need. For example, run the following:

git stash

And later (after git reset), reapply these uncommitted changes:

git stash pop

Which may create merge conflicts.

Answer from RNA on Stack Overflow
🌐
Medium
medium.com › @FlorentDestrema › a-simple-way-to-clean-up-your-git-project-branches-283b87478fbc
A simple way to clean up your git project branches | by Florent Destremau | Medium
June 3, 2020 - A simple way to clean up your git project branches When you switch between branches and get lost in the names… TD;LR: just use theses git branch -d $(git branch --merged=master | grep -v …
🌐
Git Tower
git-tower.com › learn › git faq › how to use "prune" in git to clean up remote branches
How to Use "prune" in Git to Clean Up Remote Branches | Learn Version Control with Git
5 days ago - Clean up stale Git remote branch references with "prune"! Learn how to use "git fetch --prune" and "git remote prune" to keep your repo tidy.
Discussions

version control - How do I force "git pull" to overwrite local files? - Stack Overflow
Then git pull merges the changes from the latest branch. This did exactly what I wanted it to do.. Thanks! 2014-12-05T17:42:09.43Z+00:00 ... Save this answer. ... Show activity on this post. WARNING: git clean deletes all your untracked files/directories and can't be undone. More on stackoverflow.com
🌐 stackoverflow.com
git - How to get a fresh copy of a branch from the remote repository? - Stack Overflow
My friend's local master branch is apparently a disaster (through accidental merges and commits, I guess). However, his dev branches are fine but contain changes he's not ready to push to remote. ... More on stackoverflow.com
🌐 stackoverflow.com
How can I make a clean pull request using git rebase?
If you are seeing commits from the main branch in your PR, that means you are not up-to-date. You can ensure you are rebasing on top of the latest main branch with: git pull --rebase origin main Tip: you can make rebasing the default too. So you'd just need to do git pull origin main And then your branch would be rebased on top of the latest main. See https://github.com/yawaramin/dotfiles/blob/d7df7b17053ccd8ce0297238e546deddf1a6446b/gitconfig#L42 More on reddit.com
🌐 r/git
7
2
December 7, 2024
git - How to prune local tracking branches that do not exist on remote anymore? - Stack Overflow
With git remote prune origin I can remove the local branches that are not on the remote any more. But I also want to remove local branches that were created from those remote branches (a check if... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/git › git, complete clean and refresh - all branches
r/git on Reddit: Git, complete clean and refresh - all branches
June 10, 2024 -

Hi,

Sometimes I need to refresh the local repository. Basically start from scratch. Practically like removing whole repo and doing a new clone.

I know so far I can do

git fetch

git reset —hard

git clean -fdx

But from my understanding this only treats the checked out branch. Do I really have to script switching to every branch one after another and doing same steps?

🌐
Git
git-scm.com › docs › git-pull
Git - git-pull Documentation
The "remote" repository to pull from. This can be either a URL (see the section GIT URLS below) or the name of a remote (see the section REMOTES below). Defaults to the configured upstream for the current branch, or origin.
🌐
DataCamp
datacamp.com › tutorial › git-pull
Git Pull: Keeping Your Local Repository Up to Date | DataCamp
March 10, 2025 - What’s cool about git pull is that it combines two steps: first, it performs a git fetch to download the latest changes, and then it automatically runs a git merge to integrate those updates into your branch. If you prefer a cleaner history without extra merge commits, you can use git pull ...
🌐
Railsware
railsware.com › home › engineering › git clean-up in local and remote branches, repositories
Git Clean-up in Local and Remote Branches, Repositories | Railsware Blog
May 14, 2023 - $ for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r · Now, you can delete own remote branches, and ask other authors to clean-up theirs:
Find elsewhere
🌐
Techwatching
techwatching.dev › posts › cleaning-git-branches
Clean up your local git branches. - Alexandre Nédélec
April 6, 2020 - We can integrate this script into our git commands by creating a git alias. Let's say I want to create the alias bcl for branch clean up, we only need to add the following to our .gitconfig:
🌐
DEV Community
dev.to › this-is-learning › how-to-clean-up-your-local-repository-with-git-commands-531o
How to Clean Up Your Local Repository with Git Commands - DEV Community
June 16, 2023 - Git is a powerful and popular version control system that helps you manage your code history and collaborate with other developers. However, over time, your local repository may accumulate some unwanted files and branches that clutter your workspace and take up disk space. In this blog post, I will show you how to clean up your local repository with some useful git commands.
🌐
DataCamp
datacamp.com › tutorial › git-pull-force
Git Pull Force: How to Overwrite a Local Branch With Remote | DataCamp
August 6, 2024 - This results in a very thorough cleaning, removing all untracked files in the repository, including build outputs, log files, and other generated artifacts that are typically ignored. The usual way we update our local repository with remote changes is by using the git pull command.
🌐
Graphite
graphite.com › guides › clean-local-branches
Cleaning up local branches in Git - Graphite
Regularly pull changes: Regularly pull changes from the remote to keep your local branches up to date. Consistent naming conventions: Use clear, consistent naming conventions for branches to make management easier. Regular cleanup schedules: Establish regular intervals for branch cleanup to prevent clutter. Review before deletion: Always review branches before deleting, especially when using bulk deletion commands. For further reading on cleaning up branches in Git...
🌐
Netlify
nickymeuleman.netlify.app › blog › delete-git-branches
Clean up old git branches - Nicky Meuleman - Netlify
The codesnippets are meant to be ran in the root of the folder which is a git repository. Start off navigating to the repository you want to clean and switch to the “master” branch. git checkout master · If this repository has a remote, make sure your local version is up to date. git pull ·
🌐
Emily Lahren
emilylahren.com › 2023 › 12 › how-to-clean-up-old-local-branches-with-git
How to Clean Up Old Local Branches With Git – Emily Lahren
December 19, 2023 - The faster way to get rid of all local branches, taught me by a coworker, is the following: “git branch | grep -v “master” | xargs git branch -D”. Note: use this with caution because it will delete everything and you don’t want to delete something that you still need.
Top answer
1 of 16
1585

After pruning, you can get the list of remote branches with git branch -r. The list of branches with their remote tracking branch can be retrieved with git branch -vv. So using these two lists you can find the remote tracking branches that are not in the list of remotes.

This line should do the trick (requires bash or zsh, won't work with standard Bourne shell):

git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

This string gets the list of remote branches and passes it into egrep through the standard input. And filters the branches that have a remote tracking branch (using git branch -vv and filtering for those that have origin) then getting the first column of that output which will be the branch name. Finally passing all the branch names into the delete branch command.

Since it is using the -d option, it will not delete branches that have not been merged into the branch that you are on when you run this command.


If you would like to have this as an alias, add these lines to your .bashrc file:

alias git-list-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}"'
alias git-remove-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}" | xargs git branch -d'

Then, the procedure for pruning becomes:

git remote prune --dry-run origin
git remote prune origin # Removes origin/asdf that's no longer on origin
git-list-untracked 
git-remove-untracked # removes asdf [origin/asdf] where [origin/asdf] no longer exists (after previous command)

If you use squash merges, replace -d with -D. But note that this means that git is not checking whether the branch has been merged before deleting it.

2 of 16
659

If you want to delete all local branches that are already merged into master, you can use the following command:

git branch --merged master | grep -v '^[ *]*master$' | xargs -d'\n' git branch -d

If you are using main as your master branch, you should modify the command accordingly:

git branch --merged main | grep -v '^[ *]*main$' | xargs -d'\n' git branch -d

More info.

NOTE: xargs -d'\n' parameter is used to allow proper deletion of branches with an apostrophe in the name, see https://unix.stackexchange.com/questions/38148/why-does-xargs-strip-quotes-from-input.

🌐
Reddit
reddit.com › r/git › how do i delete remote branches in local .git repo?
r/git on Reddit: How do I delete remote branches in local .git repo?
April 21, 2024 -

Hey people! I just played around with the idea of branches and encountered the following:

I added branches, via git and also via the GitHub Web. Now after deleting all branches in GitHub as well as in my local git repo, I ran git branch -a

On GitHub, those branches don't exist anymore and they shouldn't be present in my local repo. Why are they still listed and is there any way to fully remove those entries?

Help is appreciated, many thanks!

🌐
Reddit
reddit.com › r/git › clean up my master branch, but save work on new branch
r/git on Reddit: Clean up my master branch, but save work on new branch
September 10, 2023 -

I have a repo that has a single 'master' branch which is being used for development. Yeah its a mess. What I want is a clean (useable) master branch and move the development into the it's own branch. My thoughts are to do this :

# create a develop branch, stay on master
git branch develop 
<clean up the master branch, commit changes>
git switch develop
git reset master
# At this point working directory looks just like master did before all this started.
<commit the changes in the working directory, moving the development branch ahead of the master with all the messy changes in place>

At this point I will have a clean master, and develop will look like master used to. Is there a better way to do this?

### Edit: Add example

As a simple example, imagine I have only one file `file.md`. This file has some parts that are finished, and some parts that are under developed, perhaps just placeholders and such. I want to clean things up so that on the master branch, this file can stand on its own and all the under developed parts are moved to some new 'development' branch. My thought is to clean the file up as much as possible without removing the under developed parts, then do the process above to move those to a development branch: create a development branch , but stay on master and then delete all those offending parts. Then switch to the development branch and move the head to the tip of master, leaving all the files *with * those parts in the working directory. I can then commit them onto the development branch.

🌐
freeCodeCamp
freecodecamp.org › news › git-pull-force-how-to-overwrite-local-changes-with-git
Git Pull Force – How to Overwrite Local Changes With Git
July 20, 2020 - As you have probably figured out, ... git pull at all! git fetch is just enough. One thing to note is that by default, git fetch will only bring you changes from the current branch. To get all the changes from all the branches, use git fetch --all. And if you'd like to clean up some of ...
🌐
GitHub
github.com › git-guides › git-pull
Git Guides - git pull · GitHub
You can see all of the many options with git pull in git-scm's documentation. If you're already working on a branch, it is a good idea to run git pull before starting work and introducing new commits. Even if you take a small break from development, there's a chance that one of your collaborators has made changes to your branch.