[英]How to revert some of the changes made by a newer commit?
我想撤消使用最新提交在一個文件中所做的部分但不是全部更改。 最好將部分還原的文件放置在我的工作目錄中。
我對checkout --patch filename
感到困惑,最好的方法是什么?
正如您自己說的那樣, git checkout --patch
是執行此操作的最佳方法。 您可能會感到困惑,因為您需要指定要從中檢出的樹狀對象(提交或分支)。 在您的特定示例中,您需要git checkout --patch HEAD~1
git checkout --patch
行為與git add --patch
非常相似,除了它從另一棵樹添加到工作目錄而不是將工作目錄添加到索引。
鍵入要檢出的樹和路徑后,git會顯示文件與要檢出的樹之間的逐塊差異。 然后,您可以指定是要應用塊,拒絕塊還是將塊拆分成較小的塊。 按?
在此交互式工具中獲取您可以做什么的完整列表。
它看起來應該像這樣:
$ # git checkout --patch <tree> <path>
$ git checkout --patch 9e881cb conf/nginx.conf
diff --git b/conf/nginx.conf a/conf/nginx.conf
index ff0454a..a3c3ed7 100644
--- b/conf/nginx.conf
+++ a/conf/nginx.conf
@@ -1,12 +1,8 @@
server {
server_name strikeskids.com;
+ root /www/strikeskids;
location / {
- root /www/strikeskids/jekyll;
try_files $uri $uri.html $uri/index.html =404;
}
-
- location /up/ {
- root /www/strikeskids;
- }
}
Apply this hunk to index and worktree [y,n,q,a,d,/,s,e,?]? s <=== split
chunk into smaller pieces
Split into 3 hunks.
@@ -1,4 +1,5 @@
server {
server_name strikeskids.com;
+ root /www/strikeskids;
location / {
Apply this hunk to index and worktree [y,n,q,a,d,/,j,J,g,e,?]? y <=== include
this chunk in the checkout
@@ -3,5 +4,4 @@
location / {
- root /www/strikeskids/jekyll;
try_files $uri $uri.html $uri/index.html =404;
}
Apply this hunk to index and worktree [y,n,q,a,d,/,K,j,J,g,e,?]? n <=== do not
include this chunk in the checkout
@@ -6,7 +6,3 @@
try_files $uri $uri.html $uri/index.html =404;
}
-
- location /up/ {
- root /www/strikeskids;
- }
}
Apply this hunk to index and worktree [y,n,q,a,d,/,K,g,e,?]? q <== finish now,
including the chunks I've specified but not any others
git checkout HEAD~1 path/to/file
為您提供git checkout HEAD~1 path/to/file
先前版本。 然后,您可以撤消要保留的更改(因此,使文件看起來像您想要的一樣),然后照常提交更改。
如果允許您重寫歷史記錄(例如,如果尚未將提交推送到遠程存儲庫):
# undo your commit
git reset HEAD~
# now remove the changes you didn't want in your last commit
# and commit again
git commit
有兩種方法可以實現此目的:
1)由於您已經提交了代碼,請使用
git reset --soft HEAD ^
這將取消提交代碼,現在您可以根據需要修改文件。
2)使用像Git這樣的版本控制系統,您不必擔心提交,因為它基本上具有您的全部歷史。 您可以繼續進行此提交的更改,以基本上刪除您錯誤提交的代碼。
但是僅在必須修改少量代碼的情況下才選擇選項2,否則請選擇選項1。
也許您會對github博客上有關如何使用Git撤消(幾乎)撤消任何內容的非常好的文章感興趣……
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.