# 自然人眼中的 Git 大概念如下圖,配合著基本指令 ![image](https://hackmd.io/_uploads/rJu4glbwJg.png) ## Basic * Start * `git init` * `git branch -M main` local branch * `git remote add origin <url>` remote server * Most use command * `git add .` * `git commit` -> [How to Write a Git Commit Message](https://cbea.ms/git-commit/). Separate subject from body and use vim to edit gitmessage template. In gitconfig, `[commit] template = /home/<user>/.gitmessage.txt` `[core] editor = vim` * `git push -u origin main` -> push to 'origin' server's 'main' branch * `git logs` * `git reset --soft HEAD` -> [【狀況題】剛才的 Commit 後悔了,想要拆掉重做…](https://gitbook.tw/chapters/using-git/reset-commit) * How to create and push a new branch * `git branch <new branch name>` * `git check <new branch name>` * `git push -u origin <new branch name>` [Git Push Local Branch to Remote – How to Publish a New Branch in Git](https://www.freecodecamp.org/news/git-push-local-branch-to-remote-how-to-publish-a-new-branch-in-git/) ## Situation ### Convinent your github info 直接紀錄 github info,不用每一次都要輸入 ``` git config --global user.email "emailaddress@yahoo.com" git config --global user.name "username" ``` ### Reset Your Personal Access Token (PAT) 重新設定 personal access token 有兩個辦法,推薦使用 credential.helper 1. Set PAT in newURL ```shell! $ git remote set-url <remoteRepositoryName> https://<USER>:<TOKEN>@github.com/<REPO-OWNER>/<REPO-NAME>.git ``` 以我的身份要 remote 到組織中的 git 為例: ```shell! $ git remote set-url origin https://keepgoing-228:XXX@github.com/ASRockAI/asrchat.git ``` 2. Use credential.helper (better) ``` git config --global credential.helper store ``` then it will ask the userName and password(PAT) ### Unable to create '/path/my_project/.git/index.lock': File exists Remove the file directly. ``` rm -f ./.git/index.lock ``` ### Pull/Push other branch Just fetch to doublecheck the newest version then checkout remote branch. ``` git fetch origin git checkout -b feature-branch origin/feature-branch ```