name_of_user
name_of_user

Reputation: 35

How to recover local files removed by git -rm and git commit?

I am new to Git and not sure how to recover deleted files. I made some changes to python code on my local machine and then add them, commit and then have delected.

So.. Now I have this one:

Your branch is behind 'origin/start' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    base.py
        deleted:    model.py

Is it possible to cancel -rm and recover files base.py, model.py?

Upvotes: 0

Views: 1546

Answers (3)

Edward
Edward

Reputation: 55

Ok if you have a previous commit that had your files type in git log.

This will give you your commit hash. It should be a bunch of characters.

You should do git checkout . Make it a new branch or make it a feature branch. This will persist your changes on a new branch.

DO NOT hard reset or force push to GIT. if you are working with a team.

Work through it.

Here are some docs on it.

Want to change my master to an older commit, how can I do this?

Alternatively you can do some sort of feature branch.

Upvotes: 0

huytmb
huytmb

Reputation: 4274

You need to first unstage it with git reset -- <file> and then, you can then recover it with git checkout -- <file>.

  1. Restores the file status in the index

    git reset -- <file>

  2. Check out a copy from the index

    git checkout -- <file>

Upvotes: 2

user13239021
user13239021

Reputation: 81

If you just want to go back, you can try a hard reset to a previous commit, it removes everything you did after the selected commit.

It's not recommended this if you are working with others, as other may have your commits that you are binning. In your case however it sounds like you aren't.

Upvotes: 0

Related Questions