What should I do if my Git stash is accidentally emptied? Can I go back on my word after mentioning the code?

1 Introduction

The knowledge summarized in this article is very practical. Although it is an uncommon operation of the git tool, it is definitely not cold knowledge. After learning, you can upgrade from being able to use git to being a git master.
There are mainly solutions for two scenarios: How to restore the git stash record after it is emptied? How to restore after git commit code?

2. git stash clears the scene

I often use the git stash command, because I am responsible for the packaging and upgrading of the project. Sometimes I need to upgrade the project version, but I have made some changes locally and I don’t want to upgrade to the remote end (there are not many changes and there is no need to pull a new branch). This At that time, git stash temporarily saves local modifications, which is really delicious. After packaging the package, you can restore the temporarily stored code to the local through git unstash, without restarting.
Then here comes the problem. I don’t operate directly through the command line. There is a client on the idea editor that can be operated. Every time I stash, I will leave a line of stash records with an alias to facilitate the recovery of the code later. Slowly, I will record here There are too many, I can’t see too much redundant data because of mild obsessive-compulsive disorder, I accidentally took you to clear it when I unstashed it, and my hands were too fast at the time. After clearing it, I found that the code I wrote in the morning was stashed and has not been recovered yet. , sigh!
insert image description here

2. How to restore after git stash clear

  1. First, type in the console
git fsck --lost-found

insert image description here

  1. Then according to the record seen by the above command: dangling commit xxxidxxx, enter the following command to view the specific content of the id
git show xxxxidxxxx

insert image description here
Here is the previously deleted git stash temporary information, including temporary storage date, alias (mine is 0309) and temporary storage file list information. If it is not what we want, we can only look at all dangling commits one by one For the record, I haven't found a shortcut here yet.
When you find the submitted data you want to restore, copy the id and enter the following command to restore

git merge xxxxidxxxx

After execution, the previous code is restored. I originally thought that the restoration here is equivalent to git unstash restoring the code to the local, but I was wrong.
After restoring, the effect is equivalent to committing, but it has not been pushed to the remote warehouse, but some local modifications were changed by me for fun and cannot be pushed, so the commit has to be restored.

3. Git revokes the information that has been pushed (push) to the remote warehouse

This scenario is also very common. What should I do if I submit the code by mistake occasionally? Three steps are required:

  1. 首先,通过git log查看提交信息, in order to obtain the version number that needs to be rolled back to (applicable to the scenario where the error code has been pushed to the remote end): the
    insert image description here
    version number that needs to be rolled back to here is: 4d70e000d79cb556528f99908d63e03796bd2c46.
  2. 然后,通过git reset --soft <版本号>重置至指定版本的提交, to achieve the purpose of withdrawing submission:
$ git reset --soft 4d70e000d79cb556528f99908d63e03796bd2c46

The parameter soft refers to: keep the current workspace for resubmission//If the last submission is incomplete, if you want to add something on the basis of the last time, you can also
choose the parameter hard, which will cancel the modification of the corresponding workspace, so be careful Use//If you don’t want to submit something last time, you can choose this
step. This step cancels the commit message (it has not been pushed to the remote warehouse)
2. Undo
For the situation that has been pushed to the remote warehouse, you need to pass git push origin master –force Forcibly submit the current version number to achieve the purpose of revoking the version number. The parameter force must be added to force the submission, otherwise the submission will fail, because the local project version number is lower than the remote warehouse version number at this time:

$ git push origin master
To github.com:hanchao5272/myreflect.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:hanchao5272/myreflect.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

3. The rollback is successful, you can modify the code locally, resubmit and push it

insert image description here

Guess you like

Origin blog.csdn.net/qq_42887496/article/details/129421784