简体   繁体   中英

Git: create a new branch from a commit, delete everythng else

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.

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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM