Elias Zamaria
Elias Zamaria

Reputation: 101053

How can I ignore spaces when applying a git stash?

I have some modifications in a git stash, and I would like to apply them using git stash pop or git stash apply. But I have changed some indentation in the files that the stash changes, and when I try to apply the stash, I get a bunch of merge conflicts. Is there a way to ignore whitespace while applying the stash, similar to the -Xignore-all-space option for git merge?

I tried git stash apply -Xignore-all-space stash@{2} and it told me error: unknown switch `X'.

Upvotes: 2

Views: 959

Answers (2)

torek
torek

Reputation: 487885

Don't try to do this with git stash. Use the full Git tool suite.

OK, but you already have a stash. Fortunately there's a really easy way to convert the stash to a temporary branch, using git stash branch:

git status    # make sure all is clean; if not, commit or stash

then:

git stash branch temp-branch stash@{2}
git status
git commit     # optional: use if desired to save index before adding
git add ...    # add all files as needed
git commit     # make the commit

Now go back to the branch you were on and use git merge or git cherry-pick or whatever you like to work with the commit at the tip of temp-branch. The -X options are now available.

Note that git stash branch will have run git stash drop stash@{2}. That's OK because now you have a whole branch, named temp-branch (or any other name of your choice).

(You can do this, sort of, carefully, with git cherry-pick directly on stash@{2}, but it's hard and painful; the git stash branch method is a bit klunky, but painless. If you really want to do it directly, remember that this is reflog entry #2 of refs/stash, and the stash commit for w takes the form of a merge, with first-parent being the commit from which the "stash bag" depends.)

Upvotes: 5

rodrigo
rodrigo

Reputation: 98348

I had this same problem once, and I didn't find any option to git-stash to ignore whitespaces. But git-apply does have it. So what I did was:

$ git stash show --patch stash@{2} | git apply --ignore-whitespace

And then optionally, git stash drop to delete the stash.

Upvotes: 0

Related Questions