DEV Community

Cover image for You messed up your git repository? Don't panic! Here is the first-aid kit 🤕🩹
Christophe Colombier
Christophe Colombier

Posted on

You messed up your git repository? Don't panic! Here is the first-aid kit 🤕🩹

Using git is a long journey. I'm still learning things even after a decade of using it daily at work.

You will need time to understand what you do. Unfortunately, the best way to learn is to fail. And you will fail a lot with git, believe my own experience.

It requires understanding the concepts you are using: branch, rebase, merge, push, cherry-pick, stash, squash, fixup, reflog …

You will need time to understand all these. The main issue is that the ones who make the most mistakes are the ones that are starting using git, and they are likely to do the worst mistakes. Yes, unfair.

I consider myself as an advanced git user. Some may say it's an imposter syndrome, because they think I'm almost an expert, but I consider we can always progress.

I would like to give you guys some references and tools.

Don't panic, and try to understand what you did

You need to figure what you made, before trying to fix it.

So take the time needed to understand.

Then you need to do a backup

As always, I would recommend you to perform backup before trying anything.

Copy your repository project, including the .git folder somewhere. Think about using mktemp -d.

Then try to solve your issue in this /tmp/ folder, not in your repository. This will avoid screwing everything even more. And you can restart trying fixing if you fail.

How to fix then ?

Here is good reference and resource: https://ohshitgit.com/

It will provide you guidance and what to do to recover.

There is also a tool that can help you. I found it recently.

GitHub logo Bhupesh-V / ugit

🚨️ ugit helps undo git commands. Your damage control git buddy. Undo from 20+ git scenarios.

ugit

ugit logo

Undo your last oopsie 🙈️ in git

build ugit Docker pulls total downloads (github + brew) platform support linux and macos git undo text guide Twitter: bhupeshimself

ugit demo: restore file gif

More Video Demos ✨️

Undo git add

undo-git-add.mp4

Undo git branch -D

undo-git-branch-delete.mp4

Undo git merge

undo-git-merge.mp4

Why use ugit?

  • You accidentally ran a git command you wish to 'undo'.
  • You want to save time by not searching for how to undo ...
  • Your focus is on the problems at hand and not on Git (avoid context switching)

What's in the box?

ugit/git-undo supports undoing the following operations, some are a WIP. If you know of any other operations that can be undone and are not in the list, make sure to raise an issue or make a quick PR 💛️

  • Undo git commit
  • Undo git add
  • Undo git push
  • Undo git branch -D (branch delete)
  • Undo git pull
  • Undo git reset
  • Undo git tag -d (tag delete)
  • Undo git stash apply
  • Undo git




Photo credits: https://www.tumblr.com/dictatortirah

Top comments (0)