Stash only one file out of multiple files that hav

2019-01-03 00:18发布

How can I stash only one of multiple changed files on my branch?

标签: git git-stash
27条回答
Anthone
2楼-- · 2019-01-03 00:54

You can also use git stash save -p "my commit message". This way you can select which hunks should be added to stash, whole files can be selected as well.

You'll be prompted with a few actions for each hunk:

   y - stash this hunk
   n - do not stash this hunk
   q - quit; do not stash this hunk or any of the remaining ones
   a - stash this hunk and all later hunks in the file
   d - do not stash 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
查看更多
小情绪 Triste *
3楼-- · 2019-01-03 00:55

Just in case you actually mean discard changes whenever you use git stash (and don't really use git stash to stash it temporarily), in that case you can use

git checkout -- <file>

[NOTE]

That git stash is just a quicker and simple alternative to branching and doing stuff.

查看更多
叛逆
4楼-- · 2019-01-03 00:56

To stash a single file use git stash --patch [file].

This is going to prompt: Stash this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?. Just type a (stash this hunk and all later hunks in the file) and you're fine.

查看更多
叛逆
5楼-- · 2019-01-03 00:57

Since creating branches in Git is trivial you could just create a temporary branch and check the individual files into it.

查看更多
Juvenile、少年°
6楼-- · 2019-01-03 00:58

Sometimes I've made an unrelated change on my branch before I've committed it, and I want to move it to another branch and commit it separately (like master). I do this:

git stash
git checkout master
git stash pop
git add <files that you want to commit>
git commit -m 'Minor feature'
git stash
git checkout topic1
git stash pop
...<resume work>...

Note the first stash & stash pop can be eliminated, you can carry all of your changes over to the master branch when you checkout, but only if there are no conflicts. Also if you are creating a new branch for the partial changes you will need the stash.

You can simplify it assuming no conflicts and no new branch:

git checkout master
git add <files that you want to commit>
git commit -m 'Minor feature'
git checkout topic1
...<resume work>...

Stash not even needed...

查看更多
Animai°情兽
7楼-- · 2019-01-03 00:59

Quick Answer


For revert a specific changed file in git, you can do the following line:

git checkout <branch-name> -- <file-path>

Here is an actual example:

git checkout master -- battery_monitoring/msg_passing.py

查看更多
登录 后发表回答