preface

Git is a distributed version control system that is easy to learn but difficult to master. If you are not familiar with Git, you are advised to read Liao Xuefeng’s Git tutorial before reading this article, because this article focuses on using techniques and won’t explain too much about basic nouns.

Git tutorial by Liao Xuefeng

www.liaoxuefeng.com/wiki/896043…

Restore the code that was forced to push push missing

Git push -f is a command that overwrites remote branches for various reasons (such as git rebase).

Do not panic. This does not mean that your partner’s commit will never be found back. In most cases, they can still be retrieved.

Git reflog can view all operations on all branches (including commit and reset operations), including deleted commit records. Git reflog cannot view deleted commit records

Reflog can be a lifesaver, but Git does a regular gc to clean up the reflog, so don’t wait too long to recover after you’ve been overwritten, or you may never find it again.

Operation method

  1. Back up data for the current workspace

You can use git stash and other commands to back up the code you are writing

  1. Enter on the command linegit reflog/git log -g

Git log displays all historical commits to find the commits you need (including deleted commits)

2.1 Forcibly Rollback to the commit that was deleted

git reset --hard <SHA1>
Copy the code

2.2 or directly

git cherry-pick <SHA1>
Copy the code

Just take the original version of the work back. But if there are conflicts, there are conflicts to deal with.

  1. Push the remote branch
git push -f origin <branch>
Copy the code

If the cause of the commit loss is not recorded in reflog, such as running rm -rf. git/logs/, because the reflog data is stored in.git/logs/, there will be no reflog.

You can use the Git FSCK tool, which checks the data integrity of your repository. If the –full option is specified, this command displays all objects that are not referenced (pointed to) by other objects:

It can then be restored in the same way, by creating a branch to that SHA.

Don’t panic about pushing git push-f anymore?

Pay attention to my

I’m a back-end development engineer.

Focus on back-end development, data security, crawler, Internet of Things, edge computing and other directions, welcome to exchange.

I can be found on every platform

  • Wechat official account: A ramble on back-end technology
  • Making: @ qqxx6661
  • CSDN: @ Rude3Knife
  • Zhihu: @ Zhendong
  • Jane: @pretty three knives a knife
  • Nuggets: @ pretty three knife knife
  • Headline: @Back-end Technology Ramblings

Original blog main content

  • Java knowledge points review the full manual
  • Leetcode algorithm parsing
  • Sword point offer algorithm analysis
  • SpringBoot rookie combat series
  • SpringCloud rookie combat series
  • Crawler related technical articles
  • Technical articles related to back-end development
  • Anecdotes/good books to share/personal interests

Personal public account: Back-end technology ramble

If the article is helpful to you, you might as well collect, coin, forward, in the look ~