4

I've cloned an existing project repository through github, which copied over all commit history and branches. I'd like to remove all commit history from the cloned project's master branch and keep the commit history from the branches I've been working with (see diagram). The project looks like this:

A [master]
\
  B [branchDevelop] ________
   \                        \
     C [branchFeature1]      D [branchFeature2]

I've tried rebasing and squashing the commits in master but I keep running into merge conflicts. Also, if I delete .git and reinitialize a project I will loose my branches. How do I delete the commit history of master and retain the development branches and commits that I've been working on?

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
Liver
  • 363
  • 1
  • 3
  • 19
  • The ASCII commit graph isn't very clear. Could you redraw it using a style similar to the one in [this post](http://stackoverflow.com/questions/25488138/move-initial-commits-off-master-to-another-branch-in-git/25490288#25490288)? – jub0bs Sep 07 '15 at 18:45
  • This goes beyond my git knowledge, do [any of these scenarios](https://github.com/blog/2019-how-to-undo-almost-anything-with-git) cover what you've done? – TomNash Sep 07 '15 at 18:48
  • *What I'd like to do now remove all commit history from the cloned project but keep the new branches I've been working with.* Some of the commit history from the original repo has got to be contained in the ancestry of your new branches. What do you mean by "remove all commit history"? – jub0bs Sep 07 '15 at 19:10
  • @Jubobs I'd like to remove all commits from master, as they were from another project. The work done in other branches is specific to the project I am working on so I would like to keep the commit history of those branches. Also, I've updated the ascii picture. – Liver Sep 07 '15 at 19:14
  • Remove all commits from master? Do you mean [squash](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Squashing-Commits) them into one commit? – jub0bs Sep 07 '15 at 19:25
  • What should happen to `branchDevelop`? – jub0bs Sep 07 '15 at 19:49
  • I'd like to keep it as a separate branch with all commit history and child branches (branchFeature1, branchFeature2) if possible. – Liver Sep 07 '15 at 20:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89016/discussion-between-liver-and-jubobs). – Liver Sep 07 '15 at 20:04

2 Answers2

0

First, create a commit on a new branch with the same content as master, but without master's history:

git checkout --orphan new_master master
git commit -m'squashed'

Warning: Severe history rewriting ahead!

  • Do have a backup ready.
  • Do not expect you'll be able to push back to the original remote or pull or merge from it without major trouble!

Then rebase branchDevelop and your feature branches on top of it using git filter-branch:

git filter-branch \
    --parent-filter \
        "test \$GIT_COMMIT = $(git show -s master --format=%H) \
            && echo '-p $(git show -s new_master --format=%H)' \
            || cat" \
    --tag-name-filter "cat" \
    -- \
    branchDevelop branchFeature1 branchFeature2

Lastly, get rid of the old master branch and rename the new one to master:

git branch -M new_master master
das-g
  • 9,718
  • 4
  • 38
  • 80
-1

You can rename your branch as master?

git branch -M branchDevelop master

Make sure this is what you want to do. You can push this using a force push:

git push -f origin master
manojlds
  • 290,304
  • 63
  • 469
  • 417