1

After creating branch Y from working branch X, I deleted some files to reduce visual noise by not seeing those files around on the file system, while working on branch Y. These files are being changed in branch X.

Here is the scenario:

A - B [origin/master] \ C - D - G - H [origin/X] \ E - F [Y] 

Changes from branch Y are also pushed to origin/Y.

I already raised a pull request (PR) on branch Y for review.


How can I rewrite the commit history in origin/Y to get a file back?

5
  • 1
    Possible duplicate of How to undelete a file previously deleted in git's history? Commented Jul 21, 2019 at 3:34
  • @eftshift0 Branch Y is already on PR request on latest commit. Is this query not different from attached? Commented Jul 21, 2019 at 3:52
  • that part is not explained there... but the recipe to get the files back is still the same.... now, when you get your corrected branch, then you do: git push -f origin Y. After you have force-pushed the branch, the PR should update on github or bitbucket. Commented Jul 21, 2019 at 3:55
  • @eftshift0 I have the parent of the commit that deleted the file say id1. What is id of the previous commit in step 2 below? I have more than one file deleted after id1 Commented Jul 21, 2019 at 4:22
  • The parent ID is the id you use to run the git checkout <rev-id> -- <deleted-filepath> command. Commented Jul 21, 2019 at 4:24

2 Answers 2

1

I will give you a simple recipe as a fallback, just in case. Suppose your branch is called A, and you deleted files on A~3 (that would be, 3 revisions behind A):

git checkout A~3 # We set ourselves on the revision where the files where deleted git checkout HEAD~1 -- file1 file2 file3 # Get back the files from the previous revision git commit --amend --no-edit # Commit the new revision with the deleted files back in place git cherry-pick A~3..A # Replay revisions after this new revision # If you like the results: git branch -f A # Set the branch on this new position git checkout A git push -f origin A 

That should work.

Sign up to request clarification or add additional context in comments.

12 Comments

I see this approach more simple... Because in my case one file is deleted an one file is renamed and modified. But this is changing my commit history.
Commit history is gonna change... at least the content of the revisions... even if you keep the same line of revisions with comment/author, you are rewriting history.
So, I want to apply this approach on the commit am currently on. But isn't this going to require a new commit after applying this aproach and then push those changes? Because I have a PR request that is on current commit?
So, what you want is to create a new additional revision where you get the files back? Sure, it's possible.... not the most elegant way to handle it, but possible.... and sure, you will need a new commit after the checkout of the deleted files.
After commit history changes, does git push -u origin Y create any issue? because commit history is changed in local branch Y?
|
1

As eftshift0 mentioned in the comments, you might find this answer useful. It shows that you can restore the lost file and rewrite the Git history with the following:

git rebase -i <id of the commit with the files before they were deleted> git checkout <id of the commit previous to the one you're currently on> <filename> git commit --amend git rebase --continue git tag -d originalHead 

Nevertheless, there is value in keeping the git history unaltered, so another option to recover the lost file without changing git history can be found at this answer, which says this:

git checkout <id of the commit with the files before they were deleted> <filename>

In addition, if you want to recover multiple files, this answer shows some ways to do that, which could for example be running the git checkout command multiple times.

10 Comments

previous commit would be the commit that was previous to the commit where the files disappeared. In addition, if you want to recover multiple files this answer shows some ways to do that. I will update my answer.
From the directory am running these commands, there are two files deleted in subdirectory dir1.
Does this also maintain the new files created in latest commit?
It should. Take a look at git status.
git checkout will maintain the new files in the latest commit unless you checkout a file that you've modified in your current branch.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.