Search code examples
gitgithubbranchbfg-repo-cleaner

Reset all branches of a local repo to be the same as remote


First this is different because this is for all branches not one. (All the ones I have found only specify this for a single branch at a time)

I work on a repo that has about 20-30 branches (long story bad practice I know I know)

anyways I was deleting some old commits using the git bfg cleaner and after using it you need to either delete and reclone the code or reset every branch.

I know how to set branches using

git fetch origin
git reset --hard origin/master

But is there a way to reset every branch with one command or do I have to do it one branch at a time?

I have a lot of local files ignored that I do not want to deal with copying and rewriting. (like IDE files, computer config files, etc...)


Solution

  • First this is different because this is for all branches not one

    You can use a script which which will loop over your branches and then do what ever you want on each one:

    # Loop on all the desired branches
    for ref in $(git branch -a)
    do
       # ... DO ANYTHING YOU NEED HERE
       # You have the $ref which stores the branch name
       # ex: remotes/origin/master
    done
    

    Is there a better ans simple solution?

    Yes, simply clone the project again and you will have the same content as your origin.
    This is exactly what you do manually per branch.


    Note:

    You can fetch all the data at once using this command:

    # Fetch all remotes, branches and remove deledted content
    git fetch --all --prune