From 5782179bfebc4ff43e69b585fc015e99ce73e3cb Mon Sep 17 00:00:00 2001 From: wxy Date: Tue, 29 Aug 2017 08:16:05 +0800 Subject: [PATCH] PRF&PUB:20170818 How to recover from a git mistake.md @firmianay --- ...70818 How to recover from a git mistake.md | 66 +++++-------------- 1 file changed, 18 insertions(+), 48 deletions(-) rename {translated/tech => published}/20170818 How to recover from a git mistake.md (59%) diff --git a/translated/tech/20170818 How to recover from a git mistake.md b/published/20170818 How to recover from a git mistake.md similarity index 59% rename from translated/tech/20170818 How to recover from a git mistake.md rename to published/20170818 How to recover from a git mistake.md index cdc537e634..3ee48163c8 100644 --- a/translated/tech/20170818 How to recover from a git mistake.md +++ b/published/20170818 How to recover from a git mistake.md @@ -1,64 +1,43 @@ -如何从 git 错误中恢复 +如何恢复丢弃的 git stash 数据 ============================================================ -### 不要让 git 命令中的错误抹去数天的工作 +> 不要让 git 命令中的错误抹去数天的工作 ![How to recover from a git mistake](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bubblehands_fromRHT_520_0612LL.png?itok=_iQ2dO3S "How to recover from a git mistake") -Image by : opensource.com -今天我的同事几乎失去了他在四天工作中所做的一切。由于不正确的 **git** 命令,他把保存在 [stash][20] 中的更改删除了。在这悲伤的情节之后,我们试图寻找一种恢复他所做工作的方法,而且我们做到了! +今天我的同事几乎失去了他在四天工作中所做的一切。由于不正确的 `git` 命令,他把保存在 [stash][20] 中的更改删除了。在这悲伤的情节之后,我们试图寻找一种恢复他所做工作的方法,而且我们做到了! -首先警告一下:当你在实现一个大功能时,请将它分成小块并定期提交。在不做任何改动的情况下长时间工作并不是一个好主意。 +首先警告一下:当你在实现一个大功能时,请将它分成小块并定期提交。长时间工作而不做提交并不是一个好主意。 现在我们已经搞定了那个错误,下面就演示一下怎样从 stash 中恢复误删的更改。 我用作示例的仓库中,只有一个源文件 “main.c”,如下所示: -### [missing_data_from_stash_01.jpeg][9] - ![Repository with one source file](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_01.jpeg "Repository with one source file") -José Guilherme Vanz, [CC BY][1] - 它只有一次提交,即 “Initial commit”: -### [missing_data_from_stash_02.jpeg][10] - ![One commit](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_02.jpeg "One commit") -José Guilherme Vanz, [CC BY][2] - 该文件的第一个版本是: -### [missing_data_from_stash_03.jpeg][11] - ![First version of the file](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_03.jpeg "First version of the file") -José Guilherme Vanz, [CC BY][3] - 我将在文件中写一些代码。对于这个例子,我并不需要做什么大的改动,只需要有什么东西放进 stash 中即可,所以我们仅仅增加一行。“git diff” 的输出如下: -### [missing_data_from_stash_04.jpeg][12] - ![git-diff output ](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_04.jpeg "git-diff output ") -José Guilherme Vanz, [CC BY][4] - 现在,假设我想从远程仓库中拉取一些新的更改,当时还不打算提交我自己的更改。于是,我决定先 stash 它,等拉取远程仓库中的更改后,再把我的更改恢复应用到主分支上。我执行下面的命令将我的更改移动到 stash 中: ``` git stash ``` -使用命令 **git stash list** 查看 stash,在这里能看到我的更改: - -### [missing_data_from_stash_06.jpeg][13] +使用命令 `git stash list` 查看 stash,在这里能看到我的更改: ![Output of changes in our stash](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_06.jpeg "Output of changes in our stash") -José Guilherme Vanz, [CC BY][5] - -我的代码已经在一个安全的地方而且主分支目前是干净的(使用命令 **git status** 检查)。现在我只需要拉取远程仓库的更改,然后把我的更改恢复应用到主分支上,而且我也应该是这么做的。 +我的代码已经在一个安全的地方,而且主分支目前是干净的(使用命令 `git status` 检查)。现在我只需要拉取远程仓库的更改,然后把我的更改恢复应用到主分支上,而且我也应该是这么做的。 但是我错误地执行了命令: @@ -66,47 +45,38 @@ José Guilherme Vanz, [CC BY][5] git stash drop ``` -它删除了 stash,而不是像下面的命令: +它删除了 stash,而不是执行了下面的命令: ``` git stash pop ``` -这条命令在删除 stash 之前会从栈中恢复应用它。如果我再次执行命令 **git stash list**,就能看到在没有从栈中将更改恢复到主分支的之前,我就删除了它。OMG!接下来怎么办? +这条命令会在从栈中删除 stash 之前应用它。如果我再次执行命令 `git stash list`,就能看到在没有从栈中将更改恢复到主分支的之前,我就删除了它。OMG!接下来怎么办? -好消息是:**git** 并没有删除包含了我的更改的对象,它只是移除了对它的引用。为了证明这一点,我使用命令 **git fsck**,它会验证数据库中对象的连接和有效性。这是我对该仓库执行了 **git fsck** 之后的输出: - -### [missing_data_from_stash_07.jpeg][14] +好消息是:`git` 并没有删除包含了我的更改的对象,它只是移除了对它的引用。为了证明这一点,我使用命令 `git fsck`,它会验证数据库中对象的连接和有效性。这是我对该仓库执行了 `git fsck` 之后的输出: ![Output after executing the git-fsck command on the repository](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_07.jpeg "Output after executing the git-fsck command on the repository") -José Guilherme Vanz, [CC BY][6] - -由于使用了参数 **--unreachable**,我让 **git-fsck** 显示出所有不可访问的对象。正如你看到的,它显示并没有不可访问的对象。而当我从 stash 中删除了我的更改之后,再次执行相同的指令,得到了一个不一样的输出: - -### [missing_data_from_stash_08.jpeg][15] +由于使用了参数 `--unreachable`,我让 `git-fsck` 显示出所有不可访问的对象。正如你看到的,它显示并没有不可访问的对象。而当我从 stash 中删除了我的更改之后,再次执行相同的指令,得到了一个不一样的输出: ![Output after dropping changes on stash](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_08.jpeg "Output after dropping changes on stash") -José Guilherme Vanz, [CC BY][7] - -现在有三个不可访问对象。那么哪一个才是我的更改呢?实际上,我不知道。我需要通过执行命令 **git show** 来搜索每一个对象。 - -### [missing_data_from_stash_09.jpeg][16] +现在有三个不可访问对象。那么哪一个才是我的更改呢?实际上,我不知道。我需要通过执行命令 `git show` 来搜索每一个对象。 ![Output after executing the git-show command ](https://opensource.com/sites/default/files/u128651/missing_data_from_stash_09.jpeg "Output after executing the git-show command ") -José Guilherme Vanz, [CC BY][8] - -就是它!ID 号 **95ccbd927ad4cd413ee2a28014c81454f4ede82c** 对应了我的更改。现在我已经找到了丢失的更改,我可以恢复它。其中一种方法是将此 ID 取出来放进一个新的分支,或者直接提交它。如果你得到了你的更改对象的 ID 号,就可以决定以最好的方式,将更改再次恢复应用到主分支上。对于这个例子,我使用 **git stash** 将更改恢复到我的主分支上。 +就是它!ID 号 `95ccbd927ad4cd413ee2a28014c81454f4ede82c` 对应了我的更改。现在我已经找到了丢失的更改,我可以恢复它。其中一种方法是将此 ID 取出来放进一个新的分支,或者直接提交它。如果你得到了你的更改对象的 ID 号,就可以决定以最好的方式,将更改再次恢复应用到主分支上。对于这个例子,我使用 `git stash` 将更改恢复到我的主分支上。 ``` git stash apply 95ccbd927ad4cd413ee2a28014c81454f4ede82c ``` -另外需要重点记住的是 **git** 会周期性地执行它的垃圾回收程序。**gc** 执行之后,使用 **git fsck** 就不能再看到不可访问对象了。 +另外需要重点记住的是 `git` 会周期性地执行它的垃圾回收程序(`gc`),它执行之后,使用 `git fsck` 就不能再看到不可访问对象了。 - _This article was [originally published][18] on the author's blog and is reprinted with permission. _ + _本文[最初发表][18]于作者的博客,并得到了转载授权。_ + + +(题图:opensource.com,附图:José Guilherme Vanz, [CC BY][1]) -------------------------------------------------------------------------------- @@ -114,7 +84,7 @@ via: https://opensource.com/article/17/8/recover-dropped-data-stash 作者:[Jose Guilherme Vanz][a] 译者:[firmianay](https://github.com/firmianay) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出