DEV Community

Cover image for A Thorough Introduction to Git's Interactive Patch Mode

A Thorough Introduction to Git's Interactive Patch Mode

Jon Kurinsky on July 28, 2019

You've been hacking away at a project when you realize: "I haven't committed in an hour." Or, perhaps worse still: "my unstaged changes represent m...
Collapse
 
uttarasriya profile image
Sriya

Is it possible to see conflicts before merging the code using git ?

Collapse
 
krnsk0 profile image
Jon Kurinsky

Yes. Say you're in the master branch and you want to check a branch called 'feature' for conflicts. Do git merge feature --no-ff --no-commit. Once you have what you need, to get out of the state this puts git in, do git merge --abort.

Collapse
 
mak_arnautovic_69e661f698 profile image
Mak Arnautovic • Edited

Just want to make a minor correction. At the time of writing (git version 2.30.2) you can edit changes in a line and stage them separately via patch mode. Though at the end of the day the changes do have to be able to be represented in terms of an entire line. Maybe that's what you meant in the first place but in case anyone needs an example:

Say I add this function to a file:

function gonnaChangeThis(thisComesLater) {
  console.log("nothing here!");
  console.log(thisComesLater);
}
Enter fullscreen mode Exit fullscreen mode

I can do:
git add -p


+  function gonnaChangeThis(thisComesLater) {
+    console.log("nothing here!");
+    console.log(thisComesLater);
+  }
+
function someOtherThing() {
(1/1) Stage this hunk [y,n,q,a,d,e,?]? e
Enter fullscreen mode Exit fullscreen mode

and edit just by deleting what I don't want via the editor (in this case the func arg and corresponding line):

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -37,6 +37,11 @@ 

+function gonnaChangeThis() {
+  console.log("nothing here!");
+}
+
Enter fullscreen mode Exit fullscreen mode

save, quit, and voila:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   exampleFile.js

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   exampleFile.js
Enter fullscreen mode Exit fullscreen mode

git commit
git add -p

-  function gonnaChangeThis() {
+ function gonnaChangeThis(thisComesLater) {
    console.log("nothing here!");
+  console.log(thisComesLater);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chan_austria777 profile image
chan 🤖

it seems like they changed the behaviour on git add --intent-to-add, i can no longer git add -p on a new file, the e option doesn't appear or work