0

Have deleted the branch from Pycharm . I got a suboption as delete and accidentally I clicked it. The branch got deleted from the origin .Is there a way to recover the branch other than using git?

Nawaa
  • 87
  • 2
  • 10
  • 1
    Possible duplicate of [Can I recover a branch after its deletion in Git?](https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git) – phd Dec 19 '18 at 10:15
  • https://stackoverflow.com/search?q=%5Bgit%5D+recover+deleted+branch – phd Dec 19 '18 at 10:15

4 Answers4

6

In PyCharm you can find the hash of deleted branch in logs (Help > Show Log in Files).

Then run

git checkout -b <branch_name> <found_hash>
Jan M.
  • 382
  • 6
  • 3
4

Don't run: git gc

Run below commands in terminal at root of project.

Find all dangling commits:

git fsck --no-reflog
Checking object directories: 100% (256/256), done.
Checking objects: 100% (8459/8459), done.
dangling commit 2e04e4159219dbd35f55a53fb0c6ae9c187f6b8e
dangling commit 9db660c967e3b410b354c0024090a5d0bfabb614
dangling commit dc6f48a17b749ad6a76ec1fe9434b8427487dbb6

Checkout to commit to see if it was your last commit.

git checkout 2e04e4159219dbd35f55a53fb0c6ae9c187f6b8e
git log

If found commit is right commit, then checkout your branch from it

git checkout -b <YOUR BRANCH>

Your branch is recovered.

Thanks

Paritosh Singh
  • 6,288
  • 5
  • 37
  • 56
0

To restore a local branch that was deleted:

In PyCharm 2019.3.4, you can:

  1. create a new branch
  2. show the version control tool window (View -> Tool Windows -> Version Control)
  3. click on the Log tab
  4. search for branch by name (or scroll back through history - what ever works)
  5. right-click on branch, select "Reset Current Branch to Here..."
  6. select appropriate reset mode: soft, mixed, hard, keep and click Reset
  7. reconcile changes with base branch

Tips:

  • Always work in a disposable branch until you are happy with results.
  • If in a disposable branch, you can easily try the different reset modes to find the one that best suits your situation.

Notes:

  • I like the PyCharm Version Control Log viewer because it pulls together all the metadata in a historical tree view. I can locate the branch through name, tag, commit comments, browsing the changes that were made.
Steve Tarver
  • 3,030
  • 2
  • 25
  • 33
0

there is a notification button on the top-right of the Pycharm or any jetbrains IDEs if you press it and you see your recent activities like deleting a branch now click on restore to recover your deleted branch enter image description here

enter image description here