Fix Missing Files Between Local and Remote in Git: A Step-by-Step Guide

Are you seeing files in your local branch but not in the remote repository? You may be running into a .gitignore issue or other tracking problems. When certain paths are listed in .gitignore
, Git won’t track those files, causing them to be missing once you push to the remote.
Key takeaways
.gitignore
can hide vital files, preventing them from being pushed.- Use
!
rules in.gitignore
to allow specific files or folders.
Why files go missing in Git
.gitignore
can contain rules that tell Git to skip directories or file patterns. If these directories or files are important—like images, logs, or configs—they won’t be committed or pushed, causing missing files on the remote branch.
Common .gitignore problem
For example:
// inside the .gitignore file
public/files
This ignores everything inside public/files/
. Any file or folder there won’t be committed, leading to .gitignore missing files issues.
How to fix missing files in git
Step 1: Check If a file is ignored
If a file is local but missing remotely, see if .gitignore
is ignoring it:
git check-ignore -v public/files/data.json
If the command references .gitignore
, the file is ignored.
Step 2: Adjust .gitignore Rules
You can allow specific folders or files:
public/files
!public/files/documents/
!public/files/logs/
public/files
ignores the entire folder.!public/files/documents/
and!public/files/logs/
allow tracking those subfolders.
Step 3: Clear cached ignored files
If files were previously tracked or are in the index:
git rm -r --cached public/files
This removes them from Git’s index (not from your local machine) so new rules can apply.
Step 4: Re-Add the allowed files
After clearing the cache:
git add public/files/documents/ public/files/logs/
git commit -m ""Re-adding important files""
git push origin main
Step 5: Verify files in the remote repository
Use:
git ls-tree -r origin/main --name-only | grep ""public/files/""
If they show up, the issue is resolved.
Conclusion
When facing missing files between local and remote in Git, especially due to .gitignore
rules, updating your ignore patterns and re-adding the necessary files fixes the problem. Follow these steps to ensure crucial assets aren’t silently ignored.
FAQs
No. 'git rm -r --cached' removes them from Git’s index but not your local machine.
Use 'git check-ignore -v [path]'. It will show you which rule is ignoring the file.
Yes. Use an '!filename' pattern in .gitignore. If needed, you can force-add files with 'git add -f'.