Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I git stash a specific file?

Tags:

git

git-stash

How can I stash a specific file leaving the others currently modified out of the stash I am about to save?

For example, if git status gives me this:

younker % gst       # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # # 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:   app/controllers/cart_controller.php #   modified:   app/views/cart/welcome.thtml # no changes added to commit (use "git add" and/or "git commit -a") 

and I only want to stash app/views/cart/welcome.thtml, how would I do that? Something like (but of course this does not work):

git stash save welcome_cart app/views/cart/welcome.thtml 
like image 759
ynkr Avatar asked Mar 31 '11 21:03

ynkr


People also ask

Can you stash a specific file in git?

To stash a specific file, use the “git stash push” command and specify the file you want to stash. However, the other tracked files that may be modified in your current working directory are untouched.

Can I stash a single file?

You can interactively stash single lines with git stash -p (analogous to git add -p ). It doesn't take a filename, but you could just skip other files with d until you reached the file you want stashed and the stash all changes in there with a . This takes a long time if you have a lot of files you don't want to stash.


1 Answers

EDIT: Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>. For example:

git stash push -m welcome_cart app/views/cart/welcome.thtml 

OLD ANSWER:

You can do that using git stash --patch (or git stash -p) -- you'll enter interactive mode where you'll be presented with each hunk that was changed. Use n to skip the files that you don't want to stash, y when you encounter the one that you want to stash, and q to quit and leave the remaining hunks unstashed. a will stash the shown hunk and the rest of the hunks in that file.

Not the most user-friendly approach, but it gets the work done if you really need it.

like image 65
svick Avatar answered Sep 23 '22 07:09

svick