只是筆記而已,用到才備忘。找工作很重要,從 svn 要再回到 git 還是有點燒腦。
檢查改了哪些檔案
#
git status
恢復各別回未修改前的狀態,其中 filename 可以是 * 來恢復所有檔案,概念就是重新 checkout 檔案
#
git checkout -- <
filename>
看看這次改了什麼
#
git diff -- <
filename>
本地提交
#
git commit
單行列出紀錄
#
git log --oneline
顯示 commit 的詳細內容
#
git show commit_id
只顯示本地工作副本的提交紀錄
#
git reflog
增加追蹤檔案
#
git add filename
停止追蹤
#
git update-index --assume-unchanged filename
繼續追蹤
#
git update-index --no-assume-unchanged filename
Merge 其他分支的某個 commit
#
git cherry-pick commit-hash
++++++++++ 分支相關 ++++++++++
列出分支
本地
#
git branch --list
遠端
#
git branch --remotes
全部
#
git branch --all
切換分支
#
git checkout branch
建立分支
#
git branch branch
在
branch_name_father 建立分支
branch_name_son 並直接切過去
#
git checkout -b branch_name_son branch_name_father
將本地分支 push 成遠端分支
#git push remote_name branch_name
將本地目前分支掛勾到遠端分支
#
git push --set-upstream-to=remote/branch_r branch_l
直接 Checkout 遠端分支到本地分支
#
git checkout -b branch_l remote/branch_r
將分支
branch 併入
master
#
git checkout master
#
git merge branch
如果有衝突 (conflict) 可以使用 mergetool 進行排除
#
git mergetool
設定 mergetool (使用 kdiff3 會有更好的介面)
#
git config merge.tool vimdiff
#
git config merge.conflictstyle diff3
#
git config mergetool.prompt false
刪除已 merge 過的
branch 分支
#
git branch -d branch
捨棄並刪除 branch 分支
#
git branch -D branch
刪除
remote_name 遠端
branch 分支
#
git push --delete remote_name branch
同步遠端分支清單
#
git fetch
++++++++++ 分支相關 ++++++++++
捨棄前一次提交 (
限定未 push,發現前次提交還有問題,可以捨棄前一次提交,working tree 仍會保留修改過後的內容,其中 ^ 表示前一版)
#
git reset HEAD^
現有的專案建立新的 git 管理,然後 push 到空的 repository.
#
git init
#
git add .
#
git commit -m "
First commit"
#
git remote add remote_name remote_repository_URL
#
git remote -v
#
git push remote_name master
本地建立分支,將分支推到遠端新分支,並建立連結
#
git checkout -b branch_name_son branch_name_father
#git push remote_name branch_name
#
git push --set-upstream remote_name branch_name
在 sdk 分支進行修改,然後併入分支 develop
先確保分支 develop 已經乾淨(完成 commit)且同步
#
git status
#
git fetch --all
#
git pull
切換到分支 sdk
#
git checkout sdk
修改後 commit 並 push
#
git commit -a -e
#
git push
切回分支 develop
#
git checkout develop
合併分支 sdk 的修改 (如果有衝突需要手動修正)
#
git merge sdk
#
git commit -a -e
#
git push
要存取 GitLab 建立的repository 會需要 ssh key 跟 password,所以請記得去設定 password,產生 ssh key 的指令如下:
#
ssh-keygen -t rsa -b 4096 -C "
email address"
merge 衝突處理
#
git checkout br2
#
git merge br1
Auto-merging
file
CONFLICT (content): Merge conflict in
index.html
Automatic merge failed; fix conflicts and then commit the result.
#
git add file
#
git commit -m "conflict fixed"
修復 .gitignore 有列出卻還出現在 modified 清單上的問題
#
git rm --cached filename
++++++++++ Repository 相關 ++++++++++
建立完整的 repository 鏡像
一個 repository 在運行一段時間之後,可能會有很多分支。遇到需要移植到新的伺服器的狀況時,直接鏡像整個 repository 是比較單純的做法。
#
git clone --mirror src_git_url target_folder
#
cd target_folder
#
git push --mirror dst_git_url
更新 remote url
#git remote set-url remote_name new_url
更新後檢查 url 有沒有改成功
#
git remote -v
建立 tag 並推送到 remote
#
git tag -a tag_name -m '
tag_comment'
#
git push remote_name tag_name
++++++++++ 修改剛剛提交而且已經 push 的 message ++++++++++
先修改本地的提交
#git commit --amend
重新 push 內容
#git push --force-with-lease remote branch
例如使用 git bracnh --all 顯示為 remotes/origin/develop 的分支, 就執行 git push --force-with-lease origin develop
如果在重新 push 之前就已經 pull 回來, 就需要 hard reset,但是本地端的修改會消失, 執行前最好先透過 git status 找出修改過的內容並且備份
#git fetch origin
#git reset --hard remote/branch