Git – How to compare files in two different branches
This tutorial explains you how to compare files in two different branches in Git.
Git – Compare files in two different branches
Let’s say you have the following Skill POJO checked in to your “phase2-dev” branch already.
package com.sneppets.model; public class Skill { private String name; public Skill() { } public Skill(String name) { this.name = name; } public Skill(Long id, String name) { super(id); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
And you are working with local feature branch called “myfeature” branch and had modified the Skill.java POJO like below
package com.sneppets.model; public class Skill { private String name; public Skill() { } public Skill(String name) { this.name = name; } public Skill(Long id, String name) { super(id); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object obj) { if(obj == this) return true; if(!(obj instanceof Skill)) { return false; } Skill skill = (Skill)obj; return skill.id.equals(id); } @Override public int hashCode() { return this.id.hashCode(); } }
Now to compare Skill.java file between two branches (phase2-dev & myfeature branches) try the following command
$ git diff phase2-dev — src/main/java/com/sneppets/model/Skill.java
Note, Get to the current branch name (in this case myfeature branch) where you had modified the code.
sneppets@MyComputer MINGW64 /e/sneppets/code (myfeature) $ git diff phase2-dev -- src/main/java/com/sneppets/model/Skill.java --- a/code/services/MyService/src/main/java/com/sneppets/model/Skill.java +++ b/code/services/MyService/src/main/java/com/sneppets/model/Skill.java @@ -38,6 +38,28 @@ public class Skill { public void setName(String name) { this.name = name; } + + @Override + public boolean equals(Object obj) + { + if(obj == this) + return true; + + if(!(obj instanceof Skill)) + { + return false; + } + + Skill skill = (Skill)obj; + + return skill.id.equals(id); + + } + + @Override + public int hashCode() { + return this.id.hashCode(); + } }
Similarly if you wanted to compare same file between phase2-dev and master branch then try the following
$ git diff phase2-dev master — src/main/java/com/sneppets/model/Skill.java
That’s All.
Further Reading
- How to throw away the local commits from your branch in Git ?
- Convert comma separated String to List
- Expected BEGIN_OBJECT but was STRING at line 1 column 1
- Could Not Find Or Load Main Class
- Java Round up a Float value to an Int value