Master Local File Syncing: Use Git Between Machines

Need to sync files between two local machines without relying on cloud storage? Git provides a powerful, version-controlled solution that gives you complete control over your data. This tutorial shows you how to set up secure local file synchronization using Git over SSH.

What you’ll need: Two machines on the same local network, Git installed on both, and basic familiarity with the command line.

Step 1: Enable SSH on the Host Machine

First, enable SSH access on the machine that will host your Git repository. This creates a secure connection between your computers.

On macOS: Navigate to System SettingsGeneralSharing, and enable Remote Login.

On Linux: Install and start the OpenSSH server:

sudo apt install openssh-server
sudo systemctl start ssh

On Windows: Go to SettingsSystemOptional featuresAdd a feature, then install OpenSSH Server.

Test the connection from your client machine:

ssh username@host-ip-address

Replace username with your account name on the host and host-ip-address with the host’s local IP (find it using ifconfig on macOS/Linux or ipconfig on Windows). If you can log in successfully, SSH is working.

Step 2: Assign a Static IP to the Host

To avoid connection issues from changing IP addresses, give your host machine a stable network identity. You have two options:

Option A: DHCP Reservation (Recommended)
Access your router’s admin panel and create a DHCP reservation that assigns the same IP to your host’s MAC address. This is the easiest method and works automatically.

Option B: Manual Static IP
Configure a static IP directly in your host machine’s network settings. You’ll need to set the IP address, subnet mask, gateway, and DNS servers manually. Consult your OS documentation for specific steps.

Step 3: Create a Bare Repository on the Host

On your host machine, create a bare repository—a Git repository designed specifically for syncing without a working directory. This prevents conflicts and simplifies the setup.

mkdir -p ~/git-repos
cd ~/git-repos
git init --bare my_synced_files.git

The .git extension is a convention that indicates this is a bare repository. Note the full path (e.g., /Users/yourname/git-repos/my_synced_files.git)—you’ll need it in the next step.

Step 4: Clone the Repository to Your Client Machine

On your client machine, clone the repository using SSH:

git clone ssh://username@host-ip/~/git-repos/my_synced_files.git
cd my_synced_files

Replace username and host-ip with your actual values. Git will prompt for your host password. The ~ expands to your home directory on the host.

Example: If your username is alice and host IP is 192.168.1.100:

git clone ssh://[email protected]/~/git-repos/my_synced_files.git

Step 5: Sync Files Between Machines

Now you can sync files using standard Git commands. The workflow is simple:

On the machine where you made changes:

git add .
git commit -m "Describe your changes"
git push origin main

On the other machine (to get updates):

git pull origin main

Important: Always git pull before starting work to get the latest changes, and git push when you’re done. This prevents conflicts.

Step 6: Handle Merge Conflicts

If you edit the same file on both machines before syncing, Git will report a merge conflict during git pull. Here’s how to resolve it:

  1. Run git status to see which files have conflicts
  2. Open each conflicted file—you’ll see conflict markers like this:
<<<<<<< HEAD
Your changes on this machine
=======
Changes from the other machine
>>>>>>> origin/main
  1. Edit the file to keep the version you want (delete the markers)
  2. Mark the conflict as resolved:
git add filename
git commit -m "Resolved merge conflict"

The conflict is now resolved and both machines are back in sync.

Step 7: Set Up SSH Key Authentication (Recommended)

Entering your password for every push and pull gets tedious. Set up SSH key authentication for password-free access:

On your client machine, generate an SSH key:

ssh-keygen -t ed25519 -C "[email protected]"

Press Enter to accept the default location. You can add a passphrase for extra security, or press Enter to skip.

Copy your public key to the host:

ssh-copy-id username@host-ip

Or manually: Copy the contents of ~/.ssh/id_ed25519.pub and append it to ~/.ssh/authorized_keys on the host.

Test it: Run ssh username@host-ip. You should connect without entering a password. Now Git operations will be seamless.

Security note: Keep your private key (id_ed25519) secure. Set permissions with chmod 600 ~/.ssh/id_ed25519.

Troubleshooting Common Issues

Problem: “Permission denied (publickey)” when cloning
Solution: Your SSH keys aren’t set up correctly. Verify the public key is in ~/.ssh/authorized_keys on the host with correct permissions (chmod 600).
Problem: “fatal: refusing to merge unrelated histories”
Solution: You may have initialized Git on both machines separately. Use git pull origin main --allow-unrelated-histories to force the merge, then resolve any conflicts.
Problem: Push rejected with “Updates were rejected”
Solution: The remote has changes you don’t have locally. Run git pull origin main first to merge remote changes, then push again.
Problem: “Connection refused” when connecting via SSH
Solution: Verify SSH is running on the host (sudo systemctl status ssh on Linux) and that firewalls aren’t blocking port 22.

When NOT to Use This Method

Git is excellent for text files and source code, but consider alternatives for:

  • Large binary files (videos, photos, ISOs): Git tracks every version, bloating repository size. Use Syncthing or Resilio Sync instead.
  • Databases or constantly-changing files: Git isn’t designed for files that change hundreds of times per day. Use rsync or specialized database replication.
  • Real-time collaboration: If multiple people need simultaneous access, use a proper Git hosting service (GitHub, GitLab) or collaborative tools like Google Docs.

Advanced Tips

Create a .gitignore file to exclude temporary files:

# .gitignore example
.DS_Store
Thumbs.db
*.tmp
node_modules/

Automate syncing with cron (Linux/macOS) for hands-free operation:

# Run every hour
0 * * * * cd ~/my_synced_files && git pull origin main

Use branches for experimental work:

git checkout -b experimental-feature
# Make changes
git checkout main
git merge experimental-feature

Why This Works Better Than Alternatives

Unlike rsync or cloud storage, Git gives you:

  • Complete history: See every change ever made with git log
  • Conflict resolution: Git detects and helps resolve conflicting edits
  • Selective syncing: Use .gitignore to exclude files
  • Branching: Work on different versions independently
  • No monthly fees: Everything runs on your hardware

This setup transforms Git from a developer tool into a robust local file management system, giving you enterprise-grade version control for your personal workflow.

Follow us on Bluesky, LinkedIn, and X to Get Instant Updates