# 常用git整理 延伸補充: - [git指令](/1IafNr2VToaQqoAdP5Q1jA) - [git各種情境的使用](/ryymhqQlReaUfYkkyhpCVQ) ---         ## 配置相關 ``` $ git config --list ``` ## 初始化 ``` $ git init [dir-name] 設置fetch/push的remote位置 $ git remote add <name> <git-repo-url> $ git remote add origin git@git.com:xxx 下載項目 並在local產生對應的remote分支 $ git clone [url] 只抓取前幾次的commit $ git clone --depth=1 [url] ``` ## 暫存區相關 ``` $ git add [file1] [file2] ... $ git add [dir] $ git add . $ git add --all $ git add -p [file] 只stage特定範圍 patch add 將某個檔案恢復成最新commit原本的樣子 $ git checkout -- [filename] $ git checkout -- . 為了明確表示要操作的是文件而不是分支,建議加上 -- 預設從HEAD恢復內容 Restore working tree files $ git restore [file] 退回 unstaged 狀態 $ git restore --staged [file] ``` > git只關注內容變化 只改檔名無法track: [關於檔名的大小寫](https://gitbook.tw/posts/2018-06-05-case-sensitive) <!-- reset主要用途是操作HEAD的指向位置,所以建議不要使用reset將檔案移出暫存區 而檔案還原盡量使用restore指令 --> ## 查看 ``` 查詢現在這個目錄的「狀態」 $ git status git操作歷史(HEAD變更紀錄) 常用於還原一些操作 找回commit $ git reflog 當前branch的commit紀錄 $ git log --oneline 簡化版 --graph 分支圖 --all 詳細 --grep="keyword" 與keyword相關的commit --author "username" 這個人的commit -[num] 近幾次的commit 其餘還可以根據內容有相關的搜尋 顯示暫存區與工作區commit的差異 $ git diff 顯示某次commit的變化 $ git show [commit] 查看這個檔案每一行 是誰在什麼時候 哪次commit寫進去的 $ git blame [file] ``` 設置方便的log快捷到`git ls` 使用git bash輸入 `$ git config --global alias.ls 'log --graph --all --pretty=format:"%C(yellow)%h %C(red)%d %C(green)%s %C(white)[%an] %ar"'` `git config --global alias.ls 'log --oneline --graph --decorate --all'` 或是到`~/.gitconfig`修改  > [檢視版本紀錄](https://bwaycer.github.io/git-command.booklet/content/local/git-log.html) > [其它方便的設定](https://gitbook.tw/chapters/config/convenient-settings) > [漂亮的cmd graph工具:git-graph](https://github.com/mlange-42/git-graph) ## 分支 ``` 列出當前分支 $ git branch [branch] 新增分支(HEAD不動) [branch] [commit] 新增分支指在commit上 -b [branch] 新增分支(HEAD會跟著移動) -d [branch] 刪除分支 -D [branch] 強制刪除 就算尚未合併 -m [branch] 修改當前分支名稱 -M [branch] 強制重命名此branch分支 若已存在會覆蓋 -r 列出remote分支 -a 列出所有分支(包含remote) -vv 查看本地分支對於remote分支的track狀態 -vva 切換到指定分支 並更新工作區 $ git checkout [branch] $ git switch [branch] 新增分支並切換過去 $ git checkout -b [branch] $ git switch -c [branch] clone只會拉主分支main 如果想要遠端上的其他分支 如dev $ git checkout -b develop origin/develop 或 $ git switch -c develop origin/develop 如果develop比main還新 那麼不會自動fetch最新內容 但是現在都有預設auto-fetch 所以通常都會是最新的 ``` ## 合併 ``` 合併指定分支到當前分支 $ git merge [branch] 不進行fast forward $ git merge --no-ff [branch] $ git merge --no-ff -m "msg" [branch] 選擇某幾個commit到當前分支之後 $ git cherry-pick [commit] 先檢過來但不合併 $ git cherry-pick [commit] --no-commit ``` ## 切換基底 ``` 把當前分支嫁接到branch這分支(HEAD會跟著移動) $ git rebase [branch] 把bugFix 嫁接到main上 $ git rebase main bugFix 從某個commit開始重新提交 可選擇要挑哪些 $ git rebase -i [commit] 遇到衝突時根據情況選擇繼續或取消 $ git rebase --continue/--abort ``` ## 撤銷 ``` 產生一個commit 去撤銷指定commit $ git revert [commit] ``` ## 重設:切換存檔點 ``` 重設到某個commit的樣子 $ git reset [commit] --mixed 預設 只重製暫存區 --soft 工作區/暫存區都不變 只重設到這個commit --hard 工作區/暫存區都重製 ``` ## 提交 ``` 提交當前暫存區到倉庫 $ git commit -m "msg" $ git commit [file1] [file2] ... -m "msg 修改上一次commit $ git commit --amend -m [message] 修改commit 但不更改msg $ git commit --amend --no-edit ``` ## remote相關 ``` 下載所有不在 local 上的 commit $ git fetch 沒有輸入則選擇設置好的upstream $ git fetch [remote address] $ git fetch origin <source>:<destination> <source>:remote上的branch <destination>:放置這些commit的local的位置 跟push相反 $ git fetch --prune 同步遠端倉庫的最新狀態 並刪除已經不存在的遠端分支 $ git branch -d -r origin/feature/update-readme 如果那個分支還存在 手動移除 $ git remote prune origin Git 自動清理所有已刪除的遠端分支 $ git remote update origin --prune 同步所有遠端分支的最新狀態 並刪除已經不存在的遠端分支 顯示remote資訊 $ git remote -v 新增倉庫位置 並給個代稱 通常為origin $ git remote add [name] [url] $ git remote remove [name] 修改遠端倉庫的URL 同時修改fetch/push位置 $ git remote set-url origin https://github.com/原始專案/倉庫.git 只修改push位置 $ git remote set-url --push origin https://github.com/你的帳號/倉庫.git fetch+merge $ git pull [remote] [branch] --rebase 改用rebase $ git push origin <source> 預設是推送到遠端倉庫的"同名"分支 如果遠端沒有這分支 會自動建立 $ git push origin <source>:<destination> source:local的特定commit位置 destination:遠端對應的branch名稱 若遠端的destination 不存在 就會自動建立這個branch $ git push origin :<destination> source留空 則可以刪除遠端的分支 移除遠端倉庫的對應分支 也會跟著移除本地的remote分支 $ git push -u origin main branch 'main' set up to track 'origin/main'. git push -u origin <branch> 會將 <branch> 推送到 origin 並設定 upstream 關聯。 後續git pull/push可以省略origin和branch,Git會自動找到對應的遠程分支。 git branch --unset-upstream 可取消關聯。 查看本地分支對於remote分支的track狀態 $ git branch -vv ```
×
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