代码之家  ›  专栏  ›  技术社区  ›  Jez

如何将一个浅克隆推送到一个新回购?

git
  •  2
  • Jez  · 技术社区  · 6 年前

    git clone --depth=50 https://my.repo
    

    这没问题,但是当我创建一个新的gitlab repo并尝试推它时,我会得到一个错误:

    git remote remove origin
    git remote add origin https://my.repo
    git push -u origin --all
    [...]
     ! [remote rejected] master -> master (shallow update not allowed)
    

    2 回复  |  直到 6 年前
        1
  •  4
  •   Jez    6 年前

    # First, shallow-clone the old repo to the depth we want to keep
    git clone --depth=50 https://...@bitbucket.org/....git
    
    # Go into the directory of the clone
    cd clonedrepo
    
    # Once in the clone's repo directory, remove the old origin
    git remote remove origin
    
    # Store the hash of the oldest commit (ie. in this case, the 50th) in a var
    START_COMMIT=$(git rev-list master|tail -n 1)
    
    # Checkout the oldest commit; detached HEAD
    git checkout $START_COMMIT
    
    # Create a new orphaned branch, which will be temporary
    git checkout --orphan temp_branch
    
    # Commit the initial commit for our new truncated history; it will be the state of the tree at the time of the oldest commit (the 50th)
    git commit -m "Initial commit"
    
    # Now that we have that initial commit, we're ready to replay all the other commits on top of it, in order, so rebase master onto it, except for the oldest commit whose parents don't exist in the shallow clone... it has been replaced by our 'initial commit'
    git rebase --onto temp_branch $START_COMMIT master
    
    # We're now ready to push this to the new remote repo... add the remote...
    git remote add origin https://gitlab.com/....git
    
    # ... and push.  We don't need to push the temp branch, only master, the beginning of whose commit chain will be our 'initial commit'
    git push -u origin master
    

        2
  •  0
  •   codewario    6 年前

    unshallow 先克隆。用 --unshallow

    git fetch --unshallow old

    git rebase master

    git rebase --onto master~y master~x master

    x 是要保留的第一个提交的数目,并且 y git log

    小心,因为改写历史可能是Git中的一件危险的事情,而且还有其他需要考虑的含义。另外,请确保不要将更改推送到旧遥控器,除非您也要删除旧的历史记录。

    https://www.clock.co.uk/insight/deleting-a-git-commit