Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - how to selectively apply part of a patch

Tags:

git

On a project I am looking after, one user has helpfully provided a large bunch of changes as a single patch. 80%+ is very good and should be included in the project. However there are a few changes he is proposing where I do not agree at all (He has misunderstood how one feature works). How can I with git be selective on his proposed changes. I have read about git checkout -p but don't understand how to use it, if this is the best answer.

like image 216
Waveney Avatar asked Mar 30 '14 11:03

Waveney


People also ask

How do you make a patch for uncommitted changes?

In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.

How do I format a patch in git?

The first rule takes precedence in the case of a single <commit>. To apply the second rule, i.e., format everything since the beginning of history up until <commit>, use the --root option: git format-patch --root <commit> . If you want to format only <commit> itself, you can do this with git format-patch -1 <commit> .


1 Answers

  1. Normally apply patch to working tree
  2. Use git add -i to interactively select files or parts of files to stage.
    • to add whole file use 2 update
    • to review changes on selected files use 5 patch, in this mode for every change git will ask you if you want to stage it or not
  3. Commit staged changes, things you don't want will stay in working tree.
  4. If you wish to clean working tree of unwanted changes use git reset and git clean
like image 144
jazgot Avatar answered Oct 21 '22 12:10

jazgot