Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial git apply

Tags:

How can I get git apply --index to not abort completely just because a single hunk fails? I have a 100K+ patch with dozens of files deleted and added, it'd be a pain to do the patch -p1, git add, git rm dance manually.

Edit: git apply --reject --index seems to do two thirds of the work: the patch is applied and removed files are staged for deletion but new files are not added.

like image 350
chx Avatar asked Apr 03 '13 19:04

chx


People also ask

What is git apply?

git apply takes a patch (e.g. the output of git diff ) and applies it to the working directory (or index, if --index or --cached is used). git am takes a mailbox of commits formatted as an email messages (e.g. the output of git format-patch ) and applies them to the current branch.

Why is git apply skipping patch?

A patch is usually skipped when the changes it contains were already applied in the past. There are many possible reasons for this: a merge, a cherry-pick, changes operated manually or using another patch etc.

How do git patches work?

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository. This way other people can check your changes from the GIT patch file you made and suggest the necessary corrections.

What is partial clone in Git?

The "Partial Clone" feature is a performance optimization for Git that allows Git to function without having a complete copy of the repository. The goal of this work is to allow Git better handle extremely large repositories. During clone and fetch operations, Git downloads the complete contents and history of the repository.

What does Git apply expect when applying patches?

By default, git apply expects that the patch being applied is a unified diff with at least one line of context. This provides good safety measures, but breaks down when applying a diff generated with --unified=0 .

How does Git apply work with Atomicity?

For atomicity, git apply by default fails the whole patch and does not touch the working tree when some of the hunks do not apply. This option makes it apply the parts of the patch that are applicable, and leave the rejected hunks in corresponding *.rej files.

How does Git handle extremely large repositories?

The goal of this work is to allow Git better handle extremely large repositories. During clone and fetch operations, Git downloads the complete contents and history of the repository. This includes all commits, trees, and blobs for the complete life of the repository.


1 Answers

Since git version 1.7.12, git apply has a --3way (or -3) option which will:

  1. Apply any changes it can figure out and stage them (i.e.: add them to the working tree and index),
  2. Add any new files and stage them (i.e.: add them to the working tree and index), and,
  3. When it encounters a merge conflict, it will, for each conflicting hunk:

    1. Put the code from the branch you are patching between <<<<<<< ours and ======= markers, and,
    2. Put the code from the patch file between ======= and >>>>>>> theirs markers.

    ... files with conflicts will not be staged: you'll have to manually fix them and git add them afterwards.

like image 58
mparker17 Avatar answered Oct 06 '22 18:10

mparker17