git删除所有Commits提交历史记录,变为全新仓库
2018-01-29 本文已影响0人
貳條
个人感觉一般两个情况下,会想有这个需要:
-
一个是:需要把旧项目提交到Git上,但是原来的某版历史记录中可能会有项目密码等敏感信息。这时候需要删除这些历史记录,形成一个全新的仓库,并且希望保持代码不变。
-
另一个是:处女座!隔三差五看到过往的Commits心烦,没有原因。
1. Checkout
新建一个branch
git checkout --orphan latest_branch
2. Add all the files
git add -A
3. Commit the changes
git commit -am "commit message"
4. Delete the branch
git branch -D master
5. Rename the current branch to master
git branch -m master
6. 连接远程库
把下面的Hzfans替换成你自己的GitHub账户名,Hzfans.github.io替换为你自己的项目名称。
连接后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库。这一步非必要,如果你之前已经连接了的话,以防万一。
git remote add origin git@github.com:Hzfans/Hzfans.github.io.git
7. Finally, force update your repository
git push -f origin master