Git

From DikapediaV2
Jump to: navigation, search

git - The stupid content tracker. Git is a stupid content tracker because it has no idea what's inside those blobs, and it doesn't try to store fine grained information like "lines 345-350 added, lines 502-508 removed" or anything like that.

https://github.com/git-guides

How to clone github repository to your local terminal


$ git clone https://github.com/ardikas/shell-scripts

How to set up your terminal with remote access to your Github repo


You may see this when you try to commit:

$ git commit
*** Please tell me who you are.  

Run

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

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <ardika@DESKTOP-TOREEKS.localdomain>) not allowed

Do:

git config --global user.email "myemail@gmail.com"
git config --global user.name "ardikas"


Another way to init Github repo:

git init
git config user.name "someone"
git config user.email "someone@someplace.com"

How to add SSH key and push using SSH


If you do git push and get this error, that is because password authentication was deprecated:

$ git push
Username for 'https://github.com': ardikas
Password for 'https://ardikas@github.com': 
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on 
currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/ardikas/python-scripts/'

Instead, my preferred way is to use SSH keys.

Follow these steps to generate an SSH key, add it to your Git Hub, and authenticate:

Then test the SSH key:

  • NOTE!! YOU ALWAYS HAVE TO RUN THESE 3 COMMANNDS WHEN YOU OPEN A NEW SSH TERMINAL. The SSH agent only persists for the duration of the terminal session. Once the session is closed, the agent is no longer running, and the key needs to be added again the next time you open a terminal. Additionally, every time you open a new terminal, you’re starting a new shell session. That’s why you need to start the SSH agent and re-add your key each time.
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/<githubkey>
$ ssh -T git@github.com
Hi ardikas! You've successfully authenticated, but GitHub does not provide shell access.

Change directory into the local clone of your repository (if you're not already there) and run:

$ git remote set-url origin git@github.com:ardikas/python-scripts.git

Now try editing a file (try the README) and then do:

$ git add -A
$ git commit -m "memo"
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.28 KiB | 1.28 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:ardikas/python-scripts.git
 * [new branch]      main -> main


Why does it appear to have two ssh key's set up on my system?
ardikas-mbp:cs-gy-6843 ardika$ ../github-ssh.sh 
Agent pid 12617
Enter passphrase for /Users/Ardika/.ssh/ads9055_github: 
Identity added: /Users/Ardika/.ssh/ads9055_github (ads9055@nyu.edu)
Hi ads9055! You've successfully authenticated, but GitHub does not provide shell access.

ardikas-mbp:cs-gy-6843 ardika$ ssh -T git@github.com
Hi ardikas! You've successfully authenticated, but GitHub does not provide shell access.

Your system appears to have two different SSH keys because SSH is likely using different identities depending on how it's configured. This happens for a few reasons:

1. Multiple SSH Keys in ~/.ssh Directory - Check how many SSH keys you have stored:

ls -l ~/.ssh/
  • If you see multiple private keys (e.g., id_rsa, id_ecdsa, id_ed25519, github_rsa), your system may be using different keys for different connections.

2. SSH Config File (~/.ssh/config) - Your ~/.ssh/config file may specify different SSH identities for GitHub. Run:

cat ~/.ssh/config
  • Example config file:
$ cat ~/.ssh/config
# Default GitHub account (personal)
Host github.com
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

# Other GitHub account (school)
Host github.com
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/ads9055_github
  • If you have two SSH keys mapped to GitHub, one may be used by default, and another when explicitly specified.
  • Your issue comes from having two Host github.com entries in your ~/.ssh/config file. What's Happening?
    • The first Host github.com block sets the SSH key to ~/.ssh/id_ed25519.
    • The second Host github.com block sets the SSH key to ~/.ssh/ads9055_github, but it overrides the first one.
    • Since both blocks use Host github.com, SSH always applies the last one in the file.
  • HOW TO FIX THIS


3. Multiple GitHub Accounts

  • Hi ads9055! might be your personal GitHub account.
  • Hi ardikas! might be your work/school GitHub account.
  • Each might have its own SSH key registered in GitHub → Settings → SSH and GPG keys.

4. SSH Agent Caching Multiple Keys - Run:

ssh-add -l
  • This lists currently loaded SSH keys. If multiple are listed, your system is using more than one identity.
  • If needed, you can remove all loaded keys and re-add a specific one:
ssh-add -D  # Remove all SSH keys from memory
ssh-add ~/.ssh/github_rsa  # Load only a specific key

5. To confirm which SSH key is being used when connecting to GitHub. The -vT flag provides verbose output, showing which identity file is being used.:

ssh -vT git@github.com

Using Multiple SSH Keys for GitHub on One Machine


To properly distinguish between two GitHub accounts, modify ~/.ssh/config like this:

# Default GitHub account (personal)
Host github-personal
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

# Other GitHub account (school)
Host github-school
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/ads9055_github

How to Use These Identities in Git
Since we've renamed the Host values (github.com → github-personal and github.com → github-school), you need to update your remote URLs in your repositories:

For Personal GitHub (personal), go to your personal repository folder and run:

git remote set-url origin git@github-personal:username/repository.git

For Work/School GitHub (school), go to your work-related repository folder and run:

git remote set-url origin git@github-work:ads9055/cs-gy-6843.git

How to Verify Which SSH Key is Used
You can now test which key is being used by running:

ssh -T git@github-personal
ssh -T git@github-work

This setup ensures that: ✅ Personal GitHub (personal) uses ~/.ssh/id_ed25519. ✅ School GitHub (school) uses ~/.ssh/ads9055_github.

How to commit/upload to git


Note: For Ardika, follow the How to add SSH key and push using SSH steps above to commit and push via your mac/pc.

To commit:

git add [new file]
git commit -m "memo"
git push

If you get: git@github.com: Permission denied (publickey), follow the steps below...

Workaround (need to find permanent fix):

  • I put this as a script:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/<githubkey>
ssh -T git@github.com
  • Then run:
git remote set-url origin git@github.com:ardikas/terraform
git add * || git add [file]
git commit -m "memo"
git push


Fatal: The current branch main has no upstream branch

If you try to do git push but you get the following error:

fatal: The current branch main has no upstream branch. 
To push the current branch and set the remote as upstream, use git push --set-upstream ads9055/CS-GY-6843-2025-Spring main

This error means that your current branch (main) does not have an upstream branch set for the remote repository. Essentially, Git doesn't know where to push your changes.

To resolve this, you need to set the upstream branch for your main branch. You can do this using the following command:

git push --set-upstream ads9055/CS-GY-6843-2025-Spring 

This command will push the main branch to the remote repository ads9055/CS-GY-6843-2025-Spring and set the upstream branch, so future git push commands will know where to send the changes.



[+] Ardika's GitHub Repositories



$ git config --list
user.email=<my email>
user.name=ardikas
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/ardikas/shell-scripts
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

How to select branch using git


cd into the repo and run git checkout:

cd [repo directory]
git checkout [branch]

How to check which branch you are in:

git status


How to remove files locally and sync it with GitHub Repo


Remove the file from the locally cloned directory by running git rm <file>. (This will delete the actual file!)

ardikas-mbp:cs-gy-6843 ardika$ git rm testfile
rm 'testfile'
 
ardikas-mbp:cs-gy-6843 ardika$ git rm testfile2
rm 'testfile2'

Then commit and push:

ardikas-mbp:cs-gy-6843 ardika$ git commit -m "test remove files"
[main 8528739] test remove files
 2 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 testfile
 delete mode 100644 testfile2

ardikas-mbp:cs-gy-6843 ardika$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 230 bytes | 230.00 KiB/s, done.
Total 2 (delta 0), reused 1 (delta 0), pack-reused 0
To github.com:ads9055/cs-gy-6843.git
   b7f857d..8528739  main -> main

What is the difference between git remote add and get remote set-url?


Both git remote add and git remote set-url are commands used to manage remote repositories in Git, but they serve different purposes:

  • git remote add:
    • Purpose: This command is used to add a new remote repository to your local Git configuration.
    • Usage: You typically use this when you want to associate a new remote repository with your local repository for the first time.
    • Example:
git remote add origin git@github.com:your-username/repository-name.git
    • Outcome: This adds a new remote repository named origin to your local configuration, pointing to the specified URL.



  • git remote set-url:
    • Purpose: This command is used to change the URL of an existing remote repository.
    • Usage: You use this when you need to update the URL for an already configured remote repository, perhaps because the remote repository has moved or you want to switch from HTTP to SSH.
    • Example:
git remote set-url origin git@github.com:your-username/new-repository-name.git
    • Outcome: This updates the URL for the existing remote repository named origin to the new specified URL.

In summary, git remote add is for adding a new remote, and git remote set-url is for updating the URL of an existing remote. They help you manage your connections to remote repositories efficiently.