Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cherry-pick with ignore whitespace / line-endings

is there a way to tell git cherry-pick to use the renormalize merge strategy? I'm not sure the -X option is working.

I have a bunch of commits that seem to assume one type of line ending, and I'm trying to apply them to a branch that assumes another. Not having a good time...

like image 899
hwjp Avatar asked Jul 16 '14 13:07

hwjp


People also ask

How do you end a cherry pick?

cherry-pick: fix --quit not deleting CHERRY_PICK_HEAD --quit is supposed to be --abort but without restoring HEAD . Leaving CHERRY_PICK_HEAD behind could make other commands mistake that cherry-pick is still ongoing (e.g. " git commit --amend " will refuse to work). Clean it too.

Can you cherry pick a range of commits?

The thing is if you are cherry picking a range of commits, it will cherry pick the parent commits correctly but then when it hits a normal commit, it fails and says commit is not a merge.

Does git cherry pick rewrite history?

Running git rebase typically rewrites history and can appear to move entire branches around. This is likely the source of the undesired history you are seeing. In contrast, git cherry-pick replays the delta from one or more commits elsewhere in a repository's history.

Does order matter in git cherry pick?

Absolutely, commit order matters. If commit B modifies stuff that was first introduced in commit A, then you can't apply commit B until commit A has been applied.


2 Answers

So, for completeness, the answer is that the ignore-all-space merge strategy does the job:

git cherry-pick -X ignore-all-space <commit-id>

And that will let you painlessly cherry-pick commits made when the file had, eg, windows line endings onto a version that has unix file endings.

like image 69
hwjp Avatar answered Oct 03 '22 06:10

hwjp


I know this question is significantly old, but adding this answer because this is the first post from googling "git cherry-pick ignore whitespace".

Even though -X ignore-all-space works fine, you have to manually check the commit if the cherry-pick without ignore space option had some conflicts. (or use --no-commit while cherry-picking and git diff --staged to review it)

In some cases, -X ignore-all-space option looks fine, but some indentations are wrong.

For example, suppose you had some merge conflict with leading whitespace while not using ignore-all-space, like this:

    Change from theirs, Indent level 1(no conflict with/out whitespace)
<<<<< HEAD
Indent level 0
=====
    Indent level 1 without any code change
>>>>> cherry-picked commit

In this case, -X ignore-all-space shows no conflict but the actual commit will look like this:

    Change from theirs, indent level 1
Indent level 0

What happened here is they changed logic so the previous code (Indent level 0) should be indented to level 1, but it didn't because you specified ignore-all-space option.

So the tl;dr is:

  1. -X ignore-all-space option also ignores leading whitespaces, which might be troublesome in some situations and languages like Python.
  2. So you have to manually review after using that option, or...
  3. Use -X ignore-space-at-eol instead, and handle leading whitespace conflicts manually.

But don't stop reading, because these options are not the main reason of this problems - the core of this problem is not everyone is sticking to same whitespace rules.

For leading&trailing whitespaces: use linting tools, or IDEs, or whatever to stick to same rules. This is not OS-specific and can be followed if your team puts some effort to making other developers' life easier.

For eol changes: this is OS-specific, but git fortunately supports core.autocrlf and core.eol for multi-platform development enveronment. see git-scm for more details.

like image 21
ik1ne Avatar answered Oct 05 '22 06:10

ik1ne