Linux command
git-apply 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Apply a patch
git apply [patch.diff]
Check if patch applies
git apply --check [patch.diff]
Apply with stats
git apply --stat [patch.diff]
Reverse a patch
git apply --reverse [patch.diff]
Apply to index
git apply --cached [patch.diff]
Apply with rejections
git apply --reject [patch.diff]
Apply to subdirectory
git apply --directory=[modules/subdir] [patch.diff]
说明
git apply applies patches to files and/or the index. Unlike git am, it works with raw diff output without email formatting, making it suitable for patches generated by git diff. The command can apply patches to the working tree, index, or both. It supports checking patches without applying (--check) and showing statistics (--stat). Reverse application (--reverse) enables undoing patches. Unlike git am, this command does not create commits, making it suitable for applying patches as working directory modifications that can be reviewed before committing.
参数
- --check
- Check if patch applies cleanly.
- --stat
- Show diffstat instead of applying.
- --reverse, -R
- Apply patch in reverse.
- --cached
- Apply to index only.
- --3way, -3
- Attempt 3-way merge if patch does not apply cleanly. Implies --index.
- --reject
- Apply applicable hunks and leave rejected ones in .rej files instead of failing the whole patch.
- --numstat
- Show added/deleted line counts in machine-readable format instead of applying.
- --directory _DIR_
- Prepend root directory to all filenames.
- --exclude _PATTERN_
- Skip files matching the given path pattern.
- --include _PATTERN_
- Only apply to files matching the given path pattern.
- --whitespace _ACTION_
- Handle whitespace errors (nowarn, warn, fix, error, error-all).
- -v, --verbose
- Report progress.
- --help
- Display help information.
FAQ
What is the git-apply command used for?
git apply applies patches to files and/or the index. Unlike git am, it works with raw diff output without email formatting, making it suitable for patches generated by git diff. The command can apply patches to the working tree, index, or both. It supports checking patches without applying (--check) and showing statistics (--stat). Reverse application (--reverse) enables undoing patches. Unlike git am, this command does not create commits, making it suitable for applying patches as working directory modifications that can be reviewed before committing.
How do I run a basic git-apply example?
Run `git apply [patch.diff]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --check do in git-apply?
Check if patch applies cleanly.