The way to “reinit” repository

前端 未结 3 1134
無奈伤痛
無奈伤痛 2020-12-31 02:49

I\'ve made kind of a big refactoring in my project: I renamed files, removed, added... Besides, I added some folders in .gitignore. However, I\'ve already made a commit to a

相关标签:
3条回答
  • 2020-12-31 02:58

    Here are steps to re-init repository.

    Scenario: I have deleted a locally project folder that I have made git init previously. After that, I would like to update to the existing git repository with a new folder of same project. Then I solved it by following procedure.

    Procedure

    git init
    git add --all
    git commit -am "re-init"
    git remote add origin https://github.com/{yourgit}/{yourproject}.git
    git push --force origin master
    
    0 讨论(0)
  • 2020-12-31 02:59

    UPDATE

    If you have deleted the .git folder (not a great idea), you can create a new clone of the repo and move your stuff to that, and continue there. Something like this

    cd ..
    git clone <remote-repo-url> new-repo
    rm -rf new-repo/*                          // this will not remove new-repo/.git
    cp -f <original-local-repo> new-repo
    cd new-repo
    

    Then continue as below.

    Note that it is better if you can restore the .git folder. Creating a new repo will loose all local repo information you had in the original local repo, like local branches that were never pushed.

    END UPDATE

    You could

    git reset --soft HEAD^
    git add -A .
    git commit -m "rewriting history"
    git push --force origin master
    

    This will back up to the previous commit (while preserving the working tree and index), commit your changes, and then force push that rewritten history to the remote.

    push --force is dangerous stuff. It will disturb others who have already pulled, and is considered very rude, but if no one else have started work on it, that is not a problem.

    This is what is happening:

    --- A -- B  master
    
        ^
        |     
        origin/master
    
    git push
    
    --- A -- B master, origin/master
    
    git reset HEAD^
    git commit -am "rewriting"
    
    --- A -- B origin/master
         \
          \
           B' master
    
    git push --force
    
    --- A -- B
         \
          \
           B' master, origin/master
    
    0 讨论(0)
  • 2020-12-31 03:00

    The lazy way is to just add all the new files and changes, and deletions:

    git add -A .
    

    So long as you don't have any conflicts, this should merge in fine.

    0 讨论(0)
提交回复
热议问题