# jonz94's Git Notes ###### tags: `git` ## Installation - On Windows, if you want to only install `git`, without `git-bash`, `git-gui`, etc, you can install `mingit` on https://github.com/git-for-windows/git/releases ## 設定 git 環境 https://git-scm.com/book/zh-tw/v1/ https://ihower.tw/blog/archives/5436 ``` # 檢查設定 $ git config --global --list # 設定 username 跟 email $ git config --global user.name <name> $ git config --global user.email <email> ``` ## 初始化資料夾 ```bash $ cd <directory> $ git init ``` ### 新增 remote 設定 git remote add ```bash # git remote add <remote_name> <ssh/https> $ git remote add origin git@github.com:user/project.git $ git remote add origin https://github.com/user/project.git # 查看目前的 remote 關係 $ git remote -v ``` * remote_name 對應到本機電腦的身分 * ssh / https 對應到遠端的 project * 使用 https 第一次 (或是每次) git push 或 git pull 時都要輸入帳號密碼 * 使用 ssh 則要設定 public 金鑰 (略) ## 出現 warning: ignoring broken ref refs/remotes/origin/HEAD 是因為遠端的預設分支改變了 - solution ```bash $ git remote set-head origin <新預設分支的名稱> ``` - e.g. ``` $ git remote set-head origin main ``` ## 將檔案加入本機 git add ```bash # 存入所有檔案 $ git add . $ git add -A # 存入特定檔案 $ git add <path/to/filename> ``` ## 加入說明 git commmit ```bash # 多行說明: 會開啟預設文字編輯器 $ git commit # 單行說明: 加上參數 -m $ git commit -m "單行說明文字" # 改寫上一筆 commit 訊息 $ git commit --amend ``` ## 執行 git commit 後開啟了 vim 、但此時想要取消 commit - `:cq` :::info - 可以在自己的 vim 設定檔裡面加入以下設定,爾後便可以使用 `:AbortGitCommit` 或 `:GitAbortCommit` 這兩個指令來取消 commit,這麼一來就不怕忘記 `:cq` 這個用法還要到處去 google 了XD ``` " abort git commit when editing the commit log message command AbortGitCommit :cq command GitAbortCommit :cq ``` ::: ## 將 git commit 時會開啟的預設文字編輯器設定成 vscode - on Linux or macOS ``` git config --global core.editor "code --wait" ``` - on Windows, you need to use **vscode's full path** ``` git config --global core.editor "'C:\\Users\\jonz94\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe' --new-window --wait" ``` ## 把檔案推上雲端 git push ```bash # git push <remote_name> <branch_name> $ git push origin master # 可以用 -u or --set-upstream 設定預設值 $ git push -u origin master $ git push --set-upstream origin master ``` ## 從雲端拉檔案到本機 git pull ```bash # git pull <remote_name> <branch> $ git pull origin master ``` ## 恢復到之前的版本 git reset ```bash $ git reset # 幾個~ 就回復幾個版本 git reset --hard HEAD~~~ # 從 HEAD 往前回復 3 個版本 ``` ## 管理分支 git branch ```bash # git branch -h 可以看到詳細的用法 $ git branch # 查看所有分支 (包含本地與遠端) $ git branch -a ``` ## 移動到不同分支 git checkout ```bash $ git checkout <branch_name> ``` ## 查看 commit 歷史紀錄 ```bash # normal $ git log # oneline $ git log --oneline # graph mode >w< $ git log --graph # check first commit $ got log --reverse ``` ### 回到某個 commit 紀錄的時間點 ```bash $ git checkout <commit_hash> ``` ## 遠端已經刪除了某個分支,本機如何同步刪除遠端分支 ```bash $ git fetch --all --prune ``` ## 從本地刪除遠端的分支 ```bash $ git push --delete origin <branch_name> ``` ## git tag 標籤 * 在本地端新增標籤 ```bash $ git tag -a <tag_name> # or $ git tag --annotate <tag_name> ``` * 同步剛新增的本地端標籤到遠端 ```bash $ git push origin --tags ``` * 在本地端移除標籤 ```bash $ git tag -d <tag_name> # or $ git tag --delete <tag_name> ``` * 同步剛移除的本地端標籤到遠端 ```bash $ git push --delete origin <tag_name> ``` * 從遠端只抓取 **特定** 的某個標籤到本地 > Credit: https://stackoverflow.com/a/45338695/9979122 ```bash # git fetch <remote_name> refs/tags/<tag>:refs/tags/<tag> $ git fetch origin refs/tags/1.2.3:refs/tags/1.2.3 $ git fetch origin refs/tags/v1.2.3:refs/tags/v1.2.3 ``` ## git cherry-pick * 撿 commit 合併到當前分支 ```bash $ git cherry-pick <commit_hash> ``` * To cherry-pick all the commits from commit A to commit B (where A should be older than B), run: ```bash $ git cherry-pick A^..B ``` ## git 移除「應該被 .gitignore 忽略、但卻仍然被 git 紀錄著」的檔案們 - source: https://stackoverflow.com/a/13541721/9979122 You can remove them from the repository manually: ```bash $ git rm --cached file1 file2 dir/file3 ``` Or, if you have a lot of files: ```bash $ git rm --cached ${git ls-files -i -c --exclude-from=.gitignore} ``` > The MAGIC part is `git ls-files -c -i --exclude-from=.gitignore` [color=#39c5bb] But this doesn't seem to work in Git Bash on Windows. It produces an error message. The following works better: ```bash $ git ls-files -i -c --exclude-from=.gitignore | xargs git rm --cached ``` for PowerShell on Windows: ```bash $ git ls-files -i -c --exclude-from=.gitignore | %{git rm --cached $_} ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up