# GIT - cheat sheet

### Install (Windows)

https://git-scm.com/

https://desktop.github.com/

### set up new repo

(create git repo in web gui first)

```
git init
git add .gitignore
touch README.md
git add README.md
git remote add myOrigin git@github.com:maxbis/examengesprekken
git push -u origin master
```

### ssh key

```
add ~/.ssh/id_rsa.pub in git hub web gui
```

### add file

```
git add <file name>
```

### update repo

```
git commit -a -m "comment"
git push
```

### Get updated files

```
git pull
```

### Overwrite local changes

```
git reset --hard
git pull
```

### New clone - install new

```
git clone https://github.com/maxbis/examengesprekken

// for Ubuntu server
sudo chgrp -R www-data examengesprekken
```

### Go to older version

```
git reset --hard 0ad5a7a6.....
```

### Git <span style="color: rgb(224, 62, 45);">clear cache</span> (if ignore file is changed)

```shell
git rm --cached config
```

### List all tracked files

```
git ls-tree -r HEAD --name-only
```

### Git issues with .ignore

```
git rm -r --cached .
git add .
git commit -m "Stop tracking files that are now ignored"
```

### Init new repo

```bash
cd path/to/class-cloud
git init
git add .
git commit -m "Initial commit"

<create new repo on githib>

git remote add origin https://github.com/maxbis/class-cloud.git
git push -u origin master

<on Linux>
git clone https://github.com/maxbis/class-cloud.git
```

### Set Remote Git &amp;&amp; Check git remote

<details id="bkmrk-ssh-key-ssh-keygen--"><summary>SSH key</summary>

ssh-keygen -t ed25519 -C "your\_email@example.com"  
  
eval "$(ssh-agent -s)"  
ssh-add ~/.ssh/id\_ed25519  
  
cat ~/.ssh/id\_ed25519.pub

</details>```
git remote set-url origin git@github.com:USERNAME/REPO.git
git remote -v
```

### Test Connection

```
ssh -T git@github.com
```

## Set up local repo (git), what?

Create git repository on own server and auto deploy.

### Init new git repo NEW

In /srv/repo

```
mkdir NEW; cd NEW
git init --bare --initial-branch=main /srv/git/NEW
```

### Init local repo NEW

```
git remote add origin ssh://max@qool.ovh:1611/srv/git/NEW
git push -u origin main  
```

### Clone

##### local

```
  git clone /srv/git/NEW /var/www/qool/NEW
```

##### remote

```
git clone ssh://max@qool.ovh:1611/srv/git/deployment /var/www/NEW
```

### Auto deploy upon push

```bash
vi /srv/git/deployment/hooks/post-receive
```

#### post\_receive script

```bash
  #!/bin/bash
  
  set -euo pipefail
  
  TARGET="/var/www/test"
  BRANCH="main"
  GIT_DIR="/srv/git/deployment"
  
  while read -r oldrev newrev refname; do
    if [ "$refname" = "refs/heads/$BRANCH" ]; then
      echo "Deploying $BRANCH to $TARGET..."
      git --git-dir="$GIT_DIR" --work-tree="$TARGET" checkout -f "$BRANCH"
      # optional: composer install, cache clear, reload apache, etc.
      # cd "$TARGET" && composer install --no-dev
      # sudo systemctl reload apache2
    fi
  done

```

\--