http://stackoverflow.com/questions/916822/unable-to-diff-files-in-two-separate-branches-in-git
git diff branch:UNREADME master:README
git difftool somebranch:UNREADME otherbranch:README (用vim比較工具查看)
View the change history of a file using Git versioning
http://stackoverflow.com/questions/278192/view-the-change-history-of-a-file-using-git-versioning
git log -p filename ( to let git generate the patches for each log entry )
git show HEAD ( get just the diff for a specific commit )
將所有新增的檔案加入:
$ git add .
http://stackoverflow.com/questions/572549/difference-of-git-add-a-and-git-add
將檔案移除
$ git rm filename
將整個資料夾移除
$ git clean -f -d
http://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy
讓某個目錄不再追蹤
$ git rm -r --cached <your directory>
http://stackoverflow.com/questions/1329291/ignoring-an-already-checked-in-directorys-contents
git移除所有刪除的檔案
Removing multiple files from a Git repo that have already been deleted from disk
http://stackoverflow.com/questions/492558/removing-multiple-files-from-a-git-repo-that-have-already-been-deleted-from-disk
http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git
$ git add -u
OR
$ git add -A
在本地端還原成某個commitID狀態$ git reset --soft HEAD^
查哪個紀錄改了什麼檔案
http://stackoverflow.com/questions/2557564/how-do-i-identify-files-directories-that-were-added-or-removed-in-a-git-commit
$ git whatchanged -n 1
http://kevyu.blogspot.tw/2011/10/git-stash.html
使用 $ git stash 暫存現在改的狀態再checkout 其他branch 繼續做其他事
查這個commit是哪個branch
How to list branches that contain a given commit?
http://stackoverflow.com/questions/1419623/how-to-list-branches-that-contain-a-given-commit
$ git branch -a --contains commit_tag
查git commit是否含有該文字
$ git log --all --grep='XXX'
--all 會查所有branch
查專案中檔案是否含有該文字(比直接grep快)
$ git grep -e XXX -e OOO
http://stackoverflow.com/questions/215718/how-do-i-reset-revert-a-specific-file-to-a-specific-revision-using-git
取出某個版本的某個檔案
Assuming the commit you want is abcde: $ git checkout abcde file/to/restore
20130717
在checkout另個branch時,因為用ctrl+c中斷掉了,使得git reset --hard後留下一堆untracked files 如何刪除這些untracked files和資料夾:
http://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy
git clean -f -d
to be sure that also directories are gone! you can check withgit status
if they are really gone.
顯示這個commit有哪些檔案被改過
http://stackoverflow.com/questions/424071/list-all-the-files-for-a-commit-in-git
$ git diff-tree --no-commit-id --name-only -r bd61ad98
或
$ git show --pretty="format:" --name-only bd61ad98
但無法顯示是新增、修改還是刪除,也無法像git pull時顯示+++-- 的示意符號
只移除某commit的修改(不是把該commit之後的commit改變都移除)
$ git revert commmitId
GIT複製別人本地端的branch
http://www.ptt.cc/bbs/Linux/M.1387124688.A.38A.html
方法1: $ git remote add jack ssh://jack.s.ip/home/jack/repo # 使用git remote rm jack 移除jack這個remote $ git fetch jack $ git checkout -b local_branch jack/local_branch # 在本地開local_branch,內容為jack/local_branch 若jack和master裡面有同樣名字的branch(ex. dev_branch),在git pull時pull到master(之前加入)的dev_branch 方法2: $ git diff master local_branch > local_branch.diff # git diff commitId_old commitId_new $ git checkout -b local_branch master $ git apply local_branch.diff $ git commmit -am "Apply patch from Jack's local_branch" # -am 自動add且commit
復原 'git add .' 在提交(commit)之前
http://stackoverflow.com/questions/348170/undo-git-add-before-commit
$ git reset .
復原某個已經被git add的檔案
$ git reset HEAD <file>
讓git diff 有顏色
http://stackoverflow.com/questions/10998792/how-to-color-the-git-console-in-ubuntu
$ git config --global color.ui auto
git push時需要輸入帳號密碼
Git push requires username and password ( github )
http://stackoverflow.com/questions/6565357/git-push-requires-username-and-password
解:把remote的url設成ssh
$ git remote set-url origin git@github.com:username/repo.git
git remote set-url origin xxx 也是設定fetch網址的方法
查某兩個版本之間改過(新增)了哪些檔案
http://stackoverflow.com/questions/6879501/filter-git-diff-by-type-of-change
$ git diff 8702591419104918304ea46dc02ec65e3a8830a4 0136fef0d24d2fd3f1a8c83633e3cef7e7fdcaab --diff-filter=A
git diff時忽略空白(如用tab和space縮排方式改變不顯示出來)
$ git diff -w
被.gitignore的檔案已經被commit了,如何讓他真的被ignore
http://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository
$ git rm -r --cached .
$ git add .
$ git commit -m ".gitignore is now working"
如果是配置文件被git rm --cached,線上更新時該commit會把該文件刪掉
忽略檔案模式(chmod)改變
如果chmod 檔案權限而不改內容,git status仍然會列出差異
http://stackoverflow.com/questions/1580596/how-do-i-make-git-ignore-file-mode-chmod-changes
$ git config core.fileMode false
這樣只改變權限就不會被列出來了
列出所有被刪除過的檔案
http://stackoverflow.com/questions/6017987/is-there-a-way-in-git-to-list-all-deleted-files-in-the-repository
列出log中哪個commit刪除了哪些檔案
$ git log --diff-filter=D --summary
git diff 只列出某檔名的差異
http://stackoverflow.com/questions/8554776/what-option-should-be-used-restrict-the-git-diff-to-a-given-set-of-file-extensio
列出html檔差異
$ git diff -w commit_old commit_new -- *.html
只列出index.html檔差異
$ git diff -w commit_old commit_new -- *index.html
git diff 忽略新增和刪除的檔案
http://stackoverflow.com/questions/6894322/how-to-make-git-diff-and-git-log-ignore-new-and-deleted-files
$ git diff -w commit_old commit_new --diff-filter=M
revert多個commit
http://stackoverflow.com/questions/1463340/revert-multiple-git-commits
假設 log 是 A -> B -> C -> HEAD
要回復到A
$ git revert --no-commit D # commit由新到舊
$ git revert --no-commit C
$ git commit -m 'revert B and C commit'
git diff 顯示方法名稱而不是class名稱
http://stackoverflow.com/questions/7344577/is-there-a-way-to-ask-git-diff-to-show-the-method-name-instead-of-the-class-name
新增 .gitattributes(放到你家目錄下即可)或 .git/info/attributes 的倉庫中,並加入下面這行
*.php diff=php
( git version >= 1.4 )
1. 將 這行放到 ~/.gitattributes
2. 執行 git config --global core.attributesfile "~/.gitattributes"
如果你想要讓他便全局,將上面那行放到 /etc/gitattributes ( 未測 )
最後git diff 結果就會顯示修改的部份是在哪個function內,而不是在哪個class內
新增空資料夾在git倉庫
http://stackoverflow.com/questions/115983/how-can-i-add-an-empty-directory-to-a-git-repository
因為git存放的是檔案,在.gitignore中寫下
tmp/
忽略整個tmp/資料夾下的檔案同時也忽略了tmp資料夾,但是不想忽略tmp資料夾又想忽略tmp資料夾下檔案,要怎麼做呢?
在每個你要忽略的資料夾下寫下 .gitignore ,內容為
# Ignore everything in this directory
*
# Except this file
!.gitignore
!test/ # 不忽略tmp下的test資料夾,但同時test資料夾下也必須新增同樣的.gitignore檔案
或是使用.gitkeep
https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep
.gitkeep 不是git的特性,因為git不能新增空目錄,所以人們通常將.gitkeep放到空目錄使得能將該目錄加到版本庫
修改commit內容
http://stackoverflow.com/questions/179123/edit-an-incorrect-commit-message-in-git
$ git commit --amend
$ git commit --amend -m "New \`commit\` message"
反引號 grave/backticks( ` ) 必須要跳脫
設定git預設編輯器為vim
http://stackoverflow.com/questions/6098742/using-git-commit-a-with-vim
$ git config core.editor "vim"
合併兩個不同repos但code 類似的專案
http://stackoverflow.com/questions/13040958/merge-two-git-repositories-without-breaking-file-history/20974621#20974621
因為code管控,手上的較舊的code (old_project)改動,現在要依功能(如:留言板)分批合併到另一個repos(new_project)上正式的code上面
先在 old_project 上回復到比較舊的版本後開一個branch(guestbook),將留言板功能的commit從master 分批cherry-pick到guestbook分支上,並commit成一個記錄
先將舊的old_project加入新new_project庫的remote上
$ git remote add old_project file:///var/www/html/old_project
fetch
$ git fetch old_project
將 old_project的guestbook分支在new_project上生成
$ git branch guestbook old_project/guestbook
將old_project/guestbook 的留言板功能commit cherry-pick到new_project 的master上
$ git cherry-pick commit_id
這時候已經成功一半了,但仍有少部分檔案會有衝突,在git status的 Unmerged paths中,此時要手動解決,我的方法是:保留舊版本衝突的檔案,然後參考 old_project/guestbook 上留言板功能的git diff分別進去手動加入更動
只將有實際更改(不含空白差異)的檔案做git add
http://stackoverflow.com/questions/3515597/add-only-non-whitespace-changes
$ git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
git add untracked 的檔案
http://stackoverflow.com/questions/7446640/adding-only-untracked-files
$ git add -i
第一個詢問選 a ( add untracked )
第二個詢問選* ( 全選 )
地三個詢問選q ( quit )
push 到gerrit的庫失敗
http://stackoverflow.com/questions/16586642/git-unpack-error-on-push-to-gerrit
$ git push origin master
Counting objects: 1, done.
Writing objects: 100% (1/1), 199 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
error: unpack failed: error Missing tree 2d0d7370fe441e2c2439c0e5a523ad55bc5a0731
fatal: Unpack error, check server log
To ssh://bear@gerrit.test.com:29418/project.git
! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'ssh://bear@gerrit.test.com:29418/project.git'
解法:
$ git push --no-thin origin master
cherry-pick 後出現both added的檔案
http://stackoverflow.com/questions/9823692/resolving-a-both-added-merge-conflict-in-git
使用原本的檔案
$ git checkout --ours src/MyFile.cs
使用commit-id新的檔案
$ git checkout --theirs src/MyFile.cs
從比較舊的commit id 開始,開一個新的分支
http://stackoverflow.com/questions/18054057/git-checkout-commit-id-is-changing-branch-to-no-branch
$ git checkout -b <new_branch_name> commit_id
僅打包兩個commit id之間更改的檔案
http://stackoverflow.com/questions/4541300/export-only-modified-and-added-files-with-folder-structure-in-git
http://tosbourn.com/using-git-to-create-an-archive-of-changed-files/
http://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit
假設git log 是這樣 A -> B -> C
列出B,C 一共改了哪些檔案
$ git diff --name-only A C
或
$ git diff-tree --no-commit-id --name-only -r A C
將B,C 改的檔案打包
$ git archive -o update.zip HEAD $( git diff-tree --no-commit-id --name-only -r A C)
或
$ git archive -o update.zip C $( git diff-tree --no-commit-id --name-only -r A C) =>如果不是打包最新版的內容的話
或 如果只打包新增(A)和修改(M)的檔案(打包刪除的發生錯誤)
$ git archive -o update.zip HEAD $( git diff-tree --diff-filter=AM --no-commit-id --name-only -r A C)
新建檔案其內容為某版本檔案的內容
https://stackoverflow.com/questions/888414/git-checkout-older-revision-of-a-file-under-a-new-name
$ git show HEAD^:main.cpp > old_main.cpp
ex.
$ git show commit_id:test.php > test2.php
無法開新分支
$ git checkout -b test
fatal: Unable to write new index file
https://stackoverflow.com/questions/16064513/git-fatal-unable-to-write-new-index-file
原因:主機硬碟滿了,刪不必要的檔案
git diff 的tab使用4個空白
https://stackoverflow.com/questions/10581093/setting-tabwidth-to-4-in-git-show-git-diff
$ git config --global core.pager 'less -x1,5'
統計所有開發者某段時間內commit數量
https://stackoverflow.com/questions/9839083/git-number-of-commits-per-author-on-all-branches
$ git shortlog -s -n --all --no-merges --since="10 Feb 2019" --before="01 Sept 2099"
git pull報錯 There are no candidates for merging among the refs that you just fetched.
https://stackoverflow.com/questions/13799108/git-wildcard-refspec-with-no-match-on-remote-when-pulling
$ git pull
There are no candidates for merging among the refs that you just fetched.
Generally this means that you provided a wildcard refspec which had no
matches on the remote end.
原因:硬碟滿了。清硬碟就好
git log -p /path/file 或git show時搜尋忽略大小寫
https://stackoverflow.com/questions/16666009/how-do-i-use-vim-as-git-log-editor
https://serverfault.com/questions/11736/gnu-less-how-can-i-search-while-ignoring-case-sensitivity-without-using-less-i
git log 使用的是 linux 的 less, less 忽略大小寫是 -I 或 -i
所以
$ git config --global core.pager 'less -I'
千萬不要用vim去看,會有亂碼
用正則刪除多個分支
https://medium.com/@rajsek/deleting-multiple-branches-in-git-e07be9f5073c
$ git branch | grep "<pattern>" | xargs git branch -D
revert多個commit
https://stackoverflow.com/a/1470452 How to revert multiple git commits?
A -> B -> C -> D -> HEAD
一次revert D+C+B的commit,回到A
$ git reset --hard A
$ git reset --soft @{1} # (or ORIG_HEAD), which is D
$ git commit
沒有留言:
張貼留言