How to only stash specific hunks (like patch)?

Is there a way to git stash just specific hunks? It seems to always take all files.

It would be nice that if the user could just stash the staged items (or the unstaged items - your choice). Essentially using the index as the input to --patch.

2 answers

+1 from me as well. TortoiseHG allows to shelve (the equivalent of stash for mercurial) hunks and lines and it's very convenient.

I know it is already possible to commit only hunks or lines, but I want to only stage working code. The scenario that I am dealing with is the following :

  1. I want to add a new simple feature to my code.
  2. along with adding the feature I realise that I must modify a deep layer of the software and I go along
  3. I get everything to work (compile, unit-tests, functional tests) but now I have too many changes to commit, which are related to different areas of the code.
  4. From now on, I am desribing the workflow that I would like to have, not yet available in sourcetree.
  5. I want to commit first the changes in the deep layer of the code. I start an interactive stashing, stashing all the hunks not related to the deep layer of the code
  6. I now have only the changes related to the deep layer in my working copy. I compile, run unit-tests, run functional tests. I fix a typo here and there. 
  7. Everything works, I commit
  8. I restore my stash of the feature I was working on.
  9. I compile, run unit-tests, run functional tests.
  10. I commit my feature.

 

As you see, the important point is step 6 : I need to compile and run the exact code that I am checking in. This is not possible with hunk staging where you are staging something that is in practise not tested isolately.

 

+1. This would be very useful if you do a codechange unrelated to your current feature and want to keep it for later use.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 23, 2018 in Sourcetree

Tip from the team: configure your repos for hosting goodness!

Supported Platforms macOS Windows We recently introduced support for additional hosting services such as GitHub Enterprise, GitLab (Cloud, Community Edition, Enterprise Edition), and...

706 views 3 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you