---
name: update-github-repo-preserving-files
description: Update a GitHub repository with new content while preserving specific files, making text replacements, and handling authentication via PAT
domain: github
---

# Update GitHub Repo Preserving Files

Updates a GitHub repository with new content while preserving specific files (like CNAME), making text replacements, and handling GitHub authentication via Personal Access Token.

## When to Use

- You want to replace most content in a GitHub repo but keep specific files (e.g., CNAME, config files)
- You need to replace placeholders or text patterns across multiple files
- You're working with a repo that requires PAT for authentication
- You want to completely refresh repo content while preserving git history and specific assets

## Steps

### 1. Prepare Preservation List
Identify files/directories to preserve (e.g., `CNAME`, `.env`, `secrets/`).  
These will NOT be deleted when cleaning the repo.

### 2. Make Text Replacements
For each file requiring text changes:
```bash
sed -i 's|OLD_TEXT|NEW_TEXT|g' path/to/file
```
Use exact patterns to avoid unintended changes.

### 3. Clean Repository (Except Preserved Files & .git)
```bash
# List all files to delete (excluding preserved files and .git)
find . -type f \
  ! -path "./.git/*" \
  ! -name "preserved_file1" \
  ! -name "preserved_file2" \
  ! -name "preserved_dir/*" \
  -delete

# Remove empty directories (optional)
find . -type d -empty -not -path "./.git/*" -delete
```

### 4. Copy New Content
```bash
cp -r /path/to/new/content/* .
```
Ensure new content doesn't overwrite preserved files (they should be excluded from source).

### 5. Configure Git Remote (if needed)
```bash
git remote set-url origin https://github.com/username/repo.git
# or
git remote add origin https://github.com/username/repo.git
```

### 6. Handle GitHub Authentication
Git will prompt for credentials. To avoid interactive prompts:
```bash
# Store credentials temporarily (for this session)
git config credential.helper store
# Then push - it will use cached credentials
```
**Note:** For security, consider using `--no-edit` or environment variables in CI.

### 7. Commit and Push
```bash
git add -A
git commit -m "Update repo: preserve specific files, replace text, refresh content"
git push origin main
```

## Pitfalls & Solutions

### ❌ Accidentally deleting .git
**Solution:** Always exclude `.git` from deletion commands using `! -path "./.git/*"`

### ❌ Text replacements affecting wrong files
**Solution:** 
- Use specific patterns (include enough context)
- Test replacements on copies first
- Limit scope with `find` + `sed` when needed:
  ```bash
  find . -name "*.tsx" -exec sed -i 's|old|new|g' {} +
  ```

### ❌ Push rejected due to missing auth
**Solution:**
1. Ensure PAT has `repo` scope
2. Configure credential helper:
   ```bash
   git config credential.helper store
   ```
3. Push once to cache credentials

### ❌ Preserved files getting overwritten
**Solution:**
- Copy new content AFTER preservation step
- Or exclude preserved files from source copy:
  ```bash
  rsync -av --exclude='CNAME' --exclude='.env' /source/ .
  ```

## Verification

After push:
1. Check GitHub repo: https://github.com/username/repo
2. Verify preserved files are intact
3. Confirm text replacements are applied
4. Ensure no extra files remain

## Example: Sailor.bar Update

Preserved: `CNAME`  
Replaced: `https://t.me/your_bot` → `https://t.me/SailorBarBot`  
Steps:
1. `rm -rf *` but kept `.git` and `CNAME`
2. `sed -i 's|https://t.me/your_bot|https://t.me/SailorBarBot|g' app/src/components/*.tsx`
3. Copied kimibot contents
4. Used PAT via credential helper
5. Pushed to `https://github.com/valldun1/Sailor-bar.git`

## Related Skills

- `github-pat-push`: For pushing single files with PAT
- `deploy-sailor-bar`: For deploying Sailor.bar to web server
- `replace-sailor-bar-content`: Specific to Sailor.bar content replacement

---
*Security Note: Never log or expose PATs. Use credential helpers or environment variables for secure handling.*