如何回滚到 Git 中的先前版本?
How do I rollback to the previous version in Git?
我想看看我是否可以在文件中提交更改,但在这样做之后,存储库的所有文件都消失了。如何恢复之前的版本?
这是我所做的(注意这是我第一次连接到这个 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 的变化,所以我认为我可以开始了:
$ 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
只有一个文件是return:
$ git ls-tree -r master --name-only
entities.py
如你所见,我们已经失去了一切。我以前用过 TFS,任何错误都可以在 1 分钟内轻松回滚,在这里我花了几个小时试图在 Git 书中寻找正确的命令,但找不到如何恢复以前的命令版本?是否至少可以在 Git 中回滚?
解决此问题的尝试失败
- 我试过这个答案 How do I undo the most recent local commits in Git? 和 运行
git reset HEAD~
但我有以下错误
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 - rollback to previous commitreturn另一个错误信息:
$ 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'
- 这个答案没有准确说明如何恢复丢失的文件:How do I undo 'git add' before commit?
还好你知道遥控器上原主的简sha。初始 git push -f
的输出显示它在 4a0d8a5
。如果你有 shell 远程访问权限,去那里创建一个分支(或一个标签。你只需要使提交可达)在那个提交上:
git branch original-master 4a0d8a5 # Run on origin
现在,在本地仓库中:
git fetch origin
git reset --hard 4a0d8a5
我想看看我是否可以在文件中提交更改,但在这样做之后,存储库的所有文件都消失了。如何恢复之前的版本?
这是我所做的(注意这是我第一次连接到这个 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 的变化,所以我认为我可以开始了:
$ 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
只有一个文件是return:
$ git ls-tree -r master --name-only
entities.py
如你所见,我们已经失去了一切。我以前用过 TFS,任何错误都可以在 1 分钟内轻松回滚,在这里我花了几个小时试图在 Git 书中寻找正确的命令,但找不到如何恢复以前的命令版本?是否至少可以在 Git 中回滚?
解决此问题的尝试失败
- 我试过这个答案 How do I undo the most recent local commits in Git? 和 运行
git reset HEAD~
但我有以下错误
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 - rollback to previous commitreturn另一个错误信息:
$ 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'
- 这个答案没有准确说明如何恢复丢失的文件:How do I undo 'git add' before commit?
还好你知道遥控器上原主的简sha。初始 git push -f
的输出显示它在 4a0d8a5
。如果你有 shell 远程访问权限,去那里创建一个分支(或一个标签。你只需要使提交可达)在那个提交上:
git branch original-master 4a0d8a5 # Run on origin
现在,在本地仓库中:
git fetch origin
git reset --hard 4a0d8a5