mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-04 22:00:34 +08:00
Merge pull request #5990 from firmianay/master
[translating] 20170824 Half a dozen clever Linux command line tricks.md
This commit is contained in:
commit
4ac1255ccc
@ -1,3 +1,5 @@
|
||||
Translating by firmianay
|
||||
|
||||
Half a dozen clever Linux command line tricks
|
||||
============================================================
|
||||
|
||||
|
@ -1,20 +1,18 @@
|
||||
Translating by firmianay
|
||||
|
||||
How to recover from a git mistake
|
||||
如何从 git 错误中恢复
|
||||
============================================================
|
||||
|
||||
### Don't let an error in a git command wipe out days of work.
|
||||
### 不要让 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
|
||||
|
||||
Today my colleague almost lost everything that he did over four days of work. Because of an incorrect **git **command, he dropped the changes he'd saved on [stash][20]. After this sad episode, we looked for a way to try to recover his work... and we did it!
|
||||
今天我的同事几乎失去了他在四天工作中所做的一切。由于不正确的 **git** 命令,他把保存在 [stash][20] 中的更改删除了。在这悲伤的情节之后,我们试图寻找一种恢复他所做工作的方法,而且我们做到了!
|
||||
|
||||
First a warning: When you are implementing a big feature, split it in small pieces and commit it regularly. It's not a good idea to work for a long time without committing your changes.
|
||||
首先警告一下:当你在实现一个大功能时,请将它分成小块并定期提交。在不做任何改动的情况下长时间工作并不是一个好主意。
|
||||
|
||||
Now that we've gotten that out of the way, let's demonstrate how to recover changes accidentally dropped from stash.
|
||||
现在我们已经搞定了那个错误,下面就演示一下怎样从 stash 中恢复误删的更改。
|
||||
|
||||
My example repository, which has only one source file, **main.c**, looks like this:
|
||||
我用作示例的仓库中,只有一个源文件 “main.c”,如下所示:
|
||||
|
||||
### [missing_data_from_stash_01.jpeg][9]
|
||||
|
||||
@ -22,7 +20,7 @@ My example repository, which has only one source file, **main.c**, looks like t
|
||||
|
||||
José Guilherme Vanz, [CC BY][1]
|
||||
|
||||
It has only one commit, the initial commit:
|
||||
它只有一次提交,即 “Initial commit”:
|
||||
|
||||
### [missing_data_from_stash_02.jpeg][10]
|
||||
|
||||
@ -30,7 +28,7 @@ It has only one commit, the initial commit:
|
||||
|
||||
José Guilherme Vanz, [CC BY][2]
|
||||
|
||||
The first version of our file is:
|
||||
该文件的第一个版本是:
|
||||
|
||||
### [missing_data_from_stash_03.jpeg][11]
|
||||
|
||||
@ -38,7 +36,7 @@ The first version of our file is:
|
||||
|
||||
José Guilherme Vanz, [CC BY][3]
|
||||
|
||||
I'll start to code something. For this example, I do not need to make a big change, just something to put in the stash, so I will just add a new line. The **git-diff** output should be:
|
||||
我将在文件中写一些代码。对于这个例子,我并不需要做什么大的改动,只需要有什么东西放进 stash 中即可,所以我们仅仅增加一行。“git diff” 的输出如下:
|
||||
|
||||
### [missing_data_from_stash_04.jpeg][12]
|
||||
|
||||
@ -46,13 +44,13 @@ I'll start to code something. For this example, I do not need to make a big chan
|
||||
|
||||
José Guilherme Vanz, [CC BY][4]
|
||||
|
||||
Now, suppose that I want to pull some new changes from a remote repository, but I'm not ready to commit my change. Instead, I decide to stash it, pull the remote repository's changes, then apply my change back to the master. I execute the following command to move my change to stash:
|
||||
现在,假设我想从远程仓库中拉取一些新的更改,当时还不打算提交我自己的更改。于是,我决定先 stash 它,等拉取远程仓库中的更改后,再把我的更改恢复应用到主分支上。我执行下面的命令将我的更改移动到 stash 中:
|
||||
|
||||
```
|
||||
git stash
|
||||
```
|
||||
|
||||
Looking into the stash with **git stash list**, I can see my change there:
|
||||
使用命令 **git stash list** 查看 stash,在这里能看到我的更改:
|
||||
|
||||
### [missing_data_from_stash_06.jpeg][13]
|
||||
|
||||
@ -60,23 +58,23 @@ Looking into the stash with **git stash list**, I can see my change there:
|
||||
|
||||
José Guilherme Vanz, [CC BY][5]
|
||||
|
||||
My code is in a safe place and the master branch is clean (I can check this with **git status**). Now I just need to pull the remote repository changes, then apply my change on the master, and I should be set.
|
||||
我的代码已经在一个安全的地方而且主分支目前是干净的(使用命令 **git status** 检查)。现在我只需要拉取远程仓库的更改,然后把我的更改恢复应用到主分支上,而且我也应该是这么做的。
|
||||
|
||||
But I accidentally execute:
|
||||
但是我错误地执行了命令:
|
||||
|
||||
```
|
||||
git stash drop
|
||||
```
|
||||
|
||||
which deletes the stash, instead of:
|
||||
它删除了 stash,而不是像下面的命令:
|
||||
|
||||
```
|
||||
git stash pop
|
||||
```
|
||||
|
||||
which would have applied the stash before dropping it from my stack. If I execute **git stash list** again, I can see I dropped my change from the stash without applying it on the master branch. OMG! Who can help me?
|
||||
这条命令在删除 stash 之前会从栈中恢复应用它。如果我再次执行命令 **git stash list**,就能看到在没有从栈中将更改恢复到主分支的之前,我就删除了它。OMG!接下来怎么办?
|
||||
|
||||
Good news: **git** did not delete the object that contains my change; it just removed the reference to it. To prove this, I use the **git-fsck** command, which verifies the connectivity and validity of the objects in the database. Here's the output after I executed the **git-fsck** command on the repository:
|
||||
好消息是:**git** 并没有删除包含了我的更改的对象,它只是移除了对它的引用。为了证明这一点,我使用命令 **git fsck**,它会验证数据库中对象的连接和有效性。这是我对该仓库执行了 **git fsck** 之后的输出:
|
||||
|
||||
### [missing_data_from_stash_07.jpeg][14]
|
||||
|
||||
@ -84,7 +82,7 @@ Good news: **git** did not delete the object that contains my change; it just
|
||||
|
||||
José Guilherme Vanz, [CC BY][6]
|
||||
|
||||
With the **--unreachable **argument, I asked **git-fsck** to show me the objects that are unreachable. As you can see, it showed no unreachable objects. After I dropped the changes on my stash, I executed the same command, and received a different output:
|
||||
由于使用了参数 **--unreachable**,我让 **git-fsck** 显示出所有不可访问的对象。正如你看到的,它显示并没有不可访问的对象。而当我从 stash 中删除了我的更改之后,再次执行相同的指令,得到了一个不一样的输出:
|
||||
|
||||
### [missing_data_from_stash_08.jpeg][15]
|
||||
|
||||
@ -92,7 +90,7 @@ With the **--unreachable **argument, I asked **git-fsck** to show me the obj
|
||||
|
||||
José Guilherme Vanz, [CC BY][7]
|
||||
|
||||
Now there are three unreachable objects. But which one is my change? Actually, I don't know. I have to search for it by executing the **git-show** command to see each object.
|
||||
现在有三个不可访问对象。那么哪一个才是我的更改呢?实际上,我不知道。我需要通过执行命令 **git show** 来搜索每一个对象。
|
||||
|
||||
### [missing_data_from_stash_09.jpeg][16]
|
||||
|
||||
@ -100,13 +98,13 @@ Now there are three unreachable objects. But which one is my change? Actually, I
|
||||
|
||||
José Guilherme Vanz, [CC BY][8]
|
||||
|
||||
There it is! The ID **95ccbd927ad4cd413ee2a28014c81454f4ede82c** corresponds to my change. Now that I found the missing change, I can recover it! One solution is check out the ID into a new branch or apply the commit directly. If you have the ID of the object with your changes, you can decide the best way to put changes on the master branch again. For this example, I will use **git-stash** to apply the commit on my master branch again.
|
||||
就是它!ID 号 **95ccbd927ad4cd413ee2a28014c81454f4ede82c** 对应了我的更改。现在我已经找到了丢失的更改,我可以恢复它。其中一种方法是将此 ID 取出来放进一个新的分支,或者直接提交它。如果你得到了你的更改对象的 ID 号,就可以决定以最好的方式,将更改再次恢复应用到主分支上。对于这个例子,我使用 **git stash** 将更改恢复到我的主分支上。
|
||||
|
||||
```
|
||||
git stash apply 95ccbd927ad4cd413ee2a28014c81454f4ede82c
|
||||
```
|
||||
|
||||
Another important thing to remember is **git** runs its garbage collector periodically. After a **gc** execution, you can no longer see the unreachable objects using **git-fsck**.
|
||||
另外需要重点记住的是 **git** 会周期性地执行它的垃圾回收程序。**gc** 执行之后,使用 **git fsck** 就不能再看到不可访问对象了。
|
||||
|
||||
_This article was [originally published][18] on the author's blog and is reprinted with permission. _
|
||||
|
||||
@ -115,7 +113,7 @@ Another important thing to remember is **git** runs its garbage collector peri
|
||||
via: https://opensource.com/article/17/8/recover-dropped-data-stash
|
||||
|
||||
作者:[Jose Guilherme Vanz][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[firmianay](https://github.com/firmianay)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user