I am working on local branch name is local1 .I wrongly deleted one file which exists in master from my local branch and then pushed update to my local branch. While raising PR it is showing file is deleted . To resolve this issue i tried with git fetch /merge and git pull origin master but nothing is working. Now i am not able to synch my local branch to master .It is not updating.
1 Answers
Well, you told Git to delete the file and Git obliged. The commits in master
/main
didn't change anything with the file, so Git considers your version to be "newer". I recommend reading the Git Book for some additional background.
Now, back to your question. How can you restore the file? It depends a little on how you deleted it and if you are okay with throwing away your commits and re-creating new commits which look similar.
You have a single commit which only deletes the file and changes nothing else.
In that case, simply revert the commit:
git revert $hash_of_your_commit
. This will create a second, new commit, which undos the changes of your original commit (basically inverts/reverses the patch/diff).It will also work if your commit contains additional changes. These changes will be "undone" (i.e. reverted) too. The final result is as if the changes never happened (think: x+1-1 == x); even though the original commit is still there.
You deleted the files and committed other changes at the same time.
Use
git checkout
to get a known version of the file, e.g. frommaster
or from your commit before you deleted it:git checkout master -- path/to/your/file
, then create a new commit:git commit -m 'Restore deleted file'
.Your history/branch is not shared and you are okay with rewriting it. Rewriting means throwing away all old commits and recreating new commits which look very similar, except for the file not being deleted.
Please be aware of the implications of this actions, before you go this route.
First, restore the file with regular means (e.g. option 1 or 2). Then use an interactive rebase and fuse the restoring commit into the original commit with
squash
orfixup
:git rebase -i $hash_before_file_was_deleted
.
References:

- 246,190
- 53
- 318
- 364
-
Thanks for your suggestion Coming to you question i wrongly deleted one file along with it i removed many variables declaration from one pojo java file and those variables exist in master branch...So how can i recover deleted file and those variables inshort how can i synch my local branch with master – sekhar Jan 12 '23 at 08:55
-
@sekhar like explained in my answer. But perhaps you only want to _revert_ your full commit? (or use `rebase` to drop it completely) – knittl Jan 12 '23 at 08:56
-
i want git command which will synch my local branch to master . Like when i do not delete any data or file in my local and do git pull origin master then my local gets updated and synchs to master properly .But when i deletes any file or data and then do git pull origin master then it does not work – sekhar Jan 12 '23 at 09:45
-
@sekhar have you read my answer? It explains how to "undo" your file removal. Or do you actually want to get rid of your branch completely and replace it with "master"? – knittl Jan 12 '23 at 09:47
-
i can undo the files with your above answer , how can i get back those codes which removed by mistake – sekhar Jan 12 '23 at 09:52
-
@sekhar https://stackoverflow.com/questions/75092580/my-local-branch-is-not-able-to-synch-with-master/75092703?noredirect=1#comment132517472_75092703: "But perhaps you only want to _revert_ your full commit?" Your question talks about undoing the removal of a file, but in the comments you are asking about undoing all changes of a commit. That's what `revert` is for (which I've written in my first comment and was already part of the answer: option 1) – knittl Jan 12 '23 at 09:58
-
If you have already executed part of my answer and got a result different from what you expect, please update your question by editing it and including the relevant commands and errors (if any). And of course, you have to push your branch after adding the new commits, otherwise you PR will not reflect the changes. – knittl Jan 12 '23 at 10:02