0

What exactly should I do in order to

  • create a new branch from a certain commit (git checkout -b new_one; git cherry-pick commit_id, am I right?)
  • delete everything else
  • rename branch new_one to master?

Of course, any change should be also pushed to remote.

Thanks.

3 Answers3

2

In order to obtain a fresh master that only holds one commit, the easiest is to create a fresh repo and then put the current state of master into it as initial state. Otherwise, you'll always have the history of master present:

  1. Export the current state of master (see https://stackoverflow.com/a/163769/520162)
    git archive --format zip --output /full/path/to/zipfile.zip master
  2. unzip the created zip file where your new repo shall be present
  3. Initialize a new repo in that directory:
    git init .
  4. Create your initial commit:
    git add . && git commit -m "initial"
  5. Assign your remote:
    git remote add ...
  6. push the new state of master there:
    git push -f origin master:master

Then, your repo will have only one branch (master) with only one commit.

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
1

Create new_branch starting it off commit_id:

git branch new_branch commit_id

Check out to it:

git checkout new_branch

Delete old master branch:

git branch -d master   # you may need to use -D, with history loss may occur

Rename new_branch to master:

git branch -m new_branch master

Note that this does not necessarily remove all history - even if you perform full garbage collection, any objects that commit_id was referring to will remain in git history (and git object store).

mvp
  • 111,019
  • 13
  • 122
  • 148
0

I would try something like this:

git branch tmp <the very first commit in the repo>
git checkout tmp
git merge --squash <commit_id>
git branch -d master
git branch -m tmp master

... followed by a forced push

Martin G
  • 17,357
  • 9
  • 82
  • 98
  • First, you obviously need to `git checkout tmp` after the first step. Second, these commands still leave you with a repo with the only commit, which is the very first commit. Which is not what I was asking about (yes, you still can add, commit and push actual files anew, but I think there should be a simpler way, shouldn't it?) – Alasdair Gray Jul 21 '14 at 10:32
  • Yes, you do need to checkout tmp. I fixed that. Merge --squash will merge to tmp and create one single commit on tmp containing everything in . – Martin G Jul 21 '14 at 10:41