顯示具有 git 標籤的文章。 顯示所有文章
顯示具有 git 標籤的文章。 顯示所有文章

2025/03/03

Force LF EOL In Git

Windows 以及 MacOS 或 Linux 的換行符號是不太一樣的,Windows 是 CRLF,Linux like 是 LF,在 Windows 使用 git 如果沒有有設定參數,pull 或 commit 時 git 有可能會幫你自己轉換行符號,這樣會導致 git 在你沒有修改任何內容的情況下判定你有更改檔案,為了避免這樣的狀況最好是設定以下的內容:

$ git config core.eol lf
$ git config core.autocrlf input

Reference

github - Force LF eol in git repo and working copy - Stack Overflow

2025/02/27

Git Export Modified Files From Commit

我們會遇到某些案子只能用 ftp deploy,會遇到只需要 upload 你修改內容的檔案這件事情,git 有可以做到這件事的指令,git archieve,該指令讓你指定 commit 後匯出從該 commit 到最後 commit 的修改檔案。

git archive -o changes.zip HEAD $(git diff --name-only 91455ec0^ HEAD)

只要將後面的 commit hash 改成你這波修改的開始就可以將修改的內容包含原始目錄結構匯出成 changes.zip,省去每次都要大量覆蓋的問題,如果你是使用 JetBrains 的軟體,並且使用他的 git 管理介面以及 ftp 功能,你可以在一整排你要上傳的使用 Ctrl + Shift + Alt + x 叫出 ftp 上傳介面,他很聰明的就會上傳你選擇的所有 commit 的修改檔案上去,很方便,我真的覺得 JetBrains 可以考慮把他的 git 管理工具獨立成一套軟體,像是 Sourcetree 那樣可以單獨使用,真的很好用。

2015/03/26

Msysgit 中文修正

etc\git-completion.bash
alias ls='ls --show-control-chars --color=auto'
etc\profile
export LESSCHARSET=utf-8
etc\gitconfig
[gui]
encoding=utf-8

2015/03/19

Set Winmerge As Git Diff Tool

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" -e "$LOCAL" "$REMOTE"

2014/10/02

Git Export By Commit Range

git archive 指令可以將檔案匯出一份乾淨的程式碼,不會有任何的 repo 紀錄,我最常用的壓縮成 zip。

git archive --format zip --ouput "file.zip" master

如果想要及時解出一個乾淨的資料夾,可以使用 tar 的功能。

git archive master | tar -x -C /folderName

如果我們有更新檔案想要傳給客戶,但是每次都把整個資料夾 zip 過去對方要覆蓋的話也很麻煩,有一個方法可以指定範圍來壓縮或者是 tar 乾淨的目錄,假設今天要壓縮的範圍是 。

# 首先使用 git log 指令來查出你要更動的範圍
git log --name-only

# 找到了這次變動需要匯出的檔案,範圍是 78428a 到 c8cd230 指令為
git archive --format zip --ouput "file.zip" 78428a $(git diff --name-only 78428a^..c8cd230)

這樣就可以匯出這個範圍 commit 的檔案了。

2013/07/30

Git Notebook

將 Dropbox 當作 Server

# 以 case/test/ 資料夾為範例
# --bare 是指不建立 .git 目錄,而把裡面的檔案直接放在目前目錄下
# 適合在沒有修改檔案或開發的機器上
~/Dropbox/case/test/git init --bare

本機端設置

/var/www/test $ git init # 初始化 git
/var/www/test $ git config --global user.name "Daivd" # 初始使用者名稱
/var/www/test $ git config --global user.email "chan15tw@gmail.com" # 初始使用者 Email
/var/www/test $ git conifg --global diff.tool vimdiff # 指定 vimdiff 當作檔案差異比較軟體
/var/www/test $ git remote add test ~/Dropbox/case/test/ # 設定遠端目錄
/var/www/test $ git config branch.master.remote test
/var/www/test $ git config branch.master.merge refs/heads/master
/var/www/test $ git config push.default current
/var/www/test $ touch 1.txt # 建立一個檔案
/var/www/test $ git add . # 新增檔案到 repo
/var/www/test $ git commit . -m "add new file" # commit 檔案
/var/www/test $ git commit --amend -m "add new file 1.txt" # 修改剛剛的 commit message
/var/www/test $ git push test master # 將剛改變的結果 push 到 test 的 master
/var/www/test $ git pull test master # 將 test server 上的最新結果 pull 下來

其他常用指令

/var/www/test $ git log # 查看歷史資料
/var/www/test $ git status # 查看當前 git 狀況
/var/www/test $ git difftool 1.txt # 比較 1.TXT 跟之前版本的差異
/var/www/test $ gitk 1.txt # 圖形化介面

git add -p

使用 git add -p 時 git dd 會進入互動模式,會逐一秀出你編輯過的內容並且讓你決定要不要加到這次的 commit,執行後下方會有幾個選項

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

我認為比較會用到的有

y - 加入這個 hunk
n - 跳過這個 hunk
q - 離開這次的 stage
a - stage 這次的 hunk 並且 stage 此單一檔案接下來所有的 hunk
d - 跳過這整個檔案的 hunk
g - 會列出這個檔案所有的 hunk 讓你選擇編號前往
/ - 搜尋 hunk
j - 跳過這個 hunk 不儲存, 前往下一個未儲存的 hunk
J - 跳過這個 hunk 不儲存, 前往下一個不管有沒有儲存過的 hunk
k - 跳過這個 hunk 不儲存, 前往上一個未儲存的 hunk
K - 跳過這個 hunk 不儲存, 前往上一個不管有沒有儲存過的 hunk
e - manually edit the current hunk

透過這個方法,可以實現某個檔案我們修改了幾個部分,卻只想先 commit 某些部分的願望。