Latest Posts

Git Stash 历险记 2012-12-31

我第一次用这个命令时被坑过,经过是这样的:我发现有一个类是多余的,想删掉它又担心以后需要查看它的代码,想保存它但又不想增加一个脏的提交。这时我想到了git stash,于是那一天我执行了下这个命令就回去睡觉了。 第二天我继续在这个目录里工作,coding了半天才发现前一天的修改都不见了,暂存区里也没有任何昨天修改的纪录。是我忘了保存了吗?

相信git老手们早就看出问题所在了,修改消失了并不是因为我忘了保存,而是git stash在保存完当前工作目录和暂存区以后,会用HEAD重置这两者。因为我昨天的修改没有提交,HEAD指向的是前天的版本,所以stash以后工作目录和暂存区就会被前天的的版本所重置。

正确的做法应该是在git stash后再执行git stash apply,当前的工作目录就恢复回来了。

git stash apply相当于利用过去贮藏(stashed)的工作目录快照,恢复当前的工作目录。如果工作目录在贮藏之后发生了变化,恢复时就会产生冲突(conflict),这种情况下git stash apply会对工作目录进行merge操作。

和merge一样,git stash apply之前要保持当前目录是干净的(没有未提交的改变),否则会保错:

error: Your local changes to the following files would be overwritten by merge: Please, commit your changes or stash them before you can merge.

git stash apply只能恢复工作目录,如果想把暂存区也按照贮藏时的暂存区恢复的话,可以加上--index,如果暂存区恢复时发生冲突了会怎么办呢?嘿嘿,它会直接报错不允许你这么做:

Conflicts in index. Try without --index.

My First Bioinformatics Web Application 2012-12-23

This tool is called CistromeFinder. It is made to help biologists to check binding sites around a given gene. Following is an introduction from the blog of my adviser, Xiaole Shirley Liu.

We have been processing all the publicly available ChIP-seq and DNase-seq data in human and mouse. Curious about whether factor X has binding sites near gene Y? CistromeFinder can help you evaluate specific ChIP-seq/DNase-seq dataset and give you the answer to this question.

This is my first Web application. As I was only familar with Python at that time, I chose Pyramid as web framework, SQLAlchemy as ORM and jQuery UI as front-end framework. As time passed, I found the jQuery UI codes really suffering to maintain, so I discarded all jQuery UI codes and rewrite UI with a much more friendly framework, which is called HTML KickStart.

Two things I've learned:

  • For UI, use a high-level UI framework. You will be frustrated to make the UI work with jQueryUI, unless you're very familiar with javascript as well as jQuery. Try HTML KickStart or Bootstrap.

  • For web framework, I'm tired of Pyramid + SQLAlchemy. This feeling becomes stronger after I tried Rails. At first, I chose Pyramid and SQLAlchemy for loose coupling, which is claimed as a feature superior to Django. However, it's not as good as it might sound. One of the comments is forthright: The loose coupling means that it's going to just take longer to get to a mature point. Even worse is that the documents of both Pyramid and SQLAlchemy are abstract and there're not many good tutorials for them. If I could rewrite the back-end part, I might try Rails or Flask.

The submission of the paper is still on the way. Hope it could be finished before January of next year.

Merry Christmas and Happy New Year! :-)

从Mercurial到Git 2012-12-21

一年前,我开始用Mercurial做版本控制,一直觉得它易学易用,该有的功能都有,而且常用的命令就只有pull, push, add, commit, update, revert这几个。其间偶尔用过两次Git,都是在命令对照表的帮助下磕磕绊绊地完成的,每次我都会感觉Git语法的怪异之处,用起来觉得很不舒服。

既然Git这么流行,肯定有它的长处。这周我就尝试学习了一下Git,大致了解了Git的底层机制以及它与Mercurial最大的差异所在。就从我刚转到Git时经常犯的错误开始说起吧。

hg add 与 git add

Mercurial的世界可以分为两个空间:工作目录(workspace)与版本库(repository)。我们在工作目录中写代码,想保存一下当前的快照就使用提交(commit)命令,然后当前的快照被存放在版本库中。在工作目录中,文件有两个状态:要么是跟踪的(tracked),要么没有被跟踪(not tracked)。只要跟踪的文件发生了变化,提交时这些变化会被自动添加到版本库中。新建的文件都是未被跟踪的,如果想要开始跟踪这个文件,只需要hg add 文件名即可。

Git却不是这样,它在工作目录与版本库之间还有着第三个空间:暂存区(staging area)或者叫索引(index)。一个跟踪中的文件如果发生了变化,需要先把这个变化加入到暂存区中,然后才能通过提交到版本库中。这样一来,工作目录里的文件的变化也有了两个状态:暂存(staged)与未暂存(not staged),请注意这里的用词,暂存用来用来形容的是文件的变化而不是文件本身

如果一个文件未被跟踪,那么git add 文件名就可以将它的状态改成跟踪,同时将这一变化放入暂存区中:

git init;
echo "first line" > new_file;
git add new_file;
git commit -m "add new_file"