git-rm
I use a git bare directory ~/dotfiles to manage my dotfiles, mostly stored under the ~/.config folder. I have an alias alias gitbare='git --git-dir="$HOME/dotfiles" --work-tree="$HOME"' defined in my .zshrc. I also use gpg and pass, and git, to manage my passwords, with a ~/.password-store folder. That means I have the home directory, with those selected subfolders and files, as a git repository, and I have a subfolder ~/.password-store as a sub git repository. Now the problem is, I can see the following four changes at the home folder level with gitbare, but not at the subfolder level inside .password-store, see below:
╭╴ alowree on Mac OS at ~ took 53s
╰─❯ gitbare ls-files --modified
.password-store/.gpg-id
.password-store/Business/alowree@twineintl.com-at-sellercentral.amazon.com.gpg
.password-store/Email/alowree@soundfreaq.com.gpg
.password-store/Email/alowree@twineintl.com.gpg
╭╴ alowree on Mac OS at ~
╰─❯ cd .password-store
╭╴ alowree on Mac OS via main at ~/.password-store
╰─❯ git ls-files --modified
╭╴ alowree on Mac OS via main at ~/.password-store
╰─❯What should I do to get rid of these modified records when running gitbare command?
This is a classic case of nested Git repositories. Your home directory Git repo (via gitbare) sees the .password-store folder as a subdirectory, but because .password-store is itself a Git repository, the parent repo sees the sub-repo's internal state as changes (since Git doesn't automatically ignore sub-repositories unless you configure it to).
There are a few ways to solve this:
Solution 1: Add .password-store to .gitignore (Recommended)
Add the .password-store folder to your home dotfiles repository's ignore list:
echo ".password-store/" >> .gitignore
gitbare add .gitignore
gitbare commit -m "Ignore .password-store sub-repository"This tells your main Git repo to completely ignore the .password-store directory and not track any changes inside it.
Solution 2: Use .gitignore with exclusion pattern
If you want to track some files in .password-store but not the nested Git repository, you can ignore just the Git metadata:
echo ".password-store/.git/" >> .gitignore
gitbare add .gitignore
gitbare commit -m "Ignore .password-store's git metadata"Solution 3: Make the parent repo aware of the sub-repo (if you want to track versions)
If you actually want the parent repo to track the state of the sub-repo, use git submodule:
# First, commit current state (if any)
gitbare add .password-store
gitbare commit -m "Add .password-store as submodule"
# Then convert to submodule
gitbare submodule add $(cd .password-store && git remote get-url origin) .password-storeBut this is likely overkill for your use case.
Solution 4: Untrack the modified files (if they're already tracked)
If these files were accidentally added to the parent repo before, you need to remove them from tracking while keeping them locally:
# Remove from git index but keep on disk
gitbare rm --cached .password-store/.gpg-id
gitbare rm --cached .password-store/Business/alowree@twineintl.com-at-sellercentral.amazon.com.gpg
gitbare rm --cached .password-store/Email/alowree@soundfreaq.com.gpg
gitbare rm --cached .password-store/Email/alowree@twineintl.com.gpg
# Add to .gitignore
echo ".password-store/" >> .gitignore
gitbare add .gitignore
gitbare commit -m "Remove .password-store files and ignore directory"Solution 1 is likely what you want - it keeps your password store as a completely separate Git repository while preventing your home dotfiles repo from seeing any changes inside it.
After I run the three commands, I still see those four modified files under .password-store:
╭╴ alowree on Mac OS at ~
╰─❯ # 1. Create/append to .gitignore
echo ".password-store/" >> .gitignore
# 2. Add the modified .gitignore to staging
gitbare add .gitignore
# 3. Commit the change
gitbare commit -m "Ignore .password-store sub-repository"
[main 7e3dedc] Ignore .password-store sub-repository
1 file changed, 1 insertion(+)
╭╴ alowree on Mac OS at ~
╰─❯ gitbare ls-files --modified
.password-store/.gpg-id
.password-store/Business/alowree@twineintl.com-at-sellercentral.amazon.com.gpg
.password-store/Email/alowree@soundfreaq.com.gpg
.password-store/Email/alowree@twineintl.com.gpgWhy this is happening and what should I do next?
The issue is that those four files were already being tracked by your gitbare repository before you added them to .gitignore. Git doesn't ignore files that are already tracked - .gitignore only prevents untracked files from being added.
You need to untrack them first. Here's what to do:
Step 1: Remove the files from Git's index (but keep them on disk)
gitbare rm --cached .password-store/.gpg-id
gitbare rm --cached .password-store/Business/alowree@twineintl.com-at-sellercentral.amazon.com.gpg
gitbare rm --cached .password-store/Email/alowree@soundfreaq.com.gpg
gitbare rm --cached .password-store/Email/alowree@twineintl.com.gpgOr more efficiently, remove everything inside .password-store from tracking:
gitbare rm --cached -r .password-storeStep 2: Verify they're no longer tracked
gitbare ls-files --modified
# Should show nothing nowStep 3: Commit the removal
gitbare commit -m "Remove .password-store files from tracking"Step 4: Verify they're now ignored
Make sure the files are no longer showing up as untracked:
gitbare status
# .password-store/ should NOT appear anywhere (neither modified nor untracked)Why this happens
.gitignoreonly affects untracked files- Files already in the Git index (tracked) continue to be tracked regardless of
.gitignore git rm --cachedremoves them from tracking while keeping the actual files on your disk
After completing these steps, your gitbare repository will completely ignore the .password-store directory, and its nested Git repository will work independently.
git rm
Remove files from repository index and local filesystem. More information: https://git-scm.com/docs/git-rm.
Remove file from repository index and filesystem:
git rm path/to/file
Remove directory [r]ecursively:
git rm -r path/to/directory
Remove file from repository index but keep it untouched locally:
git rm --cached path/to/file
Reduce the repository to only the .git directory:
git rm -r .
