Creating and Editing patches for the Linux Kernel with git
There’s a lot of posts and references about submitting patches to the Linux kernel or projects that receive patches in file/email format.
The idea here is to have a quick guide about the basic commands to get things done.
First checkout the working tree. For Linux it’s usually the stable at https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git or Linus at https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/ containing latest changes not yet released.
Then, edit your required files. It’s advised that you keep meaningful functionality together and break them into different commits (that will generate separate patches). In case of a small change, usually one commit is enough.
# Add your files to staging area
$ git add [file or files]# If you have a lot of changes in the same file and parts should go to different commits, select which part is staged interactively with
$ git add -p [file if desired]# Commit changes with meaningful commit message. Usually look at previous commits to the same file or subsystem
$ git commit -s -v
Then create the patch file:
# Generate single patch file for last commit
$ file=$(git format-patch HEAD~)# Or if generating multiple patches for multiple commits
# show last log for X commits
$ git log --pretty=oneline --abbrev-commit -n [X]# Generate patch files for commits between X and Y
$ git format-patch -o ./output --cover-letter -n --thread=shallow XXX^..YYY
The last command also generates a file starting with “0000-” that is a cover letter, where you should describe what the patch series does in a broad view. If sending a single patch, it’s usually not needed.
Finally check the patch file and get the email destinations assigned for the changes made. Finally send the patch over email (to you first, then to the Linux lists). Check the appendix A at the end about configuring git to send emails.
# Check patch file