iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 28
0
自我挑戰組

30天教你學會Git系列 第 28

[Day28] 欸!我不想Commit這個部分啊!

  • 分享至 

  • xImage
  •  
  • 嘿!其實你有兩種方法可以只Commit一部分的檔案!
    • 你可以把你不要的先刪掉,Commit完了之後再ctrl+z復原((誤XD
    • 或是你可以使用指令啊!
  • 各位別笑,我一開始在用的時候,真的使用過第一種方法啊哈哈哈
  • 但之後在書上看到了這種方法,真的覺得很棒啊!
$ cat Hello.txt
Hello World from master!




I don't want to commit these messages:
asdwdwoekfmwelkfokmelkf
fwkejneocdeclekmn
wefwejvnweldkcmwl
wefwenweofwel;dfm
wecoiwelwekmfcew
wncwkjnwoeikweofkmwelewnfowef
  • 接下來我們使用git add -p Hello.txt ,看會發生什麼!
$ git add -p Hello.txt
diff --git a/Hello.txt b/Hello.txt
index e40c200..bcd1f91 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,12 @@
 Hello World from master!
+
+
+
+
+I don't want to commit these messages:
+asdwdwoekfmwelkfokmelkf
+fwkejneocdeclekmn
+wefwejvnweldkcmwl
+wefwenweofwel;dfm
+wecoiwelwekmfcew
+wncwkjnwoeikweofkmwelewnfowef
Stage this hunk [y,n,q,a,d,e,?]?

  • 這裡如果選擇Y,你就是把全部加進去,我只想要一部分,所以我選擇了E
# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1 +1,12 @@
 Hello World from master!
+
+
+
+
+I don't want to commit these messages:
+asdwdwoekfmwelkfokmelkf
+fwkejneocdeclekmn
+wefwejvnweldkcmwl
+wefwenweofwel;dfm
+wecoiwelwekmfcew
+wncwkjnwoeikweofkmwelewnfowef
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again.  If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.
  • 把你不想要的東西,刪除,所以會變成下面這個樣子!
# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1 +1,12 @@
 Hello World from master!
+
+
+
+
+I don't want to commit these messages:
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again.  If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.
  • 我們來看一下git status!
$ git status
On branch master
Your branch is ahead of 'origin/master' by 10 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   Hello.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   Hello.txt

  • 上面那個部分的Hello.txt是你剛剛調整過的檔案,裡面只有兩句話,並且是已經可以直接執行git commit的!

  • 下面那個部分是還在你的工作目錄的檔案,也就是你原始的檔案(包含你不想要的東西)

  • 這時候只要Commit就可以只Commit部分內容囉~~


上一篇
[Day27] 今天也來說一個很重要的狀況啊!
下一篇
[Day29]今天想來分享一些網路上的學習資源~以及學習歷程
系列文
30天教你學會Git30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言