# Git Notes ## 查看 remote repo 名稱 ``` git remote -v ``` ## First Time Setup ``` git config --global user.name "YOUR_GITHUB_NAME" git config --global user.email "YOUR_EMAIL@MAIL" ``` ## Cloning a Repository from GitHub ``` git clone https://github.com/REPO_OWNER/REPO_NAME.git ``` ## Initialize a New Repository Go to github and create a repo call "YOUR_REPO_NAME" ``` git init vim .gitignore # Add files or directories you do not want to push git add . git commit -m "YOUR_COMMIT_MESSAGE" git branch -M main # Rename the current branch to main git remote add origin https://github.com/YOUR_USER_NAME/YOUR_REPO_NAME.git git push -u origin main ``` ## Renaming a Repository after rename at github ``` mv /path/to/ORIGINAL_NAME /path/to/NEW_NAME git remote set-url origin https://github.com/YOUR_USER_NAME/YOUR_NEW_REPO_NAME.git ``` ## Always Pull Before Pushing ``` git checkout main # Switch to the main branch git pull origin main git add . git commit -m "YOUR_COMMIT_MESSAGE" git push origin main ``` ## get information Get the status of the repository ``` git status ``` Get the commit log ``` git log ``` ## Working flow Create a new branch ``` git checkout main git pull origin main git checkout -b feature/my-new-feature ``` WORKING!!!!!!! ``` git add . git commit -m "Add my new feature" git push origin feature/my-new-feature ``` Merge branch to main ``` git checkout main git pull origin main # Ensure main is up-to-date git merge feature/my-new-feature git push origin main ``` ## other ### Remove Large Files from Git Cache ``` pip install git-filter-repo git filter-repo --path script/tempTesting/SecLists-master.zip --invert-paths git gc --prune=now --aggressive git push origin master ``` ### Merge Unrelated Branches Merge master to main ``` git checkout main git merge master --allow-unrelated-histories ``` ### Branch Management Create a new branch ``` git branch BRANCH_NAME ``` Switch to an existing branch: ``` git checkout BRANCH_NAME ``` Create and switch to a new branch ``` git checkout -b BRANCH_NAME ``` # Great Article [Branch](https://ithelp.ithome.com.tw/articles/10211790) ## 設定token自動輸入 啟動GCM ``` git config --global credential.helper manager-core ``` ``` git remote set-url origin git@github.com:NCNU-SCC/installScript.git ``` git remote set-url:這個命令用來修改遠端倉庫的 URL。 origin:這是 Git 預設的遠端倉庫名稱。通常,當你 clone 一個倉庫時,origin 會自動被設定為遠端倉庫的 URL。 git@github.com:NCNU-SCC/installScript.git:這是使用 SSH 協議的遠端倉庫 URL。相對於 HTTPS,SSH 是一種更安全的連接方式,並且不需要每次進行操作時輸入 token 或密碼。 # 分隔線分隔線分隔線分隔線分隔線 # Remote ``` git pull origin master 這個指令會將 main 的代碼更新到 Local 的 main 術語 pull request: 形式上認為,main 是項目的,非個人的, feature-branch 是個人的。 pull request 是請求項目的主人把新的 branch pull 到項目裡去。 術語 squash and merge: 面對 pull request,把分支上的所有改變,合併成一個改變,然後把此 commit 改變,放到 main 上。然後 feature branch 就會刪掉。 ``` 先 # Local ``` 術語 當前 checkout: 表示當前的分支,例如 checkout 是 main,代表當前分支是 main 術語 一個 commit: 你的代碼改動的統稱。 術語 HEAD: 代表最近的一次 commit。 術語 HEAD~1: 代表最近的一次 commit的前一個 commit,在只有一個 commit 時,代表初始狀態。 git checkout -b my_feature 這個指令會改動 Disk 上的代碼為 my_feature 分支的代碼 git rebase main 會從 main 的角度,合併 main 和當前 branch,可能會出現需要解決的衝突。 git reset --soft HEAD~1 把 Local 的狀態變成 HEAD~1 ,HEAD~1 是 HEAD的前一個。 git revert HEAD 添加一個和 HEAD 相反的 commit ``` # Disk ``` git checkout main 切換 Disk 的代碼為 main 分支 git status 查看提交的狀態,例: Changes not staged for commit Changes to be committed git diff 查看代碼的更動 ``` # 使用順序 有三件事。 1. 取得 repo 開始在 feature-branch 交代碼。 2. 每次寫代碼後 pull main,rebase main,push feature-branch 3. 在 feature-branch 的代碼完成後,pull main,rebase,p ## 得到 repo 並開始交代碼 Remote->Local ``` git clone https://github.com/you/your_repo ``` Local->Disk ``` git checkout -b my_feature ``` Disk->Local ``` 寫代碼 git diff git add [changed_file] git commit ``` Local->Remote ``` git push origin my-feature ``` ## 在 Remote update 的情況下同步 feature 裡 Local->Disk ``` git checkout main ``` Remote->Local ``` git pull origin main ``` Local->Disk ``` git checkout my-feature ``` Local->Local (my-feature 分支和 main 分支) ``` git rebase main ``` Local->Remote ``` git push -f origin my-feature ``` Remote ``` New pull request Squash and merge ``` ## 刪除 branch Local->Disk ``` git checkout main ``` Local->Local ``` git branch -D my-feature ``` Local->Remote ``` git pull origin master ``` # 撤銷操作 ## 在 Disk 上 Disk ``` 寫代碼 git diff git status 紅色的 ``` ``` git checkout [changed_file] ``` ## 在 Disk 上 add 了,在 Stating 裡 ### 移出 Staging ``` git reset [changed_file] ``` ### 移出 Staging 和 Disk ``` git checkout HEAD [changed_file] ``` ## 在 Disk 上 commit 了,在 Local 裡 ### 移出 Local ``` git reset --soft HEAD~1 ``` ### 移出 Local 和 Staging ``` git reset HEAD~1 ``` ### 移出 Local, Staging 和 Disk ``` git reset --hard HEAD~1 ``` ## 在 Local ``` git revert HEAD git revert 70a0(commit 的 hash) ``` ## Remote ### 公有分支(不只你在使用的分支) Local->Remote,添加到 Remote ``` git push ``` 公有分支是不能回頭的,添加 revert HEAD 來撤銷 ``` git revert HEAD git push ``` ### 私有分支(例如 feature branch) ``` git reset --hard HEAD~1 git push -f ```