2014-05-16 14 views
11

Tôi đang cố gắng tìm ra cách để lưu trữ hai tệp cụ thể trong số nhiều thay đổi không được cam kết.Git: làm thế nào để sử dụng stash -p để stash các tập tin cụ thể?

Câu trả lời rất hứa hẹn này, Stash only one file out of multiple files that have changed with Git?, không hiển thị mức sử dụng và tôi đang gặp sự cố khi sử dụng.

Sau đây không hoạt động và trang người đàn ông không phải là rất hữu ích (nó xuất hiện để nói về đầu ra thiết bị đầu cuối, không thực sự stashing). Tôi muốn stash application.confplugins.sbt và sau đó cam kết mọi thứ khác.

app (master)$ git status 
On branch master 
Your branch is ahead of 'origin/master' by 29 commits. 
    (use "git push" to publish your local commits) 

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

     modified: views/mobile/blog.scala.html 
     modified: views/mobile/slideshow.scala.html 
     modified: ../public/css/mobile/styles.css 

Unmerged paths: 
    (use "git reset HEAD <file>..." to unstage) 
    (use "git add <file>..." to mark resolution) 

     both modified: ../conf/application.conf 

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: ../project/plugins.sbt 

app (master)$ git stash -p ../conf/application.conf ../project/plugins.sbt 

usage: git stash list [<options>] 
    or: git stash show [<stash>] 
    or: git stash drop [-q|--quiet] [<stash>] 
    or: git stash (pop | apply) [--index] [-q|--quiet] [<stash>] 
    or: git stash branch <branchname> [<stash>] 
    or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] 
         [-u|--include-untracked] [-a|--all] [<message>]] 
    or: git stash clear 

Trả lời

18

Sử dụng

git stash --patch 

git sau đó sẽ hiển thị một hộp thoại như sau, cho mỗi hunk trong khả năng của bạn cam kết:

diff --git files over files 
index e69de29..ac4f3b3 100644 
--- a/file.txt 
+++ b/file.txt 
@@ -0,0 +1 @@ 
+you did awesome stuff! 
Stash this hunk [y,n,q,a,d,/,e,?]? 

Một hunk là một diff mạch lạc của dòng như git-diff sản xuất nó. Để chọn một tệp, bạn sẽ phải thêm d ecline để thêm các đoạn miễn là bạn tiếp cận tệp đó, sau đó bạn có thể thêm a ll hunks từ tệp đó.

Bạn cũng có thể chọn một hunk đơn bằng cách trả lời câu hỏi với y es. Nếu hunk có vẻ là quá lớn, bạn thậm chí có thể s plit nó. Nó cũng có thể e dit hunk hiện tại.


Sử dụng --patch -option có thể vào lệnh git khác nhau (f.e. stash, commitadd).

Đây là lời giải thích chi tiết về các --patch -function, mà tôi nắm lấy từ developers documentation:

This lets you choose one path out of a 'status' like selection. 
After choosing the path, it presents the diff between the index 
and the working tree file and asks you if you want to stage 
the change of each hunk. You can select one of the following 
options and type return: 

    y - stage this hunk 
    n - do not stage this hunk 
    q - quit; do not stage this hunk or any of the remaining ones 
    a - stage this hunk and all later hunks in the file 
    d - do not stage this hunk or any of the later hunks in the file 
    g - select a hunk to go to 
/- search for a hunk matching the given regex 
    j - leave this hunk undecided, see next undecided hunk 
    J - leave this hunk undecided, see next hunk 
    k - leave this hunk undecided, see previous undecided hunk 
    K - leave this hunk undecided, see previous hunk 
    s - split the current hunk into smaller hunks 
    e - manually edit the current hunk 
    ? - print help 
+1

hoặc git stash -p – Matthias

5

Sử dụng git stash -p không có đối số. Sau đó, bạn có thể chọn tương tác các thay đổi mà bạn muốn phát hiện:

diff --git a/test b/test 
index acbd8ae..662d47a 100644 
--- a/test 
+++ b/test 
@@ -10,6 +10,7 @@ test 
(...) 
+ test 
(...) 
Stash this hunk [y,n,q,a,d,/,e,?]? 

Rất tiếc, không thể chọn tệp, chỉ có các tệp.

Các vấn đề liên quan