Recover a file in GIT

Asked

Viewed 11,166 times

5

I have two folders (test-git1 and test-git2), in both I gave a clone of the server repository.

If I accidentally delete a file inside the test-git1 folder, how do I recover it? I tried to give git pull origin master, but this message appears:

$ git pull origin master
From github.com:asdasdadasasd/teste-git
 * branch            master     -> FETCH_HEAD
Already up-to-date.

In my test-git1 folder, I delete the file joao.txt, then I made a

git status
`deleted:    joao.txt`

Until then ok, but how do I recover this data again? Do I have to give a clone again? There is a way to recover again by pull?

  • forehead $ git reset --hard origin/master, and notice that this will erase local changes you have made.

  • You didn’t try to give a merge? When you give a pull you’re just bringing content from one repository to another but you’re not putting it in your workbook. But there are other ways to do what you want.

  • now it’s weird, I deleted the test-git1 folder and cloned the server files again to a new folder with the same name (test-git1), now I went to try to add new files in this newly cloned folder and went to check in git status, no change/modification appeared, etc..

  • very carefully with "reset --hard" git will remove all changes until it is in origin/master. Whenever you use this command be sure what you are doing. By the way, you are very sure!

  • @Flavius reset only forces the local folder to have the same as the remote folder. I agree that it should be used with caution but does not change anything from origin/master

  • "reset --hard" removes any and all files and changes that git knows and no possibility of recovery, it is not only force the local folder to have the same as the remote folder... I don’t know the level of knowledge of @João but it is always warn right... :-)

  • my level of knowledge is: learning git since yesterday. what essential commands does a group of developers in a company use? git status, git pull, git checkout, git push, git add, git commit, and git log? have more tips?

  • @John, even with his description, is easier if you give us the sequence of commands in each cloned repository, accompanied by the output on git status. It makes it easier to explain and help :).

Show 3 more comments

4 answers

3

Since you cloned the repository and soon after that deleted the file (the status deleted that you said), to get it back just rotate:

git checkout <nome do arquivo excluído>

This command discards all modifications made to the file passed as parameter since the last commit (version record). But the command checkout is more powerful than this, is accepted several other options: git-checkout Docs

1

You can use these commands:

git rev-list -n 1 HEAD -- <file_path>

Where -n X is the amount of previous commits that the file exists.

git checkout <deleting_commit>^ -- <file_path>

this command is to pull the file again.

Here has the post with the same problem.

0

To restore the file just run the following command:

git restore joao.txt
  • 1

    Edit the response and put the syntax of the command, a description of what you do and the link to documentation as they are signaling to remove your response.

0

I answered that here: My answer is that cases where you just used :

git checkout -- .

before committing something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.