Pages

May 4, 2012

Git: git stash how to

故事是這樣的,我在github上開了一個repo打算拿來放系統上的設定檔,我把一些設定檔整理好並加了一些README,以防以後痴呆了,忘記設定檔該放在哪裡,就在我準備要commit時,我想到我還有台laptop...

laptop和pc的環境很不一樣,設定檔相對的也會有不少差別,所以我應該要開兩個branch,一個放laptop的設定,一個放pc的設定,生性懶惰使然,我不想重作一次工作,也不想把完成的部份commit到master branch上,於是git stash就派上用場了,看一下man page怎麼說:
$ man git stash
...
DESCRIPTION
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory...
...

以下是我的流程:
# do something...
$ git add . # 將目前的變更加入stage
$ git stash # stage的狀態會被存起來,並將working directory reset
$ git checkout -b newbranch # 產生一個新的branch並checkout
$ git stash pop # 將存起來的stage狀態pop出來

man page裡面提供了另外兩種情境可以參考
情境一
Pulling into a dirty tree
upstream有了更新,但跟你的local changes有conflict,導致無法直接用git pull解決
$ git stash
$ git pull
$ git stash pop

情境二
Interrupted workflow
你老闆要你馬上修正某個問題,但你正在做些偉大的事,一個方法是,先把目前的工作commit到一個臨時的branch上,等到問題解決了,在soft reset回來,用git stash可以大大簡化這步驟
# changing the world...
$ git stash
# fix something...
$ git commit -a -m "Stupid bug fixed."
$ git stash pop
# continue on changing the world...

git stash pop有可能會遇到conflict的狀況,哪天遇到了再回來補充...

Vim: Delete lines that match a pattern

Delete lines that match the pattern
:g/pattern/d
Delete lines that do not match the pattern
:v/pattern/d
or
:g!/pattern/d

Apr 25, 2012

Calculate with command-line tool bc

$ echo 'expr' | bc

Hex to Dec
$ echo 'ibase=16;obase=A;FF' | bc
255

Dec 17, 2011

python: list assign(copy)

>>> a = [1,2,3,4,5]
>>> b = a
>>> b[0] = 10
>>> print a
[10,2,3,4,5]
在python中,list的assign有點類似c的pointer,改變b的值同時也會改變a的值,
要作到真正的copy有兩種方法:

1.
>>> b = a[:]
這個作法雖然簡單,但是在nested的結構中會有問題

2.
>>> import copy
>>> b = copy.copy(a)
copy module中又有copy與deepcopy,兩者的差別要再研究研究

reference:
http://docs.python.org/library/copy.html

Dec 7, 2011

bash wrapping 位置錯誤

在bash環境下,紀錄terminal寬度的變數叫做COLUMNS,可以用
$ echo $COLUMNS

查看目前terminal的寬度(字元數)

bash藉由這個變數來決定哪個位置該wrap,改變terminal大小時,通常這個值也會跟著改變,但有時候就會有靈異現象,造成terminal大小改變,但COLUMNS的值卻沒有改變

bash的一個built-in command shopt,或許可以解決這個問題,shopt中有一個選項checkwinsize,會在每次做完一個指令後檢查terminal的大小,並視情況更新LINES & COLUMNS,可以把他加在 ~/.bashrc 裡面
shopt -s checkwinsize

至於效果如何,需要再觀察看看囉

Reference:
Bash Reference Manual
http://www.gnu.org/software/bash/manual/bashref.html#The-Shopt-Builtin