簡體   English   中英

如何在 Git 中回滾到之前的版本?

[英]How do I rollback to the previous version in Git?

我想看看我是否可以在文件中提交更改,但是這樣做之后,repo 的所有文件都消失了。 如何恢復以前的版本?

這是我所做的(注意這是我第一次連接到這個 Git 服務器)。 首先我初始化我的存儲庫:

 $ rm -rf .git/
 $ git init
 $ git remote add origin https://remote.url/myProject.git
 $ git commit -m "My first commit"
 On branch master
 Initial commit
 Untracked files:
   (use "git add <file>..." to include in what will be committed)
        .foundry/
        .gitignore
        .teamcity/
        GitVersion.yml
        README.md
        config.ini
        block.py
        view.py
        entities.py
 nothing added to commit but untracked files present (use "git add" to track)

然后因為我只修改了entities.py ,所以我將它添加到提交列表中:

$ git add entities.py
$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   entities.py
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .foundry/
        .gitignore
        .teamcity/
        GitVersion.yml
        README.md
        config.ini
        block.py
        view.py

Git 檢測到entities.py的變化所以我認為我對go 很好:

$ git commit -m "entities.py first commit"
[master (root-commit) 1dc09d9] entities.py first commit
1 file changed, 535 insertions(+)
create mode 100644 entities.py
$ git push -f origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 6.64 KiB | 1.66 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://remote.url/myProject.git
 + 4a0d8a5...1dc09d9 master -> master (forced update)

當我想在服務器上檢查提交是否有效時,項目的所有文件都消失了:只有一個文件: entities.py 當我運行git ls-tree時,只返回一個文件:

$ git ls-tree -r master --name-only
entities.py

正如你所看到的,我們已經失去了一切。 我以前使用TFS,任何錯誤都可以在1分鍾內輕松回滾,在這里我花了幾個小時試圖在Git書中尋找正確的命令,但找不到如何恢復以前的版本? 是否至少可以在 Git 中回滾?

解決此問題的嘗試失敗

fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ git push origin HEAD~1:master
error: src refspec HEAD~1 does not match any
error: failed to push some refs to 'https://remote.url/myProject.git'

幸好你知道遙控器上原主的簡寫。 初始git push -f的 output 向您顯示它位於4a0d8a5 如果您有 shell 訪問遠程 go 並在該提交處創建一個分支(或標簽。您只需要使提交可訪問):

git branch original-master 4a0d8a5    # Run on origin

現在,在本地倉庫中:

git fetch origin
git reset --hard 4a0d8a5

暫無
暫無

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

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