2014-07-18

git 指令小記

* git add
# git add . # add to index only files created or modified and not those deleted
# git add -u # add to index only files modified or deleted and not those created
# git add -A # do both operation at once, add to index all files

* undo delete
1. 用 git reflog 找出 sha1 碼
2. 使用指令
git branch branchName <SHA1>

* 重新命名 local branch
git branch  -m  <原來名>    <新名>

* 刪除遠端 branch
git push remote :遠端branch名

* 在 git repository 中切換 main branch
git symbolic-ref HEAD refs/heads/BRANCH_NAME
註: 等同 local 的 git checkout BRANCH_NAME

* 如果早已把 file 加入 track, 但現在不想讓改變一直被 track, 例如 config file. 則可以下以下命令
git update-index --assume-unchanged FILENAME
未來, 又想它被 track 可以用以下命令回復

git update-index --no-assume-unchanged FILENAME

* git push 後造成 objects 目錄下的權限問題
當有人上傳新版code, 則會在objects下產生其自有的目錄, 權限是user:group, 其中 group 只有 read 權限,
故必需要在建立 git repository 時先就要設 group share 或已經建立後, 可再下命令改 git config
a. 建立時
# git init --bare --shared=group my-repo.git
 

b. 建立後修補方法
$ git config core.sharedRepository group

 

c.建立步驟參考
# mkdir -p /usr/local/share/git/stuff.git
# cd /usr/local/share/git/stuff.git
# chgrp gitusers . # make sure all users that need access are in the group
# chmod 0660 .
# chmod g+s .     # setguid bit
# git init --bare --shared=0660 .

or
# git init --shared=0644

參考文件: http://chirp.syxyz.net/2012/02/sharing-a-git-repository-over-ssh/


* git 移除已加入 git repository 的檔案
git rm --cached FILE_NAME

use -r to recursively remove files from cache,
git rm --cached -r FILE_NAME

* git 放棄 merge
git reset --merge


===================================
[rebase]

f you are not sharing develop branch with anybody, then I would just rebase it every time master updated, that way you will not have merge commits all over your history once you will merge develop back into master. Workflow in this case would be as follows:

> git clone git://<remote_repo_path>/ <local_repo>
> cd <local_repo>
> git checkout -b develop
....do a lot of work on develop
....do all the commits
> git pull origin master
> git rebase master develop
Above steps will ensure that your develop branch will be always on top of the latest changes from the master branch. Once you are done with develop branch and it's rebased to the latest changes on master you can just merge it back:

> git checkout -b master
> git merge develop
> git branch -d develop

========================
檢查兩個 commit 的不同檔案
git diff --name-status 65813002de..9bafa9b3f9




* 當你在windows 及 linux 之間編輯檔案時, 可能會因 windows 換行符號與 linux 不同而讓 git 誤以為檔案有所改變. 這時候你需要一個聰明辨別 windows/linux 的 git.很巧的是 git 的確有提供這個功能, 指令如下:

$git config --global core.autocrlf true
這樣子, 當你再下 git status 時, 那些惱人的因錯辨換行符號而誤以為是檔案被更改過的問題就一掃而空了.
$git status


* 變更 git commit 時使用旳編輯器(editor) 
例如使用 nano  
$ git config --global core.editor nano 
例如使用 OSX 的文字編輯器(TextEdit)
$ git config --global core.editor 'open -a TextEdit' 

*將舊 log 裡的某個檔案拿出來儲存成其它名稱
git show 897be4c3902:api/old_file_name.py > new_file_name.py
 

========================
當你有兩個遠端 bare repository, 例如 repo  A 跟 repo B 
基本上你都 將 local push 到 repo B 再由 repo B push 到 repo A 
 
[repo A]  <-- [repo B] <-- [local] 
 
可是今天可能是同伴開發時 未經 B 直接就 push 到 A
也就是 repo A 比 repo B 新
[repo A](new)  <-- [someone]  ,      [repo B] (old)    
這時你從 repo B push 上 repo A 時會遇到這個訊息: 
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and merge the remote changes
hint: (e.g. 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 
 
 
若你真的確定 A 比 B 新,你要從 A pull 到 B,你可以在 B 裡下以下指令 
$ git fetch origin +refs/heads/*:refs/heads/* --prune
 
這個動作會將 repo A 的所有 branch update "覆蓋" 你的 repo B
然後你就可以再重新在 local pull repo B, merge repo B
最後再經一般上傳方式上傳
 
 
 
======================== 
當你想產生版本差異的完整檔案(非 patch),且照既有目錄排好,這時你要如何產生?

假設你要取版本 71eba333 ~ 目前 HEAD 位置的所有差異檔案
git archive --format=zip HEAD `git diff 71eba333 HEAD --name-only` > a1.zip

解壓 a1.zip 就是你要的,而且會依照原目錄的結構存放
真的太好用了

延伸題
如上,但你 71eba333 ~ 目前 HEAD有刪除的檔案,這時怎麼辦?可以用 --diff-filter 來過濾掉 D(deleted) 的檔案
git archive --format=zip HEAD `git diff --diff-filter=ACMRTUXB 71eba333 HEAD --name-only` > a2.zip


這個好處是你可以給 patch 檔給不會程式的人,但有 sense 知道如何解壓覆蓋,例如客戶,叫他直接解壓覆蓋即可。什麼語言合適?類interpreter 語言均好用,php, python, perl, bash, ...


參數意義
A Added
C Copied
D Deleted
M Modified
R Renamed
T have their type (mode) changed
U Unmerged
X Unknown
B have had their pairing Broken
* All-or-none
========================
修改最後一個 commit message
git commit --amend
按enter後直接修改即可

修改之前 commit message
只能利用 rebase git rebase -i @˜4 然後利用交談式介面完成 註 @ 即 HEAD

===================================
中止 rebase
git rebase --abort回

回復到未 rebase 狀況
git reset --hard ORIG_HEAD
這不常用,會忘記 head 名稱(不常用是對的)

===================================
樹狀顯示 git log
git log --pretty=format:"%h %s" --graph
git log --graph


===================================


沒有留言:

張貼留言