Skip to content

Git

Git Commands

See status:

1
git status

Add more than one file to the git repo:

1
2
git add .
git add *

Commit files to local repo:

1
git commit -m "message to commit"

Push changes to remote repo:

1
git push origin master

Create Local Git Repository

Create a folder for the git repository then use the following command to initialise it as a git repo:

1
git init

connect to remote repo:

1
git remote add origin https://github.com/<username>/<repo>.git

verifiy the repo with:

1
git remote -v

if the remore repo already exists and you want to replace it:

1
git remote set-url origin https://github.com/<username>/<repo>.git

GitHub

Once git is installed and repo is cloned set the global username and password to allow automatic push and pull to work:

  • git config –global user.name “Your Name”
  • git config –global user.email “your email - try to use the one generated by github to shield your own email from commits”

install github cli:

1
winget install --id GitHub.cli -e

login to github:

1
gh auth login

create repo from current folder:

1
gh repo create work --private --source=. --remote=origin --push

Add SSH keys for use with github

generate keys:

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

Add key to ssh agent:

start service:

1
2
3
# start the ssh-agent in the background
Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent

Add SSH key:

1
ssh-add c:/Users/YOU/.ssh/id_ed25519

Add the public key to github: find the .pub file and add to github:

Find key in folder:

1
gc ssh-add c:/Users/YOU/.ssh/id_ed25519.pub

copy and paste public key into the keys section in github for account/repo

Clone Repo via SSH:

Authenticate via Git Credential Manager:

1
 git credential-manager configure

browse to location to place git repo and run clone command:

1
2
#get url for github repo and use git clone command
git clone git@github.com:username/reponame.git

##Use SSH keys and point each repo to an SSH remote.

1) Create SSH key(s) (Windows PowerShell)

For one account:

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

Press Enter for default path (~/.ssh/id_ed25519).

For two accounts (recommended: separate keys):

1
2
  ssh-keygen -t ed25519 -C "personal_email@example.com" -f ~/.ssh/id_ed25519_personal
  ssh-keygen -t ed25519 -C "work_email@example.com" -f ~/.ssh/id_ed25519_work

2) Start agent and add keys

1
2
3
4
  Get-Service ssh-agent | Set-Service -StartupType Automatic
  Start-Service ssh-agent
  ssh-add ~/.ssh/id_ed25519_personal
  ssh-add ~/.ssh/id_ed25519_work

(If one account, add that single key.)

3) Add public keys to GitHub

Show public key:

1
2
  Get-Content ~/.ssh/id_ed25519_personal.pub
  Get-Content ~/.ssh/id_ed25519_work.pub

Copy each key into GitHub:

  • GitHub > Settings > SSH and GPG keys > New SSH key

4) Create ~/.ssh/config

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes

  Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

5) Set repo remotes to the right host alias

Personal repo:

1
  git remote set-url origin git@github-personal:USERNAME/REPO.git

Work repo:

1
  git remote set-url origin git@github-work:ORG_OR_USER/REPO.git

Check:

1
  git remote -v

6) Test auth

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