Home > Net >  git not merging "undeleted" files
git not merging "undeleted" files

Time:06-03

Here is my situation. I was working on the master branch, and made several commits. At one point (a few commits back) I deleted 2 files. I decided I wanted them back and did the following:

  1. I checked out the commit where i still had those files.

  2. I did a switch -c (creating new branch) with these "undeleted" files.

  3. Wanting to then bring these back into my master branch, I did a merge.

Problem: the "undeleted" files did not come along for the ride, the master branch does not show the files from the branch where i had the files, i.e. it did not restore them despite my having merged the new branch created from that old commit.

So, is there a better way to restore deleted files? I am missing something as far as the Git methodology goes?

Any help / suggestions warmly welcomed! (^L^)

CodePudding user response:

You were on the right track, and there are multiple ways to fix this. Conceptually, an easy way that is guaranteed to work is:

  1. Check out the latest commit that had the files.
  2. Copy the files outside of the repo.
  3. Check out master (to the HEAD commit).
  4. Copy the files from outside the repo back where you want them.
  5. The files will appear as added files. Stage them and commit.

A more "Gitty" way to achieve the same thing would be to revert the commit that deleted the files. If the only thing that commit did was delete the files, then the solution is a one-liner:

git revert <commit-id-that-deleted-the-files>

This will create a new commit that undoes the specified commit, effectively doing the identical multi-step process described above, but in a single command.

If the commit did other things too, then you could do a partial revert, like this:

git revert <commit-id-that-deleted-the-files> --no-commit

# Now unstage all files except the two files you are "undeleting"

# commit the change
git commit # Adjust the commit message appropriately

# Now undo any other pending changes leftover from the revert
git reset --hard HEAD

If you use the revert command, Git proposes a useful commit message. However, if you modify the revert in some way, I recommend modifying the commit message like this:

Partial Revert "Previous commit message..."

This commit only reverts the deletion of files X and Y.

This reverts commit <commit-id-that-deleted-the-files>.

By the way, the reason your merge didn't work is because as described, the merge didn't do anything. When you merge, you only bring in commits that aren't already on your branch. The new branch you made was on the same commit ID and was already in your branch, so the merge had no effect. You may have even seen the response: "Already up to date."

CodePudding user response:

One option is to use the restore command to restore the files from the commit.

For example, say abcde is the commit hash for your commit with the missing files before they were deleted, and you want to restore deleted files file1.txt & path/to/file2.txt. The following adds those files to your working tree, then commits them:

git restore -s abcde -- file1.txt path/to/file2.txt
git add file1.txt path/to/file2.txt
git commit -m "Restoring deleted files"

Per the documentation:

Synopsis

git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>…​

[…]

Description

Restore specified paths in the working tree with some contents from a restore source. If a path is tracked but does not exist in the restore source, it will be removed to match the source.

[…]

Options

-s <tree>
--source=<tree>
Restore the working tree files with the content from the given tree. It is common to specify the source tree by naming a commit, branch or tag associated with it.

[…]

<pathspec>…​
Limits the paths affected by the operation.

  •  Tags:  
  • git
  • Related