troubleshooting Question

Remove all Git Commits in each branch keeping most recent code

Avatar of phoffric
phoffric asked on
ProgrammingProgramming Languages-OtherGit* Bitbucket
74 Comments17 Solutions162 ViewsLast Modified:
In Bitbucket, we have master branch, and four developer branches, named A, B, C, D. Each branch has 10-100 commits in them. I want to keep the latest code in the respective branches, and start fresh without changing the Bitbucket URL. I would like to remove all the commits from A, B, C, D but leave the lastest code except for one commit in each one.

Here is one way I thought of (and have not tested this idea yet since I am hoping there is a better way).
  1. I git clone the repo 5 times in different local repos.
  2. In each of the 5 clones:
    1. I checkout a different branch - one of the 5 original branches.
    2. I delete the .git folder.
    3. I go a git init. (Now I think the master has the latest code in the respective branch.)
    4. I set origin to the Bitbucket URL
    5. I create and checkout the respective branch and do a git push to get the branch pushed to Bitbucket.
  3. I delete the 5 clones.
  4. I do a git clone to get the latest Bitbucket repo that now has only one commit per branch (I think).
At this point, if any developer looks at Bitbucket in their respective branch, then I hope they would see exactly what they had in there before I completed the above steps (but without history or their commits).

Is there a way to accomplish this goal without having to do a clone 6 times?
Actually, I would rather have nothing in the master branch. I will need to work with the developers to have a common folder tree structure, and then start merging.
Duncan Roe
Software Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 17 Answers and 74 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 17 Answers and 74 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004