Overriding deleted files

0
Hi,   I have dozens of branches. One, of course, is the main branch. The other is called Internship. I made my developments on my Internship branch and committed them. Then I moved to the main branch and merged the Internship branch into main. Then, I committed a few things as extra in a main branch. However, when I realized I'd broken something in main, I deleted all the Internship branch related files in the main branch, essentially restoring it to its pre-merge state.   I switched back to my Internship branch, continued my development, and committed them. Later, when I tried to pull my main branch into my Internship branch, I found that all my internship-related developments had been overridden by the deleted files. How can I fix this? I want the final Internship branch to override the internship-related files (deleted files) in the main branch, not the other way around.
asked
2 answers
1

I can help you with that. This is a common Git pitfall.

 

The problem happened because when you deleted the files on the main branch and committed, you didn't undo the merge. Instead, you created a new commit that explicitly told Git, "The correct state of this branch is for these files to be deleted." When you later merged main into Internship, Git correctly applied that new "delete these files" instruction to your Internship branch.

 

The safest and most recommended way to fix this is to revert the commit where you deleted the files.

 

Step 1: Find the Commit Where You Deleted the Files

First, you need to find the hash of the specific commit where you removed the files.

 

Switch to your main branch:

Bash:

git checkout main

 

Look at the commit history to find the commit where you deleted the files. The --oneline flag makes it easier to read.

Bash:

git log --oneline

  1. Your output will look something like this. You are looking for the commit with a message like "Removed internship files" or similar.
  2. a1b2c3d (HEAD -> main) Extra commit I made
  3. e4f5g6h Removed internship files  <-- THIS IS THE ONE YOU WANT
  4. c7d8e9f Merge branch 'Internship'
  5. b6a5c4d Initial commit

Copy the hash of that commit (in this example, e4f5g6h).

 

Step 2: Revert the Deletion Commit

Now, you'll create a new commit that undoes the deletion.

While still on the main branch, run the revert command with the hash you just copied:

Bash:

git revert e4f5g6h

 

Your code editor will open, asking you to confirm a commit message for the revert. The default message is usually fine (e.g., "Revert 'Removed internship files'"). Just save and close the editor.

Your main branch is now fixed! The files from the Internship branch have been restored.

 

Step 3: Update Your Internship Branch

Now you can safely bring the fixed history from main into your Internship branch.

 

Switch back to your Internship branch:

Bash:

git checkout Internship

 

Merge the corrected main branch into it:

Bash:

git merge main

 

Everything should now be in order. Your Internship branch has all of its latest developments, and the "deleted" files are restored. You can now continue your work and merge into main in the future without this issue.

 

Why This Happened: A Quick Explanation

Think of Git history as a logbook, not an editable document.

  • Merge: You added a log entry: "Merged Internship's work."
  • Mistake: You added another log entry: "Deleted all those files."
  • Pull/Merge: When you pulled main into Internship, Git read the logbook and saw the most recent instruction regarding those files was "Delete them," so it did.
  • git revert: This adds a third log entry: "Undo the 'Deleted all those files' entry." This cleanly reverses the mistake without erasing the historical record.

answered
0

Hello,

If reverting the deletion commit on main is not an option (because the files really needed to be deleted there), one simple workaround is to create a small dummy commit.

The idea is that once main has a commit saying “delete these files”, Git will always try to apply that deletion whenever it’s merged. By adding a dummy commit (for example, editing a README or adding a temporary file) on both main and Internship, you “break the chain” of that deletion history. After that, when merging Internship into main, Git allows the Internship branch versions of the files to override the deleted ones instead of removing them again.

This way you don’t need to rewrite history, and you still get main cleaned up while preserving the latest Internship work.

 

Regards

Reemali

answered