如何重置远程 Git 存储库以删除所有提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2006172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:37:39  来源:igfitidea点击:

How to reset a remote Git repository to remove all commits?

gitversion-control

提问by Priyank Bolia

How can I reset a remote and local Git repository to remove all commits?

如何重置远程和本地 Git 存储库以删除所有提交?

I would like to start fresh with the current Head as the initial commit.

我想以当前的 Head 作为初始提交重新开始。

回答by Lilith River

Completely reset?

彻底重置?

  1. Delete the .gitdirectory locally.

  2. Recreate the git repostory:

    $ cd (project-directory)
    $ git init
    $ (add some files)
    $ git add .
    $ git commit -m 'Initial commit'
    
  3. Push to remote server, overwriting. Remember you're going to mess everyone else up doing this … you better be the only client.

    $ git remote add origin <url>
    $ git push --force --set-upstream origin master
    
  1. 删除.git本地目录。

  2. 重新创建 git 存储库:

    $ cd (project-directory)
    $ git init
    $ (add some files)
    $ git add .
    $ git commit -m 'Initial commit'
    
  3. 推送到远程服务器,覆盖。请记住,这样做会使其他所有人都陷入困境……您最好成为唯一的客户。

    $ git remote add origin <url>
    $ git push --force --set-upstream origin master
    

回答by R. Martinho Fernandes

First, follow the instructions in this questionto squash everything to a single commit. Then make a forced push to the remote:

首先,按照此问题中的说明将所有内容压缩为单个提交。然后强制推送到遥控器:

$ git push origin +master

And optionally delete all other branches both locally and remotely:

并可选择删除本地和远程的所有其他分支:

$ git push origin :<branch>
$ git branch -d <branch>

回答by Kwnstantinos Nikoloutsos

Were I you I would do something like this:

如果我是你,我会做这样的事情:

Before doing anything please keep a copy(better safe than sorry)

在做任何事情之前,请保留一份副本(安全总比后悔好)

git checkout master
git checkout -b temp 
git reset --hard <sha-1 of your first commit> 
git add .
git commit -m 'Squash all commits in single one'
git push origin temp

After doing that you can delete other branches.

完成后,您可以删除其他分支。

Result: You are going to have a branch with only 2 commits.

结果:您将拥有一个只有 2 个提交的分支。

Use git log --onelineto see your commits in a minimalistic way and to find SHA-1 for commits!

用于git log --oneline以简约的方式查看您的提交并查找提交的 SHA-1!