簡體   English   中英

如何將 Git 存儲庫重置為第一次提交之前的狀態?

[英]How is it possible to reset a Git repository to the state before the first commit?

我知道我可以使用git reset --hard <commit>回到歷史上更早的提交。 但是,這只讓我回滾到第一次提交(而不是在第一次提交之前)。

我想這樣做,因為我創建了一個 Git 存儲庫並提交了很多錯誤的文件和目錄。 簡單地刪除存儲庫並重新開始在這里不是一種選擇,因為存儲庫是由管理員創建的。

如何將我的 Git 存儲庫重置為第一次提交之前的狀態?

作為修改第一次提交的替代方法,您還可以使用git checkout --orphan創建一個全新的分支,然后將master重置git checkout --orphan分支:

git checkout --orphan new_master
# add files
git commit -m "new initial commit"
git checkout master
git reset --hard new_master
git branch -D new_master
git checkout --orphan temp      # Start a new history with a dummy initial branch
# Prepare your first commit...
git commit                      # As usual, this will actually create the temp branch
git checkout -B master          # Bring master onto this new commit
git push --force origin master  # Update the remote, dropping the old history
git branch -d temp              # Delete the temporary branch

你可以做任何你想做的改變,並修改到最后一次提交:

# do changes
git commit --amend
# enter a new commit message if you wish

如果你想合並兩個提交,你可以使用git rebase -i --root它允許你處理第一個提交和下一個提交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM