# git & github
[toc]
## New Device
### install git
```bash
~$ sudo apt install git
```
### setting user
```bash
~$ git config --global user.name "YuShan122"
~$ git config --global user.email "sunnychen9262@gmail.com"
~$ git config --list # check
```
### ssh key
```bash
~$ ssh-keygen -t ed25519 -C "sunnychen9262@gmail.com"
```
Then press <kbd>enter</kbd> three times.
Check `~/.ssh/id_ed25519.pub`.
Copy the public key, without email address, and add a new ssh key in your github setting.
## New Repository
### create new repository on github (remote)
Yeah, you know how to do that. Just click "New Repo" and make some settings.
### initailize the repository (local)
```bash
~/repo_path$ git init
```
Then `.git` is generated. You can check with `la`.
Creat `.gitignore`:
```bash
~/repo_path$ vim ./.gitignore
```
Then write these in `.gitignore`:
```
# my ROS template
.vscode/
devel/
build/
.catkin_workspace
```
You can check with `git status`.
Then start using git:
```bash
~/repo_path$ git add . # add all files staged
~/repo_path$ git commit -m "first commit" # commit staged files. "-m" allows you to write a short commit message in command line; otherwise a text editer would be opened.
~/repo_path$ git branch -M main # rename the branch as "main". default branch name: "master".
```
### link local and remote
```bash
~/repo_path$ git remote add origin git@github.com:YuShan122/repo_name.git
~/repo_path$ git push -u origin main # "-u" links local and remote repo
```
## Clone Existing Repository
```bash
~/workspace_path$ git clone <url>
# cloning all branches, the default option
```
```bash
~/workspace_path$ git clone <url> <new_name>
# rename the repository when you clone it
```
```bash
~/workspace_path$ git clone -b <branch_name> --single-branch <url>
# "-b" cloning the specified branch
# "--single-branch" clone only one branch without downloading other branches
```
```bash
~/workspace_path$ git clone --recurse-submodules <url>
# "--recurse-submodules" clone repository with submodule, including the init and update things
```
## Change Remote URL
```bash
~/workspace_path$ git remote set-url origin <new_url>
```
```bash
~/workspace_path/.git$ vim config
# edit if you need to
```
```bash
~/workspace_path$ git fetch
~/workspace_path$ git log
# check the remote head pointer
```
## Submodule
### clone repository with submodule
```bash
~/workspace_path$ git clone --recurse-submodules <url>
```
### remove submodule
```bash
~/workspace_path$ git rm <submodule>
# add ".gitmodule" and commit
~/workspace_path/.git$ vim config
# delete submodule url and other info
```
## Commonly Used
### information
```bash
~/repo_path$ git status # check status
~/repo_path$ git log # check commit history
```
### commit
```bash
~/repo_path$ git add file_name
~/repo_path$ git add . # add all edited files
~/repo_path$ git commit -m "message"
```
or just use VSCode extension. That is good.
[new_empty_directory](https://gitbook.tw/chapters/using-git/add-folder-to-git)
### branch
```bash
~/repo_path$ git branch # list all branches and check which one we're on now
~/repo_path$ git branch branch_name # create new branch
~/repo_path$ git branch -d branch_name # delete branch
~/repo_path$ git checkout branch_name # switch branch
```
[delete_branch](https://www.freecodecamp.org/news/how-to-delete-a-git-branch-both-locally-and-remotely/)
### merge
```bash
# if we are on branch "main" now
~/repo_path$ git merge branch1 # merge "branch1" into "main"
```
If confict occurs, edit the code and commit again.
### pull
```bash
~/repo_path$ git fetch # check remote version
~/repo_path$ git pull # git fetch + git merge
~/repo_path$ git pull origin branch_name # the other choice
~/repo_path$ git push --set-upstream origin eurobot_localization # push new branch
```
### push
```bash
~/repo_path$ git push # usually use this
~/repo_path$ git push origin branch_name # specify remote and branch
```
## Good References
[連猴子都能懂的Git入門指南](https://backlog.com/git-tutorial/tw/)
[Git-Reference](https://book.git-scm.com/docs)