I was happily working on my WordPress theme, swagdrip, getting it all set up for version control. I ran my usual Git commands to initialize the repository and push it to GitHub…

The Nightmare Begins

And then everything went wrong.

Suddenly, my git status was showing a ridiculous number of files—not just my theme, but .DS_Store, personal projects, and even my entire WordPress core files. Turns out, Git was tracking my entire home directory (/Users/mnrdsgn) instead of just my theme folder. 😱

What Happened?

Instead of running git init inside my theme directory, I had mistakenly initialized Git at the root of my home directory. This meant Git was now tracking everything in ~/, including Downloads, Documents, and all my random projects. This was a complete mess.

Fixing the Disaster

Step 1: Remove the Wrong Git Repo

First, I needed to delete the misplaced Git repository from my home directory:

cd ~
rm -rf .git

To confirm Git was no longer tracking my home directory, I ran:

git rev-parse --show-toplevel

If it throws an error (fatal: not a git repository), that means Git is gone from the wrong place. ✅

Step 2: Initialize Git in the Correct Folder

Next, I navigated to the correct directory where my theme actually lives:

cd "/home/mnrdjmke/public_html/wp-content/themes/swagdrip"

Since my path had spaces, I had to wrap it in quotes.

Then, I ran:

git init

Now Git was properly tracking only swagdrip.

To verify:

git rev-parse --show-toplevel

This time, it correctly returned:

/home/mnrdjmke/public_html/wp-content/themes/swagdrip

Victory! 🎉

Step 3: Add Remote Repo and Push

Now, I needed to reconnect GitHub and push my theme:

git remote add origin https://github.com/eboy79/swagdrip.git
git add .
git commit -m "Reinitialized Git inside swagdrip"
git push -u origin main

But nope—Git wasn’t done torturing me yet.

Step 4: Fixing the Git Push Rejection

Git rejected my push with this lovely message:

! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/eboy79/swagdrip.git'

GitHub already had commits in the swagdrip repo that my local version didn’t have. This meant Git wouldn’t let me overwrite the remote history without merging first.

Solution: Force Push or Merge Remote Changes

Option 1: Force Push (Overwrite Everything)

If I was 100% sure my local files were correct and I wanted to completely replace the remote repository, I could use:

git push --force origin main

⚠️ Warning: This deletes any commits from GitHub that aren’t in my local repo. Use carefully!

Option 2: Pull and Merge (Safer Option)

To be safe, I pulled the remote changes first:

git pull origin main --rebase

If there were conflicts, Git would prompt me to resolve them.

Then, I pushed my code again:

git push origin main

And finally, everything was clean and in sync. 🚀

Final Check: Confirming Git Is Fixed

To make sure Git was only tracking my theme, I ran:

git status

It should say:

On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

✅ If you see this, Git is happy, and the nightmare is over.

Lessons Learned

  1. Always check where Git is initialized with git rev-parse --show-toplevel.
  2. Be careful running git init in unknown locations.
  3. If you mess up, don’t panic. Remove .git, reinitialize, and re-add the remote.
  4. Git won’t let you overwrite a remote repo unless you pull first or force push.

Git can be a pain, but once you tame it, it’s a powerful tool. Hopefully, my mistakes help someone else avoid the same chaos! 🚀