<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Git on MoeJue&#39;s Blog</title>
    <link>https://en.moejue.cn/zh-hant/tags/git/</link>
    <description>Recent content in Git on MoeJue&#39;s Blog</description>
    <generator>Hugo -- 0.151.2</generator>
    <language>zh-hant</language>
    <lastBuildDate>Mon, 02 Jun 2025 06:59:31 +0000</lastBuildDate>
    <atom:link href="https://en.moejue.cn/zh-hant/tags/git/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>[筆記] Git 常用命令大全（按場景分類）</title>
      <link>https://en.moejue.cn/zh-hant/posts/264/</link>
      <pubDate>Mon, 02 Jun 2025 06:58:42 +0000</pubDate>
      <guid>https://en.moejue.cn/zh-hant/posts/264/</guid>
      <description>&lt;p&gt;適用於日常開發中對 Git 的常見操作。透過分類整理，幫助你快速上手或查閱。&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;-基礎指令&#34;&gt;📦 基礎指令&lt;/h2&gt;
&lt;p&gt;指令&lt;/p&gt;
&lt;p&gt;用途&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git init&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;初始化一個 Git 儲存庫（建立 &lt;code&gt;.git&lt;/code&gt; 資料夾）。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git clone &amp;lt;url&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;將遠端儲存庫複製到本地。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git status&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;查看目前工作目錄狀態（檔案修改、暫存等）。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git add &amp;lt;file&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;將檔案新增到暫存區。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git commit -m &amp;quot;message&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;提交暫存區檔案到本地儲存庫，並新增提交訊息。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git log&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;查看提交歷史。&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;-分支管理&#34;&gt;🌱 分支管理&lt;/h2&gt;
&lt;p&gt;指令&lt;/p&gt;
&lt;p&gt;用途&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git branch&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;查看本地分支列表。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git branch &amp;lt;branch-name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;建立一個新分支。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git checkout &amp;lt;branch-name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;切換到指定分支。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git switch &amp;lt;branch-name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;切換到指定分支（推薦使用的新指令）。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git merge &amp;lt;branch-name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;將指定分支合併到目前分支。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git branch -d &amp;lt;branch-name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;刪除已合併的分支。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git branch -D &amp;lt;branch-name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;強制刪除分支。&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;-遠端儲存庫操作&#34;&gt;🌍 遠端儲存庫操作&lt;/h2&gt;
&lt;p&gt;指令&lt;/p&gt;
&lt;p&gt;用途&lt;/p&gt;</description>
    </item>
    <item>
      <title>[筆記]Git常用命令大全</title>
      <link>https://en.moejue.cn/zh-hant/posts/57/</link>
      <pubDate>Sat, 16 Jun 2018 08:56:21 +0000</pubDate>
      <guid>https://en.moejue.cn/zh-hant/posts/57/</guid>
      <description>&lt;p&gt;繼上一次之後，我抽空整理了一份比較完整的 Git 常用命令清單，並找到了一張非常棒、非常高清的導圖（1759*3162）。&lt;/p&gt;
&lt;p&gt;查看、新增、提交、刪除、找回、重置修改檔案&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git help &amp;lt;command&amp;gt; # 顯示 command 的說明&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git show # 顯示某次提交的內容 git show $id&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git co -- &amp;lt;file&amp;gt; # 捨棄工作區修改&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git co . # 捨棄工作區修改&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git add &amp;lt;file&amp;gt; # 將工作檔案修改提交到本地暫存區&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git add . # 將所有修改過的工作檔案提交暫存區&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git rm &amp;lt;file&amp;gt; # 從版本庫中刪除檔案&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git rm &amp;lt;file&amp;gt; --cached # 從版本庫中刪除檔案，但不刪除檔案&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git reset &amp;lt;file&amp;gt; # 從暫存區恢復到工作檔案&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git reset -- . # 從暫存區恢復到工作檔案&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git reset --hard # 恢復最近一次提交過的狀態，即放棄上次提交後的所有本次修改&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git ci &amp;lt;file&amp;gt; git ci . git ci -a # 將 git add, git rm 和 git ci 等操作都合併在一起執行 git ci -am &amp;quot;some comments&amp;quot;&lt;/code&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Git簡單的使用步驟</title>
      <link>https://en.moejue.cn/zh-hant/posts/64/</link>
      <pubDate>Tue, 03 Apr 2018 12:04:32 +0000</pubDate>
      <guid>https://en.moejue.cn/zh-hant/posts/64/</guid>
      <description>&lt;p&gt;本文不闡述任何概念性知識，僅僅只是做一個筆記，簡單的使用步驟，如遇障礙，請Google一下&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;使用SSH 完成 Git 與 GitHub 的綁定&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;生成 &lt;code&gt;SSH key&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ssh-keygen -t rsa
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;指定 RSA 演算法生成金鑰，之後就會生成兩個檔案，分別為id_rsa和id_rsa.pub，即私鑰id_rsa和公鑰id_rsa.pub。對於這兩個檔案&lt;/p&gt;
&lt;ol start=&#34;2&#34;&gt;
&lt;li&gt;
&lt;p&gt;添加 SSH key
github.com -&amp;gt; Settings -&amp;gt; SSH and GPG -&amp;gt; New SSH key
將公鑰id_rsa.pub的內容貼到Key處的位置（Titles的內容不填寫也沒關係），然後點擊Add SSH key 即可。&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;驗證綁定是否成功&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ssh -T git@github.com
&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;
&lt;li&gt;把本地專案推送到github的命令&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(1) 打開你的目錄&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;cd demo
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(2) 初始化版本庫，用於生成git檔案&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(3) 將所有檔案添加到暫存區&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git add *
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(4) 提交目前工作空間的修改內容&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git commit -m &amp;#34;first commit&amp;#34;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(5) 將儲存庫連接到遠端伺服器&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git remote add origin &amp;lt;server&amp;gt;(就是上面你儲存庫的地址)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(6) 將改動推送到所添加的伺服器上&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git push -u origin master
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;在推送的時候如果出現如下錯誤：&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;warning: redirecting to https://github.com/178146582/dabai.git/
To http://github.com/178146582/dabai.git
 ! [rejected]        master -&amp;gt; master (fetch first)
error: failed to push some refs to &amp;#39;http://github.com/178146582/dabai.git&amp;#39; hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., &amp;#39;git pull ...&amp;#39;) before pushing again.
hint: See the &amp;#39;Note about fast-forwards&amp;#39; in &amp;#39;git push --help&amp;#39; for details.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;查了一下錯誤的原因是github中的README.md檔案不在本地程式碼目錄中。所以我們把上面第六步分成兩步：&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
