SyntaxHighlighter

About Me

2015年12月28日 星期一

Git 常用指令

git

整理一下 git 比較常用的一些指令

git

如何使用 git 縮寫

> vim ~/.gitconfig

# 加上下面內容
[alias]
      st = status
      co = checkout
      br = branch
      up = rebase
      ci = commit
      di = diff

git 指令與縮寫後的指令比較

git status   = git st
git checkout = git co
git branch   = git br
git rebase   = git up
git commit   = git ci
git diff     = git di

git 指令(以下都用縮寫)

  1. Status

     git st
    
  2. Branch

     # 顯示目前 local git 的 branch
     git br
    
     # 顯示所有 branch (包含 remote branch))
     git br -a
    
  3. Add

     # add single file
     git add <file_path>
    
     # add all unstage file
     git add .
    
     # 強制新增 ignore file in git
     git add -f [file_path]
    
  4. Commit

     git ci -m "content"
     git ci --amend -c {commit hash}
    
  5. Checkout

     # 切換到任何一個 branch 或是任何一點 commit hash 上
     git co <branch_name>
     git co <commit_hash_number>
    
     # 在現在的 branch 上面新開一個 branch 
     git co -b <new_branch_name>
    
     # checkout 一個 remote branch 
     git co -b <local_branch_name> --track origin/<remote_branch_name>
     EX: git co -b feature/123 --track origin/feature/123
    
     # 清除所有 unstage 檔案到上一次的 commit
     git co -- .
    
  6. Diff

     # 查看目前所有 git 的更動內容
     git diff
    
     # 查看某個檔案的變動
     git diff <file_path>
    
     # 查看 git 更動內容並且忽略空白
     git diff --ignore-all-space
    
  7. Merge

     # 一般直接 merge (ex: feature/123 merge to develop)
     # 建議先 pull 在 merge
     git co develop
     git merge feature/123
    
     # 保留 feature branch 上的線
     # 建議先 pull 在 merge
     git co develop
     git merge --no-ff feature/123
    
  8. Pull

     git pull or git pull origin <branch_name>
    
  9. Push

     # 一般的 push 方法
     git push or git push origin <branch_name>
    
     # 與 tag 一起 push
     git push --follow-tags or git push --follow-tags origin <branch_name>
    
     # Delete remote git branch
     git push origin --delete [branch_name]
    
  10. Fetch

     # 同步目前遠端 git 的狀態
     git fetch origin
    
  11. Tag

     # 顯示目前的 tag 狀態
     git tag
    
     # 在某個 commit hash 上面加上 tag
     git tag -a <tag_name> -m "<tag_content>" <commit_hash>
     EX: git tag -a v1.1.0 -m "test1" a9bf65b
    
     # 查詢 tag 的詳細資料
     git show <tag_name>
     EX: git show v1.1.0
    
  12. Reset

     # reset all include unstage
     git reset -q --hard HEAD --
    
     # hard reset 到某次 commit
     ps. 小心使用,中間的所有 commit 都會消失,必須透過 git reflog 去還原之前的 commit
     git reset --hard <commit_hash>
    
  13. Git Flow

    • 認識 git flow: ihower - Git flow 開發流程
    • 安裝 git flow on mac: git-flow cheatsheet
    • 簡單介紹 git flow 種類
      • feature
        • 從 develop 分支出去,常用於開發新的功能,開發完成後就會 merge 回 develop 上
      • hotfix
        • 從 master 分支出去,常用於 stable 版的緊急修復,修復完成後會 merge 回 master 跟 develop 上
      • release
        • 從 develop 分支出去,用於開發版本要釋出成 stable 版,釋出完畢會 merge 到 master 跟 develop 上

    初始化 git flow

    git flow init

    使用 git flow feature 流程

    git flow feature start 123 // 從 develop 建立一個 feature branch 123 出來 git flow feature publish 123 // 將 feature/123 這個 branch push 到 remote git repo 上 git flow feature finish 123 // 將 feature/123 這個 feature branch 結束後 merge 回 develop 上,並且會將這個 feature branch 移除

    使用 git flow release 流程

    git flow release start v1.1.0 // 從 develop 建立一個 release branch v1.1.0 git flow feature publish v1.1.0 // 將 release/v1.1.0 這個 branch push 到 remote git repo 上 git flow feature finish v1.1.0 // 將 release/v1.1.0 這個 release branch 結束後 merge 進 master 與 develop 上並且壓上版本號,然後將這個 release branch 移除

    使用 git flow hotfix 流程

    git flow hotfix start v1.1.0 // 從 master 建立一個 hotfix branch v1.1.0 git flow hotfix finish v1.1.0 // 將 hotfix/v1.1.0 這個 branch 結束後 merge 回 master 與 develop 上並且壓上版本號,然後將這個 hotfix branch 移除

2015年12月22日 星期二

使用 Mac 安裝 gitbook server

安裝 node.js

安裝 node.js

先到 node js 的官方網站依照自己的 OS 下載 node js 安裝檔
https://nodejs.org/en/

確認 node js 是否安裝

node -v    
npm -v

安裝 gitbook cli

npm install -g gitbook-cli

開啟本機 gitbook 專案

cd [gitbook folder]
# 跳至 gitbook 目錄
gitbook install
# 安裝 gitbook 相關套件
gitbook serve
# 啟動 gitbook server ,預設為 4000 port

2015年12月20日 星期日

CURL 與 Shell Script 相關使用

常用語法

分享一下最近使用 curl 去 access 網站會需要用到的指令

常用語法

1. 使用 curl 直接 access 網站

curl http://xxx.xxx.xxx

2. 透過 curl post data 給網站

curl --data "Account=xxx&Password=xxx" http://xxx.xxx.xxx
// 在這個範例中傳送了 Account 跟 Password 兩個參數

3. 透過 curl 使用 user agent

curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" http://xxx.xxx.xxx
// 此範例為使用 MAC 的 OS 然後還有使用 google chrome 的瀏覽器

4. 透過 curl 去記錄目前所使用的 cookie

1. 取得第一次驗證的 cookie

curl --cookie cookie_file.txt --cookie-jar cookie_file.txt --data "Account=xxx&Password=xxx" http://xxx.xxx.xxx/check.php

// 將驗證資料傳到 check.php 獲得 cookie ,並且將 cookie 存在同層目錄下面的 cookie_file.txt 中

2. 使用已經取得的 cookie file 進行其他的使用

curl --cookie cookie_file.txt http://xxx.xxx.xxx/something.php

shell script 額外使用

1. 使用額外的 config file

  • vim env.conf

      test1="test1"
      test2 = "test2"
      # 宣告會使用到的參數
    
  • vim sourcecode.sh

      source env.conf
    
      # 只要在 source code 中引入這個檔案就可以使用了
    
      echo $test1 # test1
      echo $test2 # test2
    

2. 取得當前執行的目錄

WORKDIR=$(cd "$(dirname "$0")"; pwd)

3. 在 mac 中可以直接雙擊 sh 檔

直接將檔名從 .sh 改成 .command

目前遇到的問題

1. 要傳送兩次 post data 給 check.php 後的 cookie_file 才可以使用

curl --cookie cookie_file.txt --cookie-jar cookie_file.txt --data "Account=xxx&Password=xxx" http://xxx.xxx.xxx/check.php
curl --cookie cookie_file.txt http://xxx.xxx.xxx/something.php

// 這樣會無法在 something.php 中取得到資料

curl --cookie cookie_file.txt --cookie-jar cookie_file.txt --data "Account=xxx&Password=xxx" http://xxx.xxx.xxx/check.php
curl --cookie cookie_file.txt --cookie-jar cookie_file.txt --data "Account=xxx&Password=xxx" http://xxx.xxx.xxx/check.php
curl --cookie cookie_file.txt http://xxx.xxx.xxx/something.php

// 這樣就可以在 something.php 中取得資料

Reference

  1. Linux - shell script 取得當前路徑
  2. 如何雙擊 shell script file
  3. 如何使用 applescript 去呼叫 shell script
  4. 如何使用 curl 進行 session 驗證

2015年12月3日 星期四

Javascript callback function 使用方法

寫法 1

寫 js 也一段時間了
整理一下 js callback funciton 的使用方法

寫法 1

function f1(callback) {
  var data = "123";
  callback && callback(data);
}

function f2() {
  f1(function(result) {
    console.warn("in function");
    console.warn(result);
  });
}

f2();

結果是:

in function
123

寫法 2

function f1(callback) {
  var data = "123";
  if (typeof callback === "function") {
    callback(data);
  }
}

function f2() {
  f1(function(result) {
    console.warn("in function");
    console.warn(result);
  });
}

f2();

結果是:

in function
123

基本上兩者的寫法是一樣的東西
只是寫法 1 稍稍有些簡潔
大家參考一下

AngularJS 事件 $broadcast 與 $on 的使用方式

Broadcast

用途:跨 Controller 傳遞變數

Broadcast

用於事件發送

$scope.$broadcast

On

用於接收事件

$scope.$on

範例

app = angular.module('TestApp', []);

// 發送的 controller
app.controller('ctl', function($scope) {
    var myData = '123';
    $scope.$broadcast('EventName', myData);
});

// 接收的 controller
app.controller('ctl2', funciton($scope) {
    $scope.$on('EventName', function(event, args) {
        console.warn(args); // equals to '123'
    });
});

程式說明

  1. 宣告 app 為 angular 的 model
  2. 宣告兩個 controller 分別為 ctl 跟 ctl2
  3. 然後我希望可以將 ctl 的變數傳到 ctl2 這個 controller 下面
  4. EventName 可以自定成不同的事件名稱