<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Reindeer Note on Medium]]></title>
        <description><![CDATA[Stories by Reindeer Note on Medium]]></description>
        <link>https://medium.com/@pinky510100?source=rss-9a3d40aaf730------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*5PS8z6HGoArMAEjIA-VgKQ.jpeg</url>
            <title>Stories by Reindeer Note on Medium</title>
            <link>https://medium.com/@pinky510100?source=rss-9a3d40aaf730------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 00:53:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pinky510100/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Ruby][Kata] — Method: digits, permutation]]></title>
            <link>https://medium.com/@pinky510100/ruby-kata-method-digits-permutation-d4a2d9596cfb?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4a2d9596cfb</guid>
            <category><![CDATA[kata]]></category>
            <category><![CDATA[digit]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[permutations]]></category>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Wed, 22 Apr 2020 03:12:16 GMT</pubDate>
            <atom:updated>2020-04-22T03:12:16.995Z</atom:updated>
            <content:encoded><![CDATA[<h3>[Ruby][Kata] — Method: digits, permutation</h3><h3>[Kata] Next bigger number with the same digits</h3><h3>Target</h3><p>Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits.<br>給定數字 N ，寫一個方法使用數字 N 中的數字們排列組合，回傳比數字 N 大的下一個數字。</p><h3>Example</h3><pre>12 ==&gt; 21<br>513 ==&gt; 531<br>2017 ==&gt; 2071</pre><p>If the digits can’t be rearranged to form a bigger number, return -1 or nil:<br>如果沒有下一個比較大的數字，回傳 -1 或 nil</p><pre>9 ==&gt; -1<br>111 ==&gt; -1<br>531 ==&gt; -1</pre><p>解法：將數字 N 轉成陣列使用 split, permutation, map 製造出數字 N 排列組合的結果，使用 uniq 篩掉重複的，再用 sort 排序，使用 index 找數字 N 在陣列中的排序，回傳下一個數字。<br>若沒有下一個數字，回傳 -1 。</p><p>以下的解法都沒出錯，但不論哪種解法，在 codewar 結果都是 time out 。</p><p>方法一：</p><pre>def next_bigger(n)<br>    new_arr = []<br>    n.to_s.split(&#39;&#39;).permutation.map { |arr| new_arr &lt;&lt; arr.join }<br>    new_arr = new_arr.uniq.sort<br>    n = new_arr.index(n.to_s)<br>    if n &lt; (new_arr.length - 1)<br>        res = new_arr[n+1]<br>    else<br>        res = -1<br>    end<br>    return res.to_i<br>end</pre><pre>p next_bigger(12) #21<br>p next_bigger(513) #531<br>p next_bigger(144) #414</pre><p>方法二：</p><pre>def next_bigger(n)<br>    new_arr = []<br>    n.digits.permutation.map { |arr| new_arr &lt;&lt; arr.join }<br>    new_arr = new_arr.uniq.sort<br>    n = new_arr.index(n.to_s)<br>    if n &lt; (new_arr.length - 1)<br>        res = new_arr[n+1]<br>    else<br>        res = -1<br>    end<br>    return res.to_i<br>end</pre><pre>p next_bigger(12) #21<br>p next_bigger(513) #531<br>p next_bigger(144) #414</pre><p>方法三：</p><pre>def next_bigger(n)<br>    new_arr = []<br>    n.digits.permutation.map { |arr| new_arr &lt;&lt; arr.join }<br>    new_arr = new_arr.uniq.sort<br>    n = new_arr.index(n.to_s)<br>    n &lt; (new_arr.length - 1) ? new_arr[n+1].to_i : -1<br>end</pre><p>如果有比較好的解法，歡迎分享。</p><h3>Ruby method: digits (Integer)</h3><p>將給定的數字，預設以 10 為基數，回傳整數的陣列。<br>這個方法的說明在英翻後，還是沒有很好理解。<br>根據手冊的範例，這個方法我理解為若指定基數，會依序回傳餘數及最後的商。</p><p>digits → array<br>預設 10 為基數，給定的數字 123 會先除以 10 ，並將餘數 3 裝在陣列中，再將商 12 繼續除以 10 ，並回傳餘數 2 ，此時商為 1 ，再除以 10 就會變成浮點數，所以就不再進行除法運算，直接將最後的商也裝入陣列回傳。</p><pre>(123).digits<br># [3, 2, 1]<br># 123 / 10 = 12 ... 3<br># 12 / 10 = 1 ... 2</pre><p>digits(base) → array<br>也可以指定基數，將給定的數字除以指定基數，依序回傳餘數及最後的商。</p><pre>(123).digits(2)<br># [1, 1, 0, 1, 1, 1, 1]<br># 123 / 2 = 61 ... 1<br># 61 / 2 = 30 ... 1<br># 30 / 2 = 15 ... 0<br># 15 / 2 = 7 ... 1<br># 7 / 2 = 3 ... 1<br># 3 / 2 = 1 ... 1</pre><h3>Ruby method: permutation (Array)</h3><p>將給定的陣列排列組合後回傳。<br>permutation {|p| block} → ary<br>permutation → Enumerator<br>給定的陣列 [1, 2, 3] 經排列組合後的結果裝在陣列中回傳，要搭配 to_a ：</p><pre>a = [1, 2, 3]<br>a.permutation<br># &lt;Enumerator: [1, 2, 3]:permutation&gt;<br>a.permutation.to_a<br># [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]</pre><p>與方才學過的 digits 還有之前學的 map 搭配使用：</p><pre>b = 12<br>b.digits<br># [2, 1]<br>b.digits.permutation<br># &lt;Enumerator: [2, 1]:permutation&gt;<br>b.digits.permutation.to_a<br># [[2, 1], [1, 2]]<br>b.digits.permutation.map { |x| x &lt;&lt; 3 }<br># [[2, 1, 3], [1, 2, 3]]</pre><p>permutation(n) {|p| block} → ary<br>permutation(n) → Enumerator<br>也可以指定回傳的陣列長度</p><pre>c = [1, 2]<br>c.permutation(1)<br># &lt;Enumerator: [1, 2]:permutation(1)&gt;<br>c.permutation(1).to_a<br># [[1], [2]]</pre><pre>d = [1, 2, 3]<br># [1, 2, 3]<br>d.permutation(1).to_a<br># [[1], [2], [3]]<br>d.permutation(2).to_a<br># [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4a2d9596cfb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Ruby][Kata] — Method: grep, grep_v & Enumerable]]></title>
            <link>https://medium.com/@pinky510100/ruby-kata-method-grep-grep-v-enumerable-38bd592d1062?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/38bd592d1062</guid>
            <category><![CDATA[grep]]></category>
            <category><![CDATA[enumerable]]></category>
            <category><![CDATA[kata]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Mon, 20 Apr 2020 13:56:57 GMT</pubDate>
            <atom:updated>2020-04-20T13:56:57.142Z</atom:updated>
            <content:encoded><![CDATA[<h3>[Ruby][Kata] — Method: grep, grep_v &amp; Enumerable</h3><h3>[Kata] Delete occurrences of an element if it occurs more than n times</h3><h3>Task</h3><p>Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].</p><h3>Example</h3><pre>delete_nth ([1,1,1,1],2)<br># return [1,1]<br>delete_nth ([20,37,20,21],1)<br># return [20,37,21]</pre><p>題目：給定一組數字陣列及一個數字 N ，寫一個方法讓新陣列中的每個數字出現的次數小於 N 。</p><p>在方法定義變數 i &amp; new_order ，使用迴圈搭配 if 條件判斷式：</p><pre>def delete_nth(order, max_e)<br>  i = 0<br>  new_order = []<br>  while i &lt; order.length<br>    new_order &lt;&lt; order[i] if new_order.grep(order[i]).count &lt; max_e<br>    i += 1<br>  end<br>  return new_order<br>end</pre><pre>p delete_nth([20,37,20,21], 1)<br># [20, 37, 20]<br>p delete_nth([1,1,3,3,7,2,2,2,2], 3)<br># [1, 1, 3, 3, 7, 2, 2, 2]<br>p delete_nth([1,3,3,1,2,2,2,2,3,1,3,7,2,3,2,2,2], 4)<br># [1, 3, 3, 1, 2, 2, 2, 2, 3, 1, 3, 7]</pre><p>第二組改用 map 取陣列中的每個數值與新的陣列比對：</p><pre>def delete_nth(order, max_e)<br>    new_order = []<br>    order.map { |i| new_order.push(i) if new_order.grep(i).count &lt; max_e }<br>    return new_order<br>end</pre><pre>p delete_nth([20,37,20,21], 1)<br># [20, 37,21]<br>p delete_nth([1,1,3,3,7,2,2,2,2], 3)<br># [1, 1, 3, 3, 7, 2, 2, 2]<br>p delete_nth([1,3,3,1,2,2,2,2,3,1,3,7,2,3,2,2,2], 4)<br># [1, 3, 3, 1, 2, 2, 2, 2, 3, 1, 3, 7]</pre><h3>Ruby method: grep</h3><p>grep 可以回傳等於指定條件 (pattern)的陣列。<br>grep(pattern) → array<br>grep(pattern) { |obj| block } → array</p><p>換句話說，grep 可以在給定的內容中，比對有沒有符合條件的，找到的話，就會將符合條件的項目放在陣列中回傳：</p><p>在範圍 1~10 尋找符合條件(2~3)</p><pre>(1..10).grep 2..3<br># [2, 3]</pre><p>也可以給一個陣列，尋找符合條件(2~3)</p><pre>[2,3,4,5,6].grep 2..3<br># [2, 3]</pre><p>也能夠搭配 block 使用，尋找符合條件(2~3)，並讓每個結果都乘以二</p><pre>[2,3,4,5,6].grep(2..3) { |i| i * 2 }<br># [4, 6]</pre><h3>Ruby method: grep_v</h3><p>grep_v 與 grep 相反，會回傳不等於指定條件 (pattern)的陣列。<br>grep_v(pattern) → array<br>grep_v(pattern) { |obj| block } → array</p><p>在範圍 1~10 尋找不符合條件(2~3)</p><pre>(1..10).grep_v 2..3 <br># [1, 4, 5, 6, 7, 8, 9, 10]</pre><p>也可以給一個陣列，尋找不符合條件(2~3)</p><pre>[2,3,4,5,6].grep_v 2..3<br># [4, 5, 6]</pre><p>也能夠搭配 block 使用，尋找不符合條件(2~3)，並讓每個結果都乘以二</p><pre>[2,3,4,5,6].grep_v(2..3) { |i| i * 2 }<br># [8, 10, 12]</pre><h3>Enumerable Module</h3><p>Enumerable 可說是一堆遞迴方法的集合體，也是 Ruby 的一個 Module ， Enumerable 中有很多好用的方法，像是 map 、 reduce。</p><p>整體來說， Enumerable Module 方法有三大種類：</p><ul><li>Traversal methods</li><li>Searching methods</li><li>Sorting methods</li></ul><h3>使用 Enumerable 時需要 block：</h3><pre>[1,2,3].map { |n| n * n } # [1, 4, 9]</pre><p>在 Ruby 中， 有幾個重要類別都有引入 Enumerable Module ，像是 Array, Hash &amp; Range</p><pre>Array.included_modules<br># [Enumerable, Kernel]<br>Hash.included_modules<br># [Enumerable, Kernel]<br>Range.included_modules<br># [Enumerable, Kernel]</pre><p>Enumerator 是引入 Enumerable Module 的一個類別：</p><pre>Enumerator.class<br># Class<br>Enumerator.included_modules<br># [Enumerable, Kernel]</pre><p>使用 Enumerable Module 方法時，在沒有給 block 的情況下會出現：</p><pre>[2, 3, 4].map<br>#&lt;Enumerator: [2, 3, 4]:map&gt;</pre><h3>如何在自訂類別中引入 Enumerable Module</h3><p>首先要引入 Enumerable Module：</p><pre>class Users<br>  include Enumerable<br>  def initialize<br>    @user = &quot;John&quot;<br>  end<br>end</pre><pre>p Users.new.map { |user| user.upcase }<br># undefined method `each&#39; for #&lt;Users:0x00007faae1831858 @user=&quot;John&quot;&gt; (NoMethodError)</pre><p>引入 Enumerable Module 的類別需要有 each 方法回傳每一個值：</p><pre>class Users<br>  include Enumerable<br>  def initialize<br>    @user = &quot;John&quot;<br>  end</pre><pre>  def each<br>    yield @user<br>  end<br>end</pre><pre>p Users.new.map { |user| user.upcase }<br># [&quot;JOHN&quot;]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38bd592d1062" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS introduction]]></title>
            <link>https://medium.com/@pinky510100/css-introduction-3209a6488ca9?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/3209a6488ca9</guid>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Tue, 17 Mar 2020 04:20:22 GMT</pubDate>
            <atom:updated>2020-03-17T08:58:43.513Z</atom:updated>
            <content:encoded><![CDATA[<h3>[CSS] Introduction</h3><h3>What is CSS (Cascading Style Sheets,階層式樣式表)?</h3><p>用來描述HTML等標記語言的文件外觀的語言，會描述結構化文件的元素該如何呈現在裝置上。</p><h3>撰寫CSS的風格</h3><ol><li>行內套用inline style</li></ol><ul><li>直接寫在html tag中，但難修改</li></ul><pre>&lt;p style=&quot;color: blue; font-size: 30px;&quot;&gt;Lorem ipsum<br>    dolor sit amet consectetur&lt;/p&gt;</pre><p>2. 整頁套用(Embedding Style Sheets)</p><ul><li>在&lt;head&gt;&lt;/head&gt;之間宣告與套用</li></ul><p>3. 外部套用(External Style Sheets)(Linking Style Sheets)</p><ul><li>另開一個css檔案，並在HTML中用&lt;link&gt;讀取</li><li>冷知識：為何不將CSS寫在HTML後就好呢？分那麼多檔案管理不會不方便嗎？<br>裝置讀取語言時，會按照順序讀取，如果網路速度慢時，網頁會呈現脫皮狀態(單純只有元素，缺乏任何外觀樣式的狀態)。</li></ul><h3>selector選取器-針對特定項目修改樣式</h3><p>可以定義樣式要套用到哪些元素。</p><h3>選取器類型</h3><h4>一般選擇器：</h4><ul><li>元素選擇器 elementname</li><li>Class 選擇器 .classname</li><li>ID 選擇器 #idname</li><li>全域選擇器 *</li><li>屬性選擇器 [attr=value]</li><li>狀態選擇器 a:active, a:link</li></ul><h3>選取器優先權</h3><p><em>明確性越高，優先權越高</em></p><ul><li>不同類型選取器：!important&gt;inline style&gt;#ID&gt;.class&gt;Tag&gt;*&gt;繼承</li><li>相同類型選取器：後方會蓋掉前面的設定</li></ul><p>補充：!important →不常用，某些情況下合理使用，例：Bootstrap；*：全域選取器。</p><h3>class分類命名規則</h3><ol><li>首字不可為數字，第二字開始可使用數字</li></ol><ul><li>O: b3ox</li><li>X: 3box</li></ul><p>2. 選取類別時，開頭要加一個點.</p><p>3. 可以使用中線、底線</p><p>4. 大小寫視為不同</p><p>5. 可以重複</p><h3>CSS reset</h3><p>各家瀏覽器預設的CSS不盡相同，可以reset複寫瀏覽器內建預設的CSS，再用自訂設計的CSS妝點網頁畫面。</p><p>常用的reset工具：</p><ol><li><a href="https://meyerweb.com/eric/tools/css/reset/">https://meyerweb.com/eric/tools/css/reset/</a></li><li><a href="https://necolas.github.io/normalize.css/">https://necolas.github.io/normalize.css/</a></li></ol><h3>元素特性</h3><ol><li>inline 行元素，只算資料或文字長度，佔據的空間就是文字的高度(ex. span, img, a)</li><li>block 區塊元素，佔據的空間是一整個橫排(ex. div, p, hr, h1~h6, header, footer, section, form)</li></ol><h3>display更改元素特性</h3><pre>display: inline<br>display: block<br>display: inline-block<br>display: none          # 移除此元素，且不會佔據空間</pre><pre>visibility: hidden     # 隱藏該元素，會佔據空間</pre><p>支援對應表</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A7LXvasdvLtmAX2O.png" /></figure><h3>CSS Box Model</h3><p>所有的HTML元素可以視為盒子，CSS Box Model就是包裹著HTML元素的盒子模型，由下列區塊組成：</p><ul><li>content 內容物</li><li>padding 內距</li><li>border 邊緣</li><li>margin 外距</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/0*GQVt_B29ysKONl8J.png" /></figure><h4>語法撰寫方式</h4><p>一行設定</p><pre>屬性：上下左右<br>屬性：上下 左右<br>屬性：上 左右 下<br>屬性：上 右 下 左</pre><p>分別設定</p><pre>屬性-top：上<br>屬性-bottom：下<br>屬性-left：左<br>屬性-right：右</pre><ul><li>所有元素的寬度=content寬+padding左/右+border左/右+margin左/右</li><li>所有元素的高度=content高+padding上/下+border上/下+margin上/下</li></ul><p>補充：margin: auto 分配剩餘的空間，一般而言inline只占據自己的空間，所以沒有剩餘的空間</p><h3>Box-sizing</h3><p>可將width/height指定給某個box，不支援margin-box, padding-box。</p><ul><li>content-box（預設，div寬高為內容寬高，padding, border, margin寬高額外加上去）</li><li>border-box (div寬高包含content, paddinding, border寬高)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3209a6488ca9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Ruby] Symbol]]></title>
            <link>https://medium.com/@pinky510100/ruby-symbol-899c8efe3d49?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/899c8efe3d49</guid>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Sun, 15 Mar 2020 08:34:14 GMT</pubDate>
            <atom:updated>2020-03-15T14:10:56.882Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5uhSKwpWcCwXf5_WgLg6w.png" /></figure><p>Symbol是帶有名字的物件，寫法是前方有個冒號，在Ruby可以自由建立Symbol。</p><h3>Symbol在記憶體的位置相同</h3><p>每次要產生Symbol的object id時，都是去找同一個物件；在找string時，則是找不同的兩個物件：</p><ul><li>Symbol會有相同的object id</li><li>String會有不同的object id</li></ul><pre># Use .object_id to get the id (integer) of the object<br>puts :symbol.object_id          #123424<br>puts :symbol.object_id          #123424</pre><pre>puts &quot;string&quot;.object_id         #21938983<br>puts &quot;string&quot;.object_id         #21938829</pre><pre># Use .freeze to fix the &quot;string&quot;<br>puts &quot;string&quot;.freeze.object_id  #21938935<br>puts &quot;string&quot;.freeze.object_id  #21938935</pre><h3>Symbol 效能 &gt; String</h3><p>Symbol效能比字串好，Symbol會直接比較是不是同一個物件；String則是一個一個字母逐一比對。</p><h3>Symbol內容不可變，String內容可變</h3><pre>puts :symbol       # symbol<br>puts &quot;string&quot;      # string</pre><pre>puts :symbol[3]    # b<br>puts &quot;string&quot;[3]   # i</pre><pre>:symbol[3] = &quot;u&quot;   #  returnerror<br>&quot;string&quot;[3] = &quot;u&quot;</pre><h3>查詢Symbol</h3><pre>Symbol.all_symbols   # return symbol table</pre><h3>Symbol無法當作變數</h3><pre>:symbol = &quot;Hello, world&quot;   # return error</pre><p>Symbol具有不可變且效能好的特性，經常作為辨識器使用：</p><ul><li>方法</li><li>變數</li><li>hash key</li></ul><p>Ruby在2.2以後加入Symbol GC(<a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)">Garbage collection</a>)，讓Symbol可以被回收，而不是堆積在記憶體。</p><p>References:</p><ul><li><a href="http://rubylearning.com/">rubylearning.com</a></li><li>為你自己學Ruby on Rails</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=899c8efe3d49" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Push commits to GitHub]]></title>
            <link>https://medium.com/@pinky510100/push-commits-to-github-4e0018dcb0f1?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e0018dcb0f1</guid>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Thu, 12 Mar 2020 07:15:17 GMT</pubDate>
            <atom:updated>2020-03-15T14:23:56.385Z</atom:updated>
            <content:encoded><![CDATA[<h3>Commits to GitHub</h3><blockquote><strong>GitHub, Inc.</strong> is a US-based global company that provides <a href="https://en.wikipedia.org/wiki/Internet_hosting_service">hosting</a> for <a href="https://en.wikipedia.org/wiki/Software_development">software development</a> <a href="https://en.wikipedia.org/wiki/Version_control">version control</a> using <a href="https://en.wikipedia.org/wiki/Git">Git</a>.</blockquote><p>GitHub是透過Git進行程式碼版控的平台，使用Git進行版本控制的專案可以上傳至GitHub，團隊協作時，可以使用GitHub分享及版控專案的程式碼。</p><h4>如何將本地端的專案連結至GitHub?</h4><p>step 1. GitHub新增repository，依照出現的指示執行</p><pre># or creat a new repository on the command line</pre><pre>echo &quot;# new&quot; &gt;&gt; README.md<br>git init<br>git add README.md<br>git commit -m &quot;first commit&quot;</pre><pre>git remote add origin http://github.com/username/repositoryname <br>(# 在遠端的repository建立一個origin)<br>git push -u origin master                                       <br>(# 將本地的master推到origin)</pre><pre># or push an existing repository from the command line</pre><pre>git remote add origin http://github.com/username/repositoryname <br>git push -u origin master</pre><p>step 2. 有兩種可以push到遠端repository的方式，在此使用SSH</p><ul><li>HTTPS</li><li>SSH (使用前需設定公私鑰，設定流程如下)</li></ul><pre># 產生ssh key<br>ssh-keygen<br>Generating public/private rsa key pair.</pre><pre># 建立key的目錄及檔名，此時可以命名<br>Enter file in which to save the key (/Users/username/.ssh/id_rsa):                    <br>Created directory &#39;/Users/username/.ssh&#39;.</pre><pre># 為key加上密碼<br>Enter passphrase (empty for no passphrase):<br>Enter same passphrase again:<br>Your identification has been saved in /Users/username/.ssh/id_rsa.<br>Your public key has been saved in /Users/username/.ssh/id_rsa.pub.</pre><pre># 方才產生的公鑰(id_rsa.pub)、私鑰(id_rsa)<br>cd /Users/username/.ssh<br>ls -al<br>total 16<br>drwx------   4 username  staff   128 12 Mar 11:24 .<br>drwxr-xr-x+ 39 username  staff  1248 12 Mar 11:24 ..<br>-rw-------   1 username  staff  1856 12 Mar 11:24 id_rsa<br>-rw-r--r--   1 username  staff   418 12 Mar 11:24 id_rsa.pub</pre><pre># 印出id_rsa.pub內容<br>cat id_rsa.pub</pre><p>step 3. 到GitHub上該專案設定公鑰</p><ul><li>設定deploy keys</li><li>勾選可寫入</li></ul><p>step 4. git push將本地端專案push至遠端</p><pre># 在遠端的Hello-world形成origin<br>git remote add origin git@github.com:lindawu100/Hello-world.git   </pre><pre># master推到遠端的origin，並在遠端形成一個master<br>git push -u origin master</pre><pre>----------------------------------------------------------<br>備註：</pre><pre># 上述的原型(master推到origin，並在遠端形成一個master)<br>git push origin master:master                                     </pre><pre># 也可以在遠端形成不同名稱的branch，master推到origin，並在遠端形成一個cat<br>git push origin master:cat   <br>                                     <br># 刪除cat<br>git push origin :cat                                              </pre><pre>* 本地端：遠端</pre><p>如果遠端專案狀態比較新，可以先將專案pull至本地端，有下列兩種方式：</p><ol><li>git pull ( = git fetch + git merge )</li></ol><pre># 去origin遠端節點，抓master的內容，並且在電腦上建立origin/master分支，並合併分支<br>git pull origin master</pre><p>2. git fetch + git merge</p><pre># 去origin遠端節點，抓master的內容，並且在電腦上建立origin/master分支，再合併分支</pre><pre>git fetch origin master<br>git merge</pre><p>如果遠端專案狀態比較新，直接push會失敗，可以使用下列：</p><ul><li>git push -f：硬推本地端的專案，並且覆蓋遠端的專(不建議使用此指令)。</li></ul><p>其他備註：</p><ul><li>git remote -v：顯示目前所在目錄的遠端資訊。</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e0018dcb0f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Basics & Common Commands]]></title>
            <link>https://medium.com/@pinky510100/git-basics-common-commands-23782ad8968b?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/23782ad8968b</guid>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Tue, 10 Mar 2020 14:39:22 GMT</pubDate>
            <atom:updated>2020-03-10T14:58:22.616Z</atom:updated>
            <content:encoded><![CDATA[<p>Git為分散型版控工具，不需要伺服器端就可以運作。</p><p>只記錄版本間不一樣的地方，例如：index.html兩次修改後的增加BBB、增加CCC。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cAkoXOLSFj53Z-jBFJw6Cg.png" /></figure><p>Git可分為三大區塊：Working Directory 工作目錄、Staging Area 暫存區域、Repository 儲存庫，Git指令在三大區塊間移動檔案。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y1iuQAWROhkl6tKBk1W5Ag.png" /></figure><p>touch：建立檔案</p><p>git add：從工作目錄移到暫存區域</p><p>git commit：從暫存區域移到本地儲存庫</p><ul><li>對已經存在Repository的檔案，在還沒add的狀況下加-a，git commit -a -m &quot;&lt;context&gt;&quot;，</li></ul><pre>touch index.html                # 建立index.html<br>git add index.html              # index.html移至暫存區<br>git commit -m &quot;add index.html&quot;  # index.html移至儲存庫，並備註add index.html</pre><p>git log：檢視git紀錄</p><ul><li>另外加上--oneline與--graph，可以看到不一樣的格式輸出</li></ul><p>git status：查看目前狀態</p><p>rm; git rm：從工作目錄刪除檔案</p><ul><li>--cached不會從工作目錄刪除檔案，僅讓檔案不受Git管控</li></ul><pre>rm index.html                      # 刪除index.html<br>git add index.html                 # 將此次的變化移至暫存區(刪除index.html)<br>git commit -m &quot;delete index.html&quot;  # 將此次的變化移至儲存庫</pre><pre>---</pre><pre>git rm index.html                  # 刪除index.html，並將此次的變化移至暫存區<br>git commit -m &quot;delete index.html&quot;  # 將此次的變化移至儲存庫</pre><pre>---</pre><pre>git rm index.html --cached         # 讓index.html不再受Git管控</pre><p>mv; git mv：變更檔案名稱</p><pre>mv index.html hello.html           # index.html更名為hello.html</pre><pre>---</pre><pre>git mv index.html hello.html       # index.html更名為hello.html，並將此次的變化移至暫存區</pre><p>修改git紀錄</p><ul><li>git commit --amend -m 修改最後一次commit紀錄</li><li>git reset 拆掉commit，再重新commit</li><li>--mixed, --soft, --hard</li></ul><pre>git commit --amend -m &quot;Hello, world&quot;  # 最後一次commit訊息改成Hello, world</pre><pre>git reset e19f41a      # 回復至e19f41a的狀態<br>git reset e19f41a^     # 回復至e19f41a前一次的狀態<br>git reset e19f41a^^^   # 回復至e19f41a前三次的狀態</pre><pre>假設HEAD &amp; master目前在e19f41a，也可改寫成下列：</pre><pre>git reset HEAD^<br>git reset master^</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Fec-yuMrNlOb5yYPrf0Pg.png" /></figure><pre>git reset --mixed   # commit拆出的檔案會移至工作目錄<br>git reset --soft    # commit拆出的檔案會移至暫存區<br>git reset --hard    # commit拆出的檔案會丟掉</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*8fllAcXdP9SkBFuHY8zA-w.png" /></figure><p>git checkout：切換分支或復原檔案</p><pre>git checkout mouse        # 切換至mouse分支</pre><pre>---</pre><pre>git checkout index.html   # 復原index.html</pre><pre>---</pre><pre>git checkout HEAD~  # 回復至一個版本以前的檔案，覆蓋工作目錄的檔案，病更新暫存區<br>git checkout HEAD~2 # 回復至兩個版本以前的檔案，覆蓋工作目錄的檔案，病更新暫存區</pre><p>git blame：查看檔案每一行code歷史紀錄</p><ul><li>-L：只顯示指定行數的紀錄</li></ul><pre>git blame index.html          # 顯示index.html每一行紀錄</pre><pre>---</pre><pre>git blame -L 3,8 index.html   # 顯示index.html 3~8行紀錄</pre><p>git reflog; git log -g：查看HEAD移動紀錄</p><pre>git reflog  # 查看HEAD移動紀錄<br>git log -g  # 查看HEAD移動紀錄</pre><p>git branch：新增分支</p><ul><li>-m 修改分支名稱</li><li>-d 刪除分支</li></ul><pre>git branch cat          # 新增cat branch<br>git branch -m cat dog   # cat更名為dog</pre><pre>---</pre><pre>git branch -m hello    # 刪除hello分支</pre><p>git merge：合併分支</p><pre>git checkout master  # 切換至master branch<br>git merge cat        # master合併car branch</pre><p>git rebase：合併分支</p><pre># 有cat, bird兩個分支，都源自於master分支</pre><pre>git rebase bird   # 剪下bird分支，接上cat分支</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23782ad8968b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git 起步走]]></title>
            <link>https://medium.com/@pinky510100/git-%E8%B5%B7%E6%AD%A5%E8%B5%B0-62fb5a475187?source=rss-9a3d40aaf730------2</link>
            <guid isPermaLink="false">https://medium.com/p/62fb5a475187</guid>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Reindeer Note]]></dc:creator>
            <pubDate>Fri, 06 Mar 2020 05:48:28 GMT</pubDate>
            <atom:updated>2020-03-12T07:19:30.031Z</atom:updated>
            <content:encoded><![CDATA[<h3>安裝Git</h3><p>以medium開始紀錄程式學習歷程，使用工具為MacBook Pro。</p><p>打開terminal，輸入下列指令安裝Homebrew：</p><pre>/bin/bash -c &quot;$(curl -fsSL <br>https://raw.githubusercontent.com/Homebrew/install/master/install.sh)&quot;</pre><p>Link：<a href="https://brew.sh/index_zh-tw.html">https://brew.sh/index_zh-tw.html</a></p><p>輸入下列指令，使用Homebrew安裝git：</p><pre>brew install git</pre><p>確認git安裝路徑及版本：</p><pre>which git      # /usr/local/bin/git<br>git --version  # git version 2.25.0</pre><ul><li>/usr/local對Finder預設隱藏，想要開啟的話，打開一個Finder，按下command+shift+G，在彈出的目錄輸入/usr/local就可以找到</li></ul><h3>開始使用</h3><p>terminal先進入想要版控的目錄，宣告要進行版控：</p><pre>git init</pre><h4>查詢當下所在的目錄內容，可使用下列三種方式：</h4><ol><li>ls：顯示當下的目錄及檔案</li><li>ls -a：顯示當下的目錄的所有目錄及檔案，包括隐藏的.和…等</li><li>ls -al：顯示當下的目錄的所有目錄及檔案，包括隐藏的.和…等，並顯示詳細訊息，包括大小、屬性，建立時間</li></ol><pre>ls  # hello.rb</pre><pre>ls -a # .		..		.git		hello.rb</pre><pre>ls -al<br># drwxr-xr-x   4 username  staff  4 Mar 12:40 .<br># drwxr-xr-x@  8 username  staff  4 Mar 12:36 ..<br># drwxr-xr-x  10 username  staff  4 Mar 14:02 .git<br># -rw-r--r--@  1 username  staff  4 Mar 17:19 hello.rb</pre><p>directory, d代表目錄; — 代表檔案，後面的九個字母要三個三個分別看</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3ZruH5j2Upi_Qckpebor1g.png" /></figure><ul><li>read, r 可讀</li><li>write, w 可寫入</li><li>execute, x 可執行</li><li>“-” 無法使用該功能</li></ul><h3>如何修改權限 chmod (change mode)</h3><h4>方法一：二進位</h4><pre>ls -al # -rw-r--r--    index.html</pre><pre>chmod 700 index.html</pre><pre>ls -al # -rwx------    index.html</pre><h4>方法二：直接指定user, group or others</h4><pre>chmod g+x index.html<br>ls -al # -rwx--x---   index.html</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62fb5a475187" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>