Why restore
Yesterday, I forgot to git pull upstream when I was writing the code, so I saved the current modification with git stash save. After the code was pulled down, I used to input git stash pop to restore the code. Then, with the git stash clear command, the reverse operation clears all the temporary storage, as follows
[bigham@DESKTOP-MKMH2OT /d/Work_Space/vant-project]$ git stash save [bigham@DESKTOP-MKMH2OT /d/Work_Space/vant-project]$ gp [bigham@DESKTOP-MKMH2OT /d/Work_Space/vant-project]$ git stash clearCopy the code
Then I type git Stash pop and suddenly I’m lost. I don’t have any history.
The solution
You can use git FSCK –lost-found to display all stash records as follows
[bigham@DESKTOP-MKMH2OT /d/Work_Space/vant-project]$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (374/374), done. dangling blob f94019b06efd6579106397257e89ee42eb846cd9 dangling commit 673a01f0feda0cb1bfffd80813637de1dd301d2c dangling blob 720225cba2c9b56bac0a2f89d9bc7e2e58735e18 dangling blob 21438a2b9613e9d1b0c1ca8fc336b44770c12964 dangling blob 0125d98ea333928261d89f948930aa56c600e1a8 dangling blob 84ad1c2287429d8b19450c885c7e397d27217db4 dangling blob a8e511fa943f6840f7b3ad8876c120a071d309a5 dangling commit d4967c308e83189edd0b61661f0cb82e47b7f882 dangling blob 69571e30b84e8639d620723ec917bb2a9bc53da5 dangling commit d4967c308e83189edd0b61661f0cb82e47b7f882 ...... And 200 more.Copy the code
Git show id to ignore the error type. Only find dangling a commit record, such as the git show d4967c308e83189edd0b61661f0cb82e47b7f882, displays the change point record, find accord with id, Using git Merge ID merges the previous Stash contents
[bigham@DESKTOP-MKMH2OT /d/Work_Space/]$ git show d4967c308e83 commit d4967c308e83189edd0b61661f0cb82e47b7f882 Merge: bb2f1da 337d569 Author: bighamD <******[email protected]> Date: Sun Sep 19 16:26:29 2021 +0800 WIP on main: Vuex diff -- cc.postcsRC. Js index 81f1884,81f1884.. vuex diff -- cc.postcsRC. 3187c01 --- a/.postcssrc.js +++ b/.postcssrc.js ....Copy the code
After trying git show for several times, I found that it is not sorted by time at all. It just feels random. It is impossible to find these 200 records one by one. So I wrote a simple Nodejs script to find the correct Stash ID
revert-stash.js
const child_process = require('child_process');
/ * * * *@returns [ "673a01f0feda0cb1bfffd80813637de1dd301d2c", "d4967c308e83189edd0b61661f0cb82e47b7f882"]
*/
function qryStashIds() {
return new Promise( (resolve, reject) = > {
child_process.exec('git fsck --lost-found', {encoding: 'utf8'}, (err, stdout) = > {
if (err) {
return reject(err);
}
// Select the id of the dangling commit
let matches = stdout.match(/commit\s+([^\s\r\n]+)/g);
if (matches) {
matches = matches.map(m= > m.replace(/commit\s+/.' ')); resolve(matches); }})})}function taskGenertor(date, id) {
return new Promise((resolve) = > {
child_process.exec('git show ' + id, {encoding: 'utf8'}, (err, log) = > {
if (err) {
return resolve();
}
// After observing that each log has the following date identifier, we just need to filter the date according to the passed date
// Author: bighamD <******[email protected]>
// Date: Sun Sep 19 16:26:29 2021 +0800
const match = log.match(/Date:\s+([^+\r\n]+)/);
if (match) {
const stashDate = match[1].trim();
// Indicate that this record is consistent
if (new Date(stashDate) > date) { resolve(id) } } resolve(); })})}async function matchRevertStashId(date) {
const stashIds = await qryStashIds();
return (await Promise.all(stashIds.map(id= > taskGenertor(date, id)))).filter(Boolean);
}
// Let's say I'm 2021-9-19 and stash Save is implemented
let match = await matchRevertStashId(new Date('2021-9-19'));
console.log(match);
// ["673a01f0feda0cb1bfffd80813637de1dd301d2c"]
Copy the code
After the terminal node revert – stash. Js, selected from hundreds of just one, at this point the git merge 673 a01f0feda0cb1bfffd80813637de1dd301d2c, hey hey, the code is back.
According to the website, idea editor has its own stash recovery operation, but I haven’t found vscode. Maybe it has this function or the plug-in has this function. Git-len and git-history have not been found
Reference:
- Git Stash clear recovery code