【GitHub】How to backup: 2025.7.15

備忘録

Backing up a folder to GitHub is an effective way to securely store and manage files, with the added benefit of version control. This guide outlines the key steps to back up a folder, manage versions, delete folders, and restore them, based on a practical example of backing up the folder D:\usr8_work\work_23_chatgpt\01_OnoPiano.Com\11_OnkoChanChatBot2025\02_OnkoChan. It also includes professional best practices for robust version management, as used by experienced developers.

Prerequisites

  • Git Installed: Ensure Git is installed on your local machine (download here).
  • GitHub Account: Create an account on GitHub.
  • Repository: A GitHub repository for storing the backup.
  • Basic Command-Line Knowledge: Familiarity with terminal or command prompt.

Step 1: Initialize and Back Up the Folder

  1. Create a GitHub Repository
    • Log in to GitHub, click the “+” icon, and select “New repository.”
    • Name it (e.g., OnkoChanBackup), choose public or private, and create it without initializing a README.
    • Copy the repository URL (e.g., https://github.com/username/OnkoChanBackup.git).
  2. Set Up the Local Folder as a Git Repository
    • Navigate to the folder:cd D:\usr8_work\work_23_chatgpt\01_OnoPiano.Com\11_OnkoChanChatBot2025\02_OnkoChan
    • Initialize Git:git init
  3. Add Files and Commit
    • Add all files to the staging area:git add .
    • Create a .gitignore to exclude unnecessary files:
      • echo "node_modules/" > .gitignore
      • echo "*.log" >> .gitignore
      • git add .gitignore
    • Commit changes:git commit -m "Initial backup"
    • Confirm committed files & folders : git status
  4. Link to GitHub and Push
    • Add the remote repository:git remote add origin https://github.com/username/OnkoChanBackup.git
    • Push to GitHub:git push -u origin main
    • Use a Personal Access Token for authentication if prompted (generate here).
  5. Verify
    • Check the GitHub repository to ensure all files are uploaded.

Step 2: Assigning Version Names

To manage backups effectively, assign version names using commits or tags.

  • Using Commit Messages:
    Include version numbers in commit messages:
    • git add .
    • git commit -m "Backup version v0.0.0"
    • git push origin main
  • Using Tags (Recommended for snapshots):
    Tag a specific commit:
  • git tag -a v0.0.0 -m "Version v0.0.0 backup"  #タグ名「v0.0.0」、メッセージ”Version v0.0.0 backup”:-a :「注釈付き(annotated)タグ」、-m:「メッセージを付ける」
  • git push origin v0.0.0  、git push origin –tags (全tag一括アップロード)
  • Tags are ideal for marking significant versions (e.g., v0.0.0, v0.0.1) and can be viewed in GitHub’s “Releases” tab.

Step 3: Reverting to a Specific Version

To restore the folder to a previous version (e.g., when old folder existed):

  1. Checknetworks: Check Commit or Tag History**:
    • check local-log
      • git log            #check local-log
    • check remote-log
      • git fetch           # まず最新状態を取ってきて
      • git log origin/main      # check remote-log
    • check local-tag
      • git tag #check local-tag
      • git tag -l -n #check local-tag+tagコメント
    • check remote-tag
      • git ls-remote –tags origin  #check remote-tag
    • check local-log,tag,etc…
      • git show
      • git show <commit_hash>
      • git show <tag>
  2. Revert to a Version:
    • Temporarily view a version: git checkout v1.0.0
    • Permanently reset to a version (warning: discards later changes):
      • git reset --hard v1.0.0
      • git push origin main --force
  3. Restore Specific Folder (e.g., old): git checkout v1.0.0 -- old git commit -m "Restored old folder from v1.0.0" git push origin main

Step 4: Deleting a Folder (e.g., old)

To remove a folder like old and update the backup:

  1. Locally via Git: git rm -r old git commit -m "Removed old folder for v1.0.1" git tag -a v1.0.1 -m "Backup after removing old" git push origin main git push origin v1.0.1
  2. On GitHub Web Interface:
    • Navigate to the old folder in the repository.
    • Click the “…” menu and select “Delete directory.”
    • Enter a commit message (e.g., Remove old folder) and commit changes.
    • If the option isn’t available, delete each file individually; the folder will disappear once empty.

Step 5: Restoring a Deleted Folder

If old was deleted (e.g., via git rm -r old):

  1. If Not Committed:
    • Unstage the deletion:git restore --staged old
    • Restore the folder:git restore old
  2. If Committed:
    • Revert the deletion commit:git revert HEAD git push origin main
    • Or restore from a specific version:git checkout v1.0.0 -- old git commit -m "Restored old folder" git push origin main
  3. If Pushed to GitHub:
    • Use the above commands and push changes.
    • For web-based restoration, locate the commit in “Commits,” copy file contents, and recreate manually (less efficient).

Professional Best Practices

Professional developers and teams use advanced version control strategies to ensure robust and scalable management. Here’s how pros handle version control for projects like 02_OnkoChan:

  • Branch-Based Workflow (Git Flow):
    • Use branches like main (stable), develop (integration), feature/* (new features), and release/v1.0.0 (specific versions).
    • Example: Create a branch to remove old:git checkout -b feature/remove-old git rm -r old git commit -m "Removed old folder" git push origin feature/remove-old Merge via pull requests for team review.
    • Why Pros Use It: Enables parallel development, safe experimentation, and structured releases.
  • Tagging for Snapshots:
    • Tag significant versions (e.g., v1.0.0) for releases or backups:git tag -a v1.0.0 -m "Backup v1.0.0" git push origin v1.0.0
    • Why Pros Use It: Clear milestones, easy rollback (e.g., git checkout v1.0.0 -- old), and integration with GitHub Releases.
  • Automation with CI/CD:
    • Use GitHub Actions for automated backups or folder management:name: Daily Backup on: schedule - cron: '0 0 * * *' jobs: backup: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: | git config user.name "GitHub Action" git config user.email "action@github.com" git add . git commit -m "Automated backup $(date +%Y%m%d)" git tag -a "backup-$(date +%Y%m%d)" -m "Daily backup" git push origin main git push origin "backup-$(date +%Y%m%d)"
    • Why Pros Use It: Reduces manual errors and supports frequent backups.
  • Semantic Versioning:
    • Use MAJOR.MINOR.PATCH (e.g., v1.0.0) for clear version tracking.
    • Example: v1.0.1 for minor updates, v2.0.0 for major changes.
    • Why Pros Use It: Standardized versioning improves clarity and compatibility.
  • Documentation:
    • Maintain detailed README.md and commit messages (e.g., Removed old folder for v1.0.1).
    • Why Pros Use It: Ensures traceability and team communication.
  • Preferred Approach: Most pros combine Git Flow with tags for flexibility and clarity. For 02_OnkoChan, create a release/v1.0.0 branch for backups and tag it with v1.0.0. Use feature branches (e.g., feature/remove-old) for changes like folder deletions, merged via pull requests.

Tips and Best Practices

  • Use .gitignore: Exclude unnecessary files (e.g., node_modules, logs) to keep backups clean.
  • Backup Locally: Before major changes, copy the folder:xcopy D:\usr8_work\work_23_chatgpt\01_OnoPiano.Com\11_OnkoChanChatBot2025\02_OnkoChan D:\backup\02_OnkoChan /E /I
  • Automate Backups: Create a script for regular backups:@echo off set VERSION=v1.0.1 cd D:\usr8_work\work_23_chatgpt\01_OnoPiano.Com\11_OnkoChanChatBot2025\02_OnkoChan git add . git commit -m "Backup %VERSION%" git tag -a %VERSION% -m "Backup %VERSION%" git push origin main git push origin %VERSION%
  • Large Files: Use Git LFS for files over 100MB.
  • Force Push Caution: Avoid git push --force in shared repositories to prevent overwriting others’ work.

Conclusion

Backing up a folder to GitHub with version control is straightforward with Git and GitHub’s web interface. By using commits, tags, and professional workflows like Git Flow, you can maintain organized backups, delete unwanted folders, and restore them as needed. For the folder 02_OnkoChan, combining branch-based workflows with tags ensures robust, scalable version management, as practiced by professionals.

For advanced automation or team collaboration, leverage GitHub Actions and pull requests. Happy backing up!

コメント

タイトルとURLをコピーしました