It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

What if you don't want to stage/commit part of the "hunk"?

Sun Kim Mar 29, 2016

What if the "hunk" has a part you don't want to stage and commit? For example, I only want to commit lines 50-100, but not 1-50. SourceTree unfortunately sees this as one hunk (line 1-100). I don't know how to commit only the lines I want.

The reason is that the code is addressing two different issues, so we don't want to co-mingle the code if possible.

I guess another way to ask is how do I make SourceTree split into smaller hunks?


2 answers

1 accepted

5 votes
Answer accepted
Johannes Kilian Mar 29, 2016

SourceTree offers the possibility to selct the lines you want to be part of the next commit ...

Simply select the lines you want to stage within the diff view and stage the selected lines - see attached videoCJ.gif

Sun Kim Mar 29, 2016

So this is a diff commit? I noticed a diff patch file shows up in the same folder after I commit. I did a discard hunk but the stage hunk line by line seems the best thing to do.

Johannes Kilian Mar 29, 2016

Not quite sure, I understood your question correctly.

The diff view shows the differences between your local version and the latest version from your repository. Within the staging area you build up a container which contains all changes which are part of the next commit - this might be complete changed files or only parts of the changed files (as you asked for) - while other changed parts are not part of the next commit: I do not unterstand the term "diff commit" here.

I never noticed a diff patch after I commit - but I have to admit, I never looked for it ... wink

Discarding a hunk is nothing else but reverting the changes within the hunk - the changes are gone then (and I think that's not what you want the second part of your hunk should probably be part of a subsequent commit)). As I unterstood your original question, "stage lines" is definitly what you are looking for ..


BTW: SourceTree is by far the best git-GUI I've seen for building up a proper staging area, supporting staging complete files and parts of files (from complete hunks down to single lines) in a very easy way

Sun Kim Mar 30, 2016

I clicked on some lines, then clicked on stage hunk. Then this patch.diff showed up in my folder. That's what I meant by diff commit. I'm sorry if that term means something else. I'm new to git.


0 votes
Jonathan Craig Mar 29, 2016

I don't know of a way to commit part of a file. I would assume that you should copy your fixes outside of your current tree. Reset the tree to the last commit and then put your first fix back in. Commit that chunk and then do it again.

Sun Kim Mar 29, 2016

When you see the diff between current and last committed file, you will see a button on the diff called stage hunk or discard hunk. This is the way to commit partial changes in the file. My issue is that the hunk sometimes includes more code than I want to include in the specific commit. I may be cleaning up code unrelated to the commit that I want to later commit separately.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder :  #!/bin/bash source "$(dirname "$0")/" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

3,092 views 1 20
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you