Connecting Local Directory to Online Repository
On this page
These commands are used in Git Bash on Windows in order to connect a local directory to an online repository on github.com.
Install And Configure Git
Install Git.
apt install gitIf its the first time you use GitHub on your machine, you will have to setup your credentials.
git config --global user.name "yourGitHubUsername"
git config --global user.email "yourEmail@domain.com"This will add your credentials to the ~/.ssh/config we used in step 5.
Generate An SSH Key Pair
Generate a key pair. Create an ed25519 key pair on your local machine.
ssh-keygenThe previous command works fine as is. However for GitHub it is better to add some options.
ssh-keygen -t ed25519 -C "your_email@example.com" -f /home/user/.ssh/keyNameCommands options.
-t typeSpecifies the key type likersa,ed25519, orecdsa(the default isrsa).-b bitsSets the key length in bits.-C commentAdds a comment to the key like your email for more security.-f fileNameSpecifies the output file path in case you want custom names.
File location: Your key pair will be saved in your SSH directory.
- The private key will be saved in
~/.ssh/keyName. - The public key will be saved in
~/.ssh/keyName.pub.
Passphrase: You can optionally add a passphrase for extra protection.
- If you add one, you’ll need to type it when using the key (like a password for your key).
- If you leave it blank by pressing Enter, you’ll be able to use the key without typing anything.
Start The SSH Agent And Add Your Private Key (Optional)
If you're having problems with git recognizing your key on your machine. you can try to execute the following steps. Start the SSH agent.
eval "$(ssh-agent -s)"Add your key to the agent.
ssh-add ~/.ssh/id_ed25519 Confirm your key loaded.
ssh-add -lThe output of this command should show a fingerprint and your email
Copy The Public Key And Add It To Your GitHub Account
Print the public key.
cat /home/user/.ssh/keyName.pubPaste it into GitHub.
- Select and copy the entire line which starts with
ssh-ed25519and ends with your email. - In GitHub go to Settings → SSH and GPG keys → New SSH key.
- Paste the key, give it a title, and the save.
Make Sure GitHub Is A Known Host
Way 1 → Try connecting to GitHub through SSH, this will automatically add github.com to the known_hosts file.
ssh -T git@github.com If prompted Are you sure you want to continue connecting (yes/no)? type yes.
Way 2 → Add GitHub directly to the known_hosts file.
ssh-keyscan github.com >> ~/.ssh/known_hostsEnsure SSH Uses The Right Key
Create and edit ~/.ssh/config in order to set up GitHub configuration.
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/KeyName
IdentitiesOnly yesThis forces the use of that key for github.com and avoids offering many keys.
Link Local Directory To GitHub Repository
Initialize git.
git initAdd SSH remote.
git remote add origin git@github.com:yourUsername/yourRepo.gitAdd Commit And Push
Create and edit the .gitignore file.
nano /etc/.gitignoreAdd the files you don't want to be pushed to your online git repository.
Add your changes.
git add .gitignore hostsCommit your changes.
git commit -m "Initial commit: added .gitignore and hosts"Push your changes for the first time. When pushing for the first time an upstream should be set. An upstream basically tells your local directory which branch it should push to.
git push -u origin masterOptions for this command.
-u= sets an upstream--set-upstream= also sets an upstream exactly like-u
Once you’ve done the first push, Git remembers your upstream for your local directory. So the next time you want to push you can simply just push without mentioning the upstream.
git pushRemark For Cloning on Device With Two SSH Accounts
If a device has two GitHub accounts using SSH keys, you need to clone using the SSH link instead of the HTTP link form GitHub.
Suppose you want to clone:
https://github.com/angelo-mouawad/my-project.gitOn GitHub, you would normally click Code → SSH and see:
git@github.com:angelo-mouawad/my-project.gitBut because you have multiple accounts, change github.com to your SSH alias:
git@github-personal:angelo-mouawad/my-project.gitThen clone:
git clone git@github-personal:angelo-mouawad/my-project.gitThat's it, Git now sees git@github-personal.