You get the .git folder back by restoring it from a backup or a remote copy, or by re-initializing the repository and reconnecting it to its remote. The important condition: if the folder was deleted, your committed history and branch refs are gone locally, but your working files usually survive. Recovery depends less on magic and more on which copy of that history still exists somewhere else.
A missing .git directory turns your project into an ordinary folder of files. Git commands fail with "not a git repository," and you lose the local record of commits, branches, and staged changes. The good news is that most users have a second copy of that history on a remote host like GitHub, GitLab, or Bitbucket, and the working files on disk are often untouched. The path to recovery runs from "check what survived" to "reconnect to a known-good history," and the order matters because an impulsive re-init can quietly bury information you could have kept.
First, confirm what actually went missing
Before restoring anything, verify the specific problem. Open a terminal in the project folder and run git status. The failure you see narrows the cause:
"fatal: not a git repository" means Git can't find a .git directory here or in any parent folder. Either it's truly gone, or you're in the wrong directory. A different error like "detected dubious ownership" or a corrupt object message means the folder may still exist but be unreadable or damaged. That's a repair problem, not a deletion problem.
Check the literal path. Run ls -a (macOS/Linux) or dir /a (Windows) to include hidden entries; on Unix-like systems the folder is named exactly .git, and a leading dot hides it. Then confirm you're in the folder you think you are with pwd or cd. A surprising share of "my .git folder vanished" cases are a wrong working directory or a project opened from a different clone.
If you're on macOS or Linux and want to know whether the folder is merely misplaced, a bounded search from your home directory works: find ~ -maxdepth 4 -type d -name .git 2>/dev/null. That lists every Git folder nearby so you can see whether it ended up one level up or in a sibling clone.
Success check: git rev-parse --git-dir should print a path. If it errors, you've confirmed the folder is unavailable and can move to recovery.
Check the recycle bin and any trash or snapshot before rebuilding
Deleting a .git folder usually does not destroy its contents immediately. On Windows, look in the Recycle Bin. On macOS, check Trash. On Linux desktops, check the trash location your file manager uses (commonly ~/.local/share/Trash/files). If the folder is there, restore it to the project root and run git status to confirm.
This is worth doing first because a restored folder preserves everything a re-init cannot: reflog entries, stashes, unpushed local branches, and the exact configuration. It costs a minute and occasionally saves hours.
Version-control and backup tools may also hold a copy. If the project sits inside a Dropbox, OneDrive, iCloud Drive, or Google Drive folder, check that service's version history and deleted-files view. Those services keep prior versions for a limited window that varies by plan and provider, so the sooner you look, the better. On macOS, Time Machine local snapshots can contain the folder even without an external drive. On Windows, File History or a restore point may include it. On Linux, look at your backup tool of choice, or at filesystem snapshots if your setup takes them.
Failure case to know: if the folder was deleted inside a Git operation's cleanup, by a script, or by a tool like a cleaner app, the trash route may not apply, and you should go straight to the remote-restore path.
Restore from a backup or an existing clone
If you have a full backup that includes the hidden .git folder, restoring is straightforward: put the folder back at the project root, keeping its exact name, then run git status.
Routine file backups often skip dot-folders by default, so a backup that seems complete may not contain .git at all. Verify by browsing the backup archive for the hidden directory rather than assuming.
A second local clone of the same repository is also a usable source. Say you have ~/work/project with a missing .git and ~/backup/project that is a healthy clone. You can copy the healthy .git folder into the damaged location, but do this deliberately:
1: Rename the damaged project folder or move it aside so nothing gets overwritten. 2: Copy the entire .git directory from the healthy clone into the same level as your working files. 3: Run git status inside the restored folder. 4: Expect a long list of modified or untracked files, because the restored history reflects the other clone's last commit, while your working tree reflects whatever you've edited since.
That mismatch is normal, not damage. Review the diff before committing anything, because the restored HEAD may point at an older commit than your files. If it does, you can reconcile by committing your current files on top, or by checking out the branch and comparing.
One caution: don't copy a .git folder from an unrelated repository. The commit history and remotes won't match your project, and you'll spend more time untangling it than reconnecting cleanly would have taken.
Reconnect to the remote with a fresh init
When no backup or clone exists but the project has a remote, rebuilding is the standard route. You keep your working files and pull the history back from the server. This works only if the commits you care about were pushed. Anything committed locally but never pushed is not on the remote and cannot be recovered this way.
Prerequisites: network access to the remote, credentials that can read the repository, and a known remote URL. If you don't remember the URL, get it from the hosting web interface.
1: Move into the project folder. If you want a safety net, copy the folder first so the original stays untouched. 2: Run git init. This creates a fresh .git directory with no commits and no remote. 3: Add the remote: git remote add origin . 4: Fetch everything: git fetch origin. 5: Point your local branch at the remote's branch: git branch --set-upstream-to=origin/main main (substitute the actual branch name, commonly main or master; check with git branch -r after fetching). 6: Check out so Git aligns the index and working tree: git checkout main or git switch main. 7: Run git status and git log --oneline -5 to confirm history is present and your file changes show as modifications.
What to expect: after step 6, files that match the remote commit show as clean; files you changed locally show as modified; files the remote has that you don't show as deleted until you restore them. Run git diff before any commit so you understand the delta.
Success check: git log shows the expected commit history, git remote -v prints the right URL, and git status reports the correct branch with upstream tracking.
Stop condition: if git fetch asks for credentials you don't have, or the remote returns an authorization error, don't keep guessing at passwords or paste tokens into shared terminals. Contact the repository owner or your organization's administrator to restore access properly.
Decide between recovery paths using a simple comparison
The choice comes down to what each path preserves. The criteria are the same across options: retained history, retained local uncommitted work, retained local-only commits, and effort.
| Situation | Best path | History kept | Local uncommitted work | Effort |
|---|---|---|---|---|
| .git found in trash or backup | Restore the folder | Everything, including reflog and stashes | Untouched | Very low |
| Healthy second clone exists | Copy its .git | Commits up to that clone's state | Preserved in working tree | Low to moderate |
| Remote has all needed commits | git init plus fetch | Remote commits only | Preserved as modifications | Moderate |
| No remote, no backup, no clone | Rebuild from working files | None | Only current file contents | High, and lossy |
The recovered history is always the intersection of what survived and what each source contains. A remote restore cannot bring back a commit that was never pushed. A backup restore cannot bring back work done after the backup was taken. State those limits to yourself before you start so you don't overestimate what you'll get.
Recover uncommitted work and local-only commits
The .git folder is where your index, stashes, and unpushed commits live, so losing it means those are at risk. Your working files usually remain, which means any uncommitted edits to tracked or untracked files still sit on disk and can be committed after reconnection.
Unpushed commits are the hard case. If a commit exists only in the deleted folder, no remote copy has it, and re-initializing won't conjure it back. That's why the trash and backup checks come first: they're the only paths that might still contain those objects.
If the project has no remote at all and no backup, the only surviving record is the current content of your files. You can initialize a new repository, commit the files as a fresh baseline, and continue, but the prior commit history, branch structure, and messages are unrecoverable. Say that plainly to anyone who depends on the project's history rather than pretending the rebuild is equivalent.
If you later find the deleted folder in a backup, don't merge it blindly. Instead, inspect it in a separate location with git log, note the branch and commits you want, and then bring those into your rebuilt repository as a remote: git remote add oldrepo /path/to/recovered/.git won't work directly because a bare .git path is awkward; a cleaner tactic is to clone from the recovered folder into a temporary directory, then add that clone as a remote and fetch. From there you can cherry-pick or merge. This is advanced, so only attempt it if the lost commits genuinely matter.
Repair symptoms that look like a missing folder
Not every "git folder gone" is a deletion. Distinguish these cases because the fix differs:
Corrupt index or objects: git status fails with object or index errors while the folder is present. Try git fsck to inspect integrity, and restore from a backup or remote rather than deleting files by hand. Wrong ownership: Git refuses to operate with a "dubious ownership" message, often after copying a project between users or machines. The supported remedy is to mark the directory as safe for your user with git config --global --add safe.directory , provided you trust the folder. Case or path confusion: on case-insensitive filesystems, a folder named .GIT or a migrated project may not resolve. Confirm exact naming. Partial clone or worktree confusion: if the project is a Git worktree, the real .git is a small file pointing elsewhere, not a folder, and deleting it breaks the link. Restoring a worktree link is a different procedure than restoring a normal repository, and you'll need the main repository to re-add it.
Stop condition: if git fsck reports missing or corrupt objects and you have no backup or remote copy, that is the point to involve whoever administers the repository or its backups. Continuing to run repair commands on a damaged object store can reduce what's salvageable.
Version and platform differences that affect recovery
The core commands are stable across Git versions, but a few details vary and are worth knowing as of late 2026.
Default branch naming shifted over time. Repositories created years ago often use master; newer ones typically use main. After fetching, git branch -r shows the actual remote branch names, so use those instead of guessing. Setting upstream to a branch that doesn't exist produces a confusing error that looks like a missing remote.
git switch is a newer, clearer alternative to git checkout for changing branches, available in Git 2.23 and later. Either works for the reconnect procedure. If you're on an old Git build, git checkout is the safer choice.
Remote authentication has tightened. Hosting providers have largely moved away from password-based pushes and pulls in favor of personal access tokens, SSH keys, or device-based login flows, and the exact method depends on the host and your organization's policy. Because these requirements change and differ per provider, verify the current method from your hosting provider's documentation or your administrator rather than relying on an older tutorial.
Filesystem details matter too. On Windows, hidden folders require dir /a; on macOS, Finder hides dot-folders by default, so use Command+Shift+. to reveal them or verify in Terminal. Line-ending settings from core.autocrlf can make restored files appear modified even when their content is logically identical, so check git diff before assuming the restore introduced real changes.
A worked scenario to make the trade-offs concrete
Suppose a developer deletes a project folder's .git directory by accident while cleaning up. The folder contains three branches, two of which were pushed, one with local-only commits, plus uncommitted edits to five files. There's no backup of the project folder, but the remote has the two pushed branches.
The order of checks: look in the trash first (found nothing, because the deletion went through a script). Check cloud sync history for the folder (not applicable). Then attempt the remote route. Of the three branches, the pushed two come back in full with their commit histories. The third branch's local-only commits are gone; the working files on disk still reflect the latest edits, and their content is preserved as modifications. Of the five uncommitted edits, all five survive because the files themselves weren't deleted. After reconnection, the developer sees two clean branches and a modified working tree, commits the five files, and accepts the loss of the local-only commit messages, which were never shared anyway.
That outcome matches what the recovery paths predict: history comes from the copies that existed, and uncommitted work survives because it lives in the files, not the folder. It also shows why the trash check comes first; had the folder been there, the third branch's commits would have been saved too.
Frequently asked questions
Does deleting the .git folder delete my files? No. The .git directory holds version history, configuration, and branch references, not your project's working files. Your source files normally remain exactly where they were, apart from the loss of version tracking. That's why reconnection and re-init are viable: the content you were editing is still on disk.
Can I recover unpushed commits after the folder is gone? Only if another copy of those commits exists somewhere: a backup, a clone, or the trash or a snapshot that still holds the deleted folder. A remote can't return commits that were never pushed. If no copy exists, the commits are unrecoverable, though the current file contents may still represent the state you wanted to save.
Can I just run git init and keep working? Yes, but treat it as starting a new history, not recovering the old one. Running git init in place gives you a fresh empty repository. If your project has a remote, reattach it and fetch so the existing history returns. If it doesn't, you're beginning a new history with the files as your first commit, and every prior commit is gone.
Is it safe to copy a .git folder between projects? Only between copies of the same repository. The folder's commits, remotes, and configuration describe one specific project history. Copying it from an unrelated repository produces mismatched history and confusing diffs. Copying it from a clone or backup of the same repository is a legitimate recovery technique if you review the differences before committing.