Git

From DikapediaV2
Revision as of 00:45, 28 January 2025 by Ardika Sulistija (talk | contribs)
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 THIS 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


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.