使當前提交成為Git存儲庫中的唯一(初始)提交?我目前有一個本地Git存儲庫,我將其推送到Github存儲庫。本地存儲庫有~10個提交,Github存儲庫是這個的同步副本。我想要做的是從本地Git存儲庫中刪除所有版本歷史記錄,因此存儲庫的當前內容顯示為唯一的提交(因此存儲庫中的舊版本文件不會存儲)。然后,我想將這些更改推送到Github。我已經調查了Git rebase,但這似乎更適合刪除特定版本。另一個可能的解決方案是刪除本地倉庫,并創建一個新的倉庫 - 雖然這可能會創造很多工作!ETA:有一些未跟蹤的特定目錄/文件 - 如果可能的話,我想保持這些文件的跟蹤。
3 回答

阿晨1998
TA貢獻2037條經驗 獲得超6個贊
這是蠻力的方法。它還會刪除存儲庫的配置。
注意:如果存儲庫有子模塊,這不起作用!如果您正在使用子模塊,則應使用例如交互式rebase
第1步:刪除所有歷史記錄(確保您有備份,這不能還原)
cat .git/config # note <github-uri>rm -rf .git
第2步:僅使用當前內容重建Git倉庫
git initgit add .git commit -m "Initial commit"
第3步:推送到GitHub。
git remote add origin <github-uri>git push -u --force origin master

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
唯一適合我的解決方案(并保持子模塊正常工作)是
git checkout --orphan newBranch
git add -A # Add all files and commit them
git commit
git branch -D master # Deletes the master branch
git branch -m master # Rename the current branch to master
git push -f origin master # Force push master branch to github
git gc --aggressive --prune=all # remove the old files
.git/當我有子模塊時,刪除總是會導致很大的問題。使用git rebase --root會以某種方式導致我的沖突(并且需要很長時間,因為我有很多歷史)。
- 3 回答
- 0 關注
- 644 瀏覽
添加回答
舉報
0/150
提交
取消