Skip to content

Commit 06ea626

Browse files
committed
Advanced merging: use -Xignore-space-change
For most programming languages use of the -Xignore-space-change option while merging could be more suitable than that of the -Xignore-all-space option because it prevents different lexemes from concatenating together (e.g. "def hello" and "defhello"). This commit fixes both long and short versions of the whitespace ignoring options.
1 parent 3d8a503 commit 06ea626

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

book/07-git-tools/sections/advanced-merging.asc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $ git commit -am 'converted hello.rb to DOS'
4040
1 file changed, 7 insertions(+), 7 deletions(-)
4141
4242
$ vim hello.rb
43-
$ git diff -w
43+
$ git diff -b
4444
diff --git a/hello.rb b/hello.rb
4545
index ac51efd..e85207e 100755
4646
--- a/hello.rb
@@ -121,11 +121,11 @@ If for some reason you find yourself in a horrible state and just want to start
121121

122122
In this specific case, the conflicts are whitespace related. We know this because the case is simple, but it's also pretty easy to tell in real cases when looking at the conflict because every line is removed on one side and added again on the other. By default, Git sees all of these lines as being changed, so it can't merge the files.
123123

124-
The default merge strategy can take arguments though, and a few of them are about properly ignoring whitespace changes. If you see that you have a lot of whitespace issues in a merge, you can simply abort it and do it again, this time with `-Xignore-all-space` or `-Xignore-space-change`. The first option ignores changes in any **amount** of existing whitespace, the second ignores all whitespace changes altogether.
124+
The default merge strategy can take arguments though, and a few of them are about properly ignoring whitespace changes. If you see that you have a lot of whitespace issues in a merge, you can simply abort it and do it again, this time with `-Xignore-all-space` or `-Xignore-space-change`. The first option ignores whitespace **completely** when comparing lines, the second treats sequences of one or more whitespace characters as equivalent.
125125

126126
[source,console]
127127
----
128-
$ git merge -Xignore-all-space whitespace
128+
$ git merge -Xignore-space-change whitespace
129129
Auto-merging hello.rb
130130
Merge made by the 'recursive' strategy.
131131
hello.rb | 2 +-
@@ -178,7 +178,7 @@ dos2unix: converting file hello.theirs.rb to Unix format ...
178178
$ git merge-file -p \
179179
hello.ours.rb hello.common.rb hello.theirs.rb > hello.rb
180180
181-
$ git diff -w
181+
$ git diff -b
182182
diff --cc hello.rb
183183
index 36c06c8,e85207e..0000000
184184
--- a/hello.rb
@@ -195,7 +195,7 @@ index 36c06c8,e85207e..0000000
195195
hello()
196196
----
197197

198-
At this point we have nicely merged the file. In fact, this actually works better than the `ignore-all-space` option because this actually fixes the whitespace changes before merge instead of simply ignoring them. In the `ignore-all-space` merge, we actually ended up with a few lines with DOS line endings, making things mixed.
198+
At this point we have nicely merged the file. In fact, this actually works better than the `ignore-space-change` option because this actually fixes the whitespace changes before merge instead of simply ignoring them. In the `ignore-space-change` merge, we actually ended up with a few lines with DOS line endings, making things mixed.
199199

200200
If you want to get an idea before finalizing this commit about what was actually changed between one side or the other, you can ask `git diff` to compare what is in your working directory that you're about to commit as the result of the merge to any of these stages. Let's go through them all.
201201

@@ -222,11 +222,11 @@ index 36c06c8..44d0a25 100755
222222

223223
So here we can easily see that what happened in our branch, what we're actually introducing to this file with this merge, is changing that single line.
224224

225-
If we want to see how the result of the merge differed from what was on their side, you can run `git diff --theirs`. In this and the following example, we have to use `-w` to strip out the whitespace because we're comparing it to what is in Git, not our cleaned up `hello.theirs.rb` file.
225+
If we want to see how the result of the merge differed from what was on their side, you can run `git diff --theirs`. In this and the following example, we have to use `-b` to strip out the whitespace because we're comparing it to what is in Git, not our cleaned up `hello.theirs.rb` file.
226226

227227
[source,console]
228228
----
229-
$ git diff --theirs -w
229+
$ git diff --theirs -b
230230
* Unmerged path hello.rb
231231
diff --git a/hello.rb b/hello.rb
232232
index e85207e..44d0a25 100755
@@ -245,7 +245,7 @@ Finally, you can see how the file has changed from both sides with `git diff --b
245245

246246
[source,console]
247247
----
248-
$ git diff --base -w
248+
$ git diff --base -b
249249
* Unmerged path hello.rb
250250
diff --git a/hello.rb b/hello.rb
251251
index ac51efd..44d0a25 100755

book/C-git-commands/1-git-commands.asc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ We use it to look for possible whitespace issues before committing with the `--c
102102

103103
We see how to check the differences between branches more effectively with the `git diff A...B` syntax in <<_what_is_introduced>>.
104104

105-
We use it to filter out whitespace differences with `-w` and how to compare different stages of conflicted files with `--theirs`, `--ours` and `--base` in <<_advanced_merging>>.
105+
We use it to filter out whitespace differences with `-b` and how to compare different stages of conflicted files with `--theirs`, `--ours` and `--base` in <<_advanced_merging>>.
106106

107107
Finally, we use it to effectively compare submodule changes with `--submodule` in <<_starting_submodules>>.
108108

@@ -192,7 +192,7 @@ The `git merge` command was first introduced in <<_basic_branching>>. Though it
192192

193193
We covered how to do a squashed merge (where Git merges the work but pretends like it's just a new commit without recording the history of the branch you're merging in) at the very end of <<_public_project>>.
194194

195-
We went over a lot about the merge process and command, including the `-Xignore-all-whitespace` command and the `--abort` flag to abort a problem merge in <<_advanced_merging>>.
195+
We went over a lot about the merge process and command, including the `-Xignore-space-change` command and the `--abort` flag to abort a problem merge in <<_advanced_merging>>.
196196

197197
We learned how to verify signatures before merging if your project is using GPG signing in <<_signing_commits>>.
198198

0 commit comments

Comments
 (0)