本地Linux内核补丁维护的最优Git配置咨询
Hey David, let's work through this setup for your Linux kernel patch workflow with your local GitLab instance. You've got two great options here—one that leans into your ideal "minimal patch-only storage" goal, and another that keeps a full code tree while still tracking upstream baseline updates clearly.
Option 1: Ideal State – Store Only Patches & New Files (Generate Full Tree On Clone)
This approach keeps your GitLab repo lightweight, storing just your patches, custom files, and a helper script to build the full patched kernel tree when needed.
Step 1: Create & Initialize the Patch Repository on GitLab
- Log into your local GitLab and create a new empty repo (e.g.,
linux-kernel-patches). - On your local machine, set up the repo and link it to GitLab:
mkdir linux-kernel-patches && cd $_ git init # Add upstream kernel as a remote to track baseline updates git remote add upstream https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git # Push initial empty repo to GitLab git remote add origin git@your-local-gitlab:your-username/linux-kernel-patches.git git push -u origin master
Step 2: Manage Patches & New Files
- Store patches: When you create patches (using
git format-patchfrom a kernel dev tree), save them to apatches/directory in this repo and commit:# Example: Generate patches from your dev branch vs upstream baseline git format-patch upstream/master --output-directory patches/ git add patches/ git commit -m "Add patches for USB driver fixes" git push origin master - Store new files: Create a
new-files/directory for any entirely new files you're adding (e.g., custom drivers), commit them directly to the repo.
Step 3: Add a Script to Build the Full Kernel Tree
Create a script that pulls the upstream baseline, applies your patches, and copies new files. Save it as build-full-tree.sh:
#!/bin/bash set -e # Configure paths (adjust as needed) UPSTREAM_BRANCH="master" FULL_TREE_DIR="../linux-kernel-full" # Fetch latest upstream baseline git fetch upstream # Create full kernel tree directory if it doesn't exist mkdir -p $FULL_TREE_DIR cd $FULL_TREE_DIR # Initialize full tree repo and pull upstream baseline if [ ! -d .git ]; then git init git remote add upstream ../linux-kernel-patches fi git pull upstream $UPSTREAM_BRANCH --force # Apply all patches from the patch repo cd ../linux-kernel-patches git format-patch --stdout upstream/$UPSTREAM_BRANCH | cd $FULL_TREE_DIR && git am # Copy new custom files into the full tree cp -r ./new-files/* $FULL_TREE_DIR/ echo "Full patched kernel tree ready at $FULL_TREE_DIR"
Make it executable, commit, and push:
chmod +x build-full-tree.sh git add build-full-tree.sh git commit -m "Add script to build full patched kernel tree" git push origin master
For Developers Cloning the Repo
They just need to run the script to get a complete, patched kernel tree:
git clone git@your-local-gitlab:your-username/linux-kernel-patches.git cd linux-kernel-patches ./build-full-tree.sh
Option 2: Full Code Tree (Track Upstream Baseline Clearly)
If you prefer working directly on a full kernel tree (no post-clone script needed), this setup stores the complete kernel code while keeping the upstream baseline source explicit.
Step 1: Mirror Upstream Kernel to Your Local GitLab
First, create a full mirror of the Linux kernel repo on your GitLab instance:
# Clone upstream as a mirror git clone --mirror https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git cd linux.git # Push mirror to your local GitLab git remote add local-gitlab git@your-local-gitlab:your-username/linux-kernel-full.git git push --mirror local-gitlab
Pro tip: Set up a GitLab scheduled pipeline to run git fetch upstream && git push --mirror local-gitlab daily to keep your mirror updated automatically.
Step 2: Develop Patches on a Feature Branch
Developers clone the full repo and add the upstream remote to track baseline updates:
git clone git@your-local-gitlab:your-username/linux-kernel-full.git cd linux-kernel-full # Link to upstream to pull baseline updates git remote add upstream https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Create a feature branch for your patches (based on the upstream baseline you're targeting):
git checkout -b my-usb-patches upstream/v5.15 # Make your code changes, add new files, commit git add drivers/usb/my-custom-driver.c git commit -a -m "Add custom USB driver for hardware X" # Push to your local GitLab git push -u origin my-usb-patches
Step 3: Update Baseline When Upstream Changes
When the upstream kernel gets updated, pull the changes and rebase your patches onto the new baseline (cleaner history than merging):
git fetch upstream git checkout my-usb-patches git rebase upstream/v5.15 # Resolve any conflicts, then push the updated branch git push origin my-usb-patches --force-with-lease
Quick Tips for Kernel Patch Workflows
- For Linux kernel development, use
git rebaseinstead ofgit mergewhen updating your patches against upstream—this keeps your patch history linear and easier to review. - Use
git diff --statto check your changes before committing, ensuring you only include the patch content you intend. - If you need to share patches externally, you can generate them from your feature branch with
git format-patch origin/masterto create individual patch files.
内容的提问来源于stack exchange,提问作者David Salame

