Skip to main content

[Git] Handle trailing space when patching with git

After complete coding one module in development branch. Next phase is merging.
Obviously, you can merge source code automatically without no errors happen.
However, life is not dream.

Create a patch
git diff HEAD > newTariff.patch

Apply patch
git apply newTariff.patch

then problem happens
Problem
git apply newTariff.patch:106: trailing whitespace. patch does not apply


Route cause:
Nguyên nhân:
Do trong source code có những dấu space thừa (ô vuông màu đỏ hình dưới)
Some whitespaces is existed in your patch. (red area in below pictures)

Fix
Clean white space and patch again.
Cách khắc phục
Xóa những dấu space này đi và thực hiện patch lại

Make up after complete coding. Find and clean whitespace before create patch file.
Sửa sau khi code xong
Kiểm tra sau khi code xong có lỗi này không
git diff HEAD --check

Prevent whitespace by manually when typing source code
Sửa ngay khi đang code
=>Bật chức năng hiển thị các dấu whitespace, hoặc remove trailing space. Tùy từng editor
Ví dụ:
Notepad ++
Edit -> Blank Operation -> Trim Trailing Space

Visual Studio Code
View -> Toggle Render Whitespace


Reference:
https://stackoverflow.com/questions/14509950/my-diff-contains-trailing-whitespace-how-to-get-rid-of-it#

For me
git apply --reject --whitespace=fix newTariff..path

Howerver it generate a new file *.rej (rej is shorted of "rejected").
I open rej file and continue fixing my problem manually


Other good guideline version.
http://vanwilson.info/2016/05/wrangling-wandering-whitespace-git/


More, Sometimes, for whatever reason, You have to produce patch-files (under Linux) that are in the wrong direction.
Reverse patch file
patch -p1 -R < path/file.patch
(If your patch was applied with the -p0 option, use that instead.)Or:
git apply -R path/file.patch

Showing trailing spaces in vim

:highlight ExtraWhitespace ctermbg=red guibg=red
:match ExtraWhitespace /\s\+$/


Comments

Popular posts from this blog

How to test frame buffer in linux

The intended goal of this article is to provide the user with enough information so they can display an image onto a screen of their choosing. How to re-build frame buffer testing application. Base knowledge http://trac.gateworks.com/wiki/OpenEmbedded/Video_Out Frame buffer test application - rebuild fb-test-app step 1: Download soure code ( git installed on your machine and account in git-hub is required) https://github.com/prpplague/fb-test-app Command in git: git init git clone https://github.com/prpplague/fb-test-app.git step 2: Change makefile vi Makefile //open Makefile by vim editor :set nu //show line number replace from line 6 to line 12 by folow code: CC=arm-linux-gnueabi-gcc CFLAGS=-02 -Wall Save change command :wq step 3: Rebuid Type “make” command step 4: Copy execute file to target system by filezilla or something like that step 5: Remote target by root account, change mode of execute file (what copied from host)

The best 12 AI Tools in 2023

The new life challenges require new tools.  For decades, AI technology has been a fuzzy field for unprofessional users. However, it is not rocket science anymore at this time.  There are the best 12 AI tools in 2023 that will keep you up and take some advantages in your business. Here, I put everything on your table. Enjoy Solves anything - ChatGPT ( https://chat.openai.com/auth/login ) Write anything - Writesonic ( https://writesonic.com/ ) Generates Art - Midjourney ( https://www.midjourney.com/explore ) Generates Code - Replit ( https://replit.com /) Generates Video - Synthesia ( https://www.synthesia.io/ ) Generates Music - Soundraw ( https://soundraw.io/ ) Generates Tiktoks - Fliki ( https://fliki.ai/ ) Generates Avatars - Starrytars ( https://starryai.com/en/starrytars ) Generates PPTs - SlidesAI ( https://www.slidesai.io/ ) Edit Pictures - Remini ( https://remini.ai/ ) Edit Video - Pictory ( https://pictory.ai/ ) Summarize Notes - Wordtune( https://www.wordtune.com /)