ico4 GitHub

Syncing a VS Code Remote SSH Workspace with GitHub

This guide assumes you are connected to a remote cluster using VS Code Remote SSH and want to synchronize files with a GitHub repository.

1. Verify Git Is Available on the Cluster

Open a terminal in VS Code and run:

git --version


3. Configure Git (First Time Only)

Check your Git configuration:

git config --global user.name
git config --global user.email

If necessary, set them:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Use the email associated with your GitHub account.


4. Create a GitHub Repository

  1. Log in to GitHub.

  2. Click New Repository.

  3. Enter a repository name.

  4. If your project already contains files, do not initialize the repository with a README.

  5. Create the repository.

GitHub will display a repository URL such as:

git@github.com:username/repository.git

5. Set Up SSH Authentication

Check for an existing SSH key:

ls ~/.ssh

Look for files such as: .. code:: text

id_ed25519 id_ed25519.pub

If no key exists, create one:

ssh-keygen -t ed25519 -C "your_email@example.com"

Accept the defaults unless you have a reason to change them.


6. Add the SSH Key to GitHub

Display the public key:

cat ~/.ssh/id_ed25519.pub

Copy the entire output.

On GitHub:

  1. Go to Settings.

  2. Select SSH and GPG keys.

  3. Click New SSH key.

  4. Paste the key.

  5. Save.


7. Test GitHub Authentication

ssh -T git@github.com

Expected output:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

This confirms that SSH authentication is working correctly.


8. Connect the Local Repository to GitHub

Add the remote repository:

git remote add origin git@github.com:username/repository.git

Verify:

git remote -v

9. Create a .gitignore File

Create a .gitignore file in the repository root:

touch .gitignore

Example contents:

# Python
__pycache__/
*.pyc

# VS Code
.vscode/

# Logs
*.log
*.out

# Slurm
slurm-*.out

# Simulation outputs
*.traj
*.dcd
*.lammpstrj

# Temporary files
*.tmp

#Entire directories
logs/
RESTART/

Commit the .gitignore file:

git add .gitignore
git commit -m "Add gitignore"

10. Add Files

Stage all files recursively:

git add .

Review staged changes:

git status

11. Commit Changes

git commit -m "Initial commit"

12. Push to GitHub

First push:

git push -u origin main

The -u option sets the upstream branch so future pushes can be performed with:

git push

Troubleshooting: “Can’t push refs to remote”

If VS Code reports:

Can't push refs to remote. Try running "Pull" first to integrate your changes.

the GitHub repository likely contains commits that your local repository does not have (for example, a README).

Try:

git pull origin main --allow-unrelated-histories

Resolve any merge conflicts, then:

git push -u origin main

If the GitHub repository contains no important content and you want to overwrite it with your local repository:

git push -u origin main --force

Use --force with caution.


Daily Workflow

After making changes:

git status
git add .
git commit -m "Describe changes"
git push

To obtain updates from GitHub:

git pull

Helpful commands

To run a quicker status check if you have many untracked files in your working directory (large data files, trajectories, etc.), use the -uno or -ufalse option with git status to ignore any untracked files

To learn about the options for any command, run

git help <command>