2334

I want to merge two branches that have been separated for a while and wanted to know which files have been modified.

Came across this link: http://linux.yyz.us/git-howto.html (moved to web.archive.org) which was quite useful.

The tools to compare branches I've come across are:

  • git diff master..branch
  • git log master..branch
  • git shortlog master..branch

Was wondering if there's something like "git status master..branch" to only see those files that are different between the two branches.

Without creating a new tool, I think this is the closest you can get to do that now (which of course will show repeats if a file was modified more than once):

  • git diff master..branch | grep "^diff"

Was wondering if there's something I missed...

PatS
  • 8,833
  • 12
  • 57
  • 100
johannix
  • 29,188
  • 15
  • 39
  • 42
  • 23
    How many others find the title of this question misleading? It is actually about finding the file differences between two branches. What I came here looking for was how to see file differences between two revisions on the same branch. Or am I the only one? – Sandeepan Nath Mar 18 '16 at 10:19
  • 7
    @SandeepanNath: with git there is no difference. You are ALWAYS referring to individual commits. – Samuel O'Malley Mar 21 '16 at 02:54
  • @SamuelO'Malley I am new to git and considering the seemingly common branching strategy wherein all the branches are finally merged to the master branch and ultimately the master is rolled out. Now, considering the event of a rollout, where the production is already at master, but behind the tip (by one revision if the last rollout happened after the last master merge), I would like to see the differences between these two revisions, to find out what would be rolled out. I would not like to look at the branch which was last merged. Correct me if I am wrong. – Sandeepan Nath Mar 21 '16 at 06:22
  • 3
    @SandeepanNath: instead of using the branch names then you can take the answers below and just specify the commit IDs instead. Or even refer the commits by their tag names if you create tags when rolling out. – Samuel O'Malley Mar 21 '16 at 06:50
  • 3
    @SandeepanNath You cannot compare 2 branches, you must specify the revision. So comparing 2 branches is comparing 2 revisions. – Bastien Vandamme Dec 15 '17 at 15:37
  • You can give tags on different commits of the same branch, or commit hashes. `$: git diff --name-status f5355424206..c755066b097` is pretty specific. :) – Paul Hodges Feb 07 '20 at 21:02
  • 1
    @SandeepanNath think of a branch name as just an alias for the last commit on that branch (aka HEAD) – cygnus May 06 '20 at 16:38

21 Answers21

2879

To compare the current branch against main branch:

$ git diff --name-status main

To compare any two branches:

$ git diff --name-status firstbranch..yourBranchName

There is more options to git diff in the official documentation (and specifically --name-status option).

Natim
  • 17,274
  • 23
  • 92
  • 150
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • 4
    What's do each of the indices on the left hand side mean (I see a lot of M's and D's)? – gogogadgetinternet Apr 05 '13 at 18:25
  • 25
    @user446936 - you can see what the letters mean in the git status man page @ https://www.kernel.org/pub/software/scm/git/docs/git-status.html - in particular, M == modified, D == deleted – James Manning Apr 11 '13 at 18:34
  • The same syntax works for comparing a branch with a tag or a tag with another tag. – Daniel Zohar May 27 '13 at 07:03
  • 13
    `git diff --name-status your_branch...master` outputs the changes that occurred on master since your_branch was created from it – Radu Aug 20 '13 at 10:38
  • 1
    The double-dot operator is superfluous, here, because diffs are pairwise. – jub0bs Sep 07 '15 at 11:54
  • 3
    I get unknown revision or path not in the working tree. – SuperUberDuper Oct 15 '15 at 14:18
  • Don't forget to use all three (3) hypens (aka dashes)! Yup, git likes hyphens. – SMBiggs Oct 19 '15 at 18:44
  • AFAIK, if you run the command and then nothing happens ie no lines are created, it means that there is no difference. – mfaani Aug 17 '16 at 22:57
  • Downvoted, `fatal: bad revision 'master..branchName'` Obviously if git command is obscure to use, and most are, answering requires lot more than just post random code snippet. – Tomáš Zato Jan 18 '17 at 15:13
  • 1
    You can actually do better than that if your working directory is the compared branch: `git diff --name-status master` – Ricardo Rodrigues Nov 22 '17 at 12:32
  • 1
    If any of the 2 branches are not present locally, you will have an error like `fatal: ambiguous argument '1stBranch..2ndBranch': unknown revision or path not in the working tree.`. Checkout the branches to solve this: `git checkout 1stBranch` & `git checkout 2ndBranch` – Elouan Keryell-Even Aug 16 '18 at 08:16
  • How to skip warnings? – ceklock Jan 21 '20 at 15:02
  • How to do this without any text being printed? So the main question is *how to say that name_x and name_y are pointing to the very same hash?* – 0andriy May 14 '20 at 13:45
  • 4
    It worth to mention '--name-only' option. It provide information only about the names of the files that changed, without status-codes. If might be more helpful in some scripts. – Vladislav Nov 13 '20 at 18:10
  • 1
    you may also filter only Modified(M) or Deleted(D) files. using `--diff-filter=(A|C|D|M|R|T|U|X|B)` – Akhil Nov 15 '22 at 08:29
  • To compare the main branch with your current working directory you omit the branch name and use two dots. For example, `git diff ..main` shows changes that need to be made from the main branch to get to the code in my working directory. – PatS Dec 28 '22 at 17:23
  • git diff --name-status Rel_Branch..Dev_Branch – Kasthuri Shravankumar Jan 25 '23 at 14:29
  • You may use `--pretty=''` in order to get rid of commit messages between file paths like in [this answer](https://stackoverflow.com/a/77017382/1174405) – Jimmix Aug 31 '23 at 16:08
441

Try

$ git diff --stat --color master..branchName

This will give you more info about each change, while still using the same number of lines.

You can also flip the branches to get an even clearer picture of the difference if you were to merge the other way:

$ git diff --stat --color branchName..master
Gerry
  • 10,584
  • 4
  • 41
  • 49
  • 80
    If you have (highly recommended, imho) git color turned on (`config --global color.ui true`), you can skip the --color. (I have lks - lazy keyboard syndrome.) – Art Swri Mar 10 '12 at 21:35
  • 28
    I'm with you on color! BTW I meant to say `git config --global color.ui true` - to be complete. – Art Swri Mar 10 '12 at 22:13
  • 3
    Does not work, throws errors: `fatal: ambiguous argument 'master..branchName': unknown revision or path not in the working tree.` – Tomáš Zato Jan 18 '17 at 15:15
  • 7
    @TomášZato sorry but you need to swap "branchName" with the name of your branch. – Gerry Jan 30 '17 at 12:01
171

Also keep in mind that git has cheap and easy branching. If I think a merge could be problematic I create a branch for the merge. So if master has the changes I want to merge in and ba is my branch that needs the code from master I might do the following:

git checkout ba
git checkout -b ba-merge
git merge master
.... review new code and fix conflicts....
git commit
git checkout ba
git merge ba-merge
git branch -d ba-merge
git merge master

End result is that I got to try out the merge on a throw-away branch before screwing with my branch. If I get my self tangled up I can just delete the ba-merge branch and start over.

Eric Anderson
  • 3,692
  • 4
  • 31
  • 34
  • 5
    Awesome. I have never thought of branching that way. I think this should be considered as part of the "best practices" when merging. – egelev Jul 01 '15 at 14:08
  • When you merge the ba-marge back into ba, isn't there a potential to have to fix the conflicts again? – Josef.B May 02 '17 at 02:48
  • 2
    @EricAnderson Right, it's a graph. SVN sticks like gum under a school desk. thx. – Josef.B May 02 '17 at 04:10
  • 1
    Why do you need to do last step 'git merge master' if ba-merge already had master – qwebek Sep 04 '18 at 14:30
  • 1
    You could leave it off. The only reason it would be useful is if new stuff landing in `master` while you were working on reviewing the code and fixing conflicts. – Eric Anderson Sep 04 '18 at 14:49
  • 3
    @EricAnderson why would you need a throw away branch if something failed during merge? Isn't just cancelling the merge changes be enough? Something like, `git reset --hard; git clean -fd` ? – nawfal Oct 18 '18 at 18:19
  • Yea, you don't need to delete it. I posted this many years ago when I understood less about git. Canceling or doing a reset is probably what you want. – Eric Anderson Oct 18 '18 at 18:26
  • I have always used 'git merge foo --no-commit --no-ff' and then git merge --abort; git add .; git stash; git drop; :) – chrisg Nov 30 '18 at 00:07
59

If anyone is trying to generate a diff file from two branches :

git diff master..otherbranch > myDiffFile.diff
Paulino III
  • 1,826
  • 1
  • 15
  • 15
  • 2
    This has come in handy especially with large branches containing a lot of differences. – vandsh Apr 07 '15 at 18:07
  • This is useful when the difference is really large. By default it would not show all diff in the console (I was wondering why), passing the diff to a file is the way to go in such case. – rotimi-best May 03 '19 at 07:52
46

There is also a GUI based method.

You can use gitk.

  1. Run:

    $ gitk --all
    
  2. Right click on a commit of a branch and select Mark this commit in the pop-up menu.

  3. Right click on a commit of another branch and select Diff this -> marked commit or Diff marked commit -> this.

Then there will be a changed files list in the right bottom panel and diff details in the left bottom panel.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Yantao Xie
  • 12,300
  • 15
  • 49
  • 79
39

One more option, using meld in this case:

git difftool -d master otherbranch

This allows not only to see the differences between files, but also provides a easy way to point and click into a specific file.

rsilva4
  • 1,915
  • 1
  • 23
  • 39
29

Note that git makes it easy to just try out the merge and back away from any problems if you don't like the result. It might be easier than looking for potential problems in advance.

David Plumpton
  • 1,929
  • 23
  • 31
20

And if you are looking for changes only among certain file(s), then:

git diff branch1 branch2 -- myfile1.js myfile2.js

branch1 is optional and your current branch (the branch you are on) will be considered by default if branch1 is not provided. e.g:

git diff master -- controller/index.js
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
mannuscript
  • 4,711
  • 6
  • 26
  • 25
15

When working collaboratively, or on multiple features at once, it's common that the upstream or even your master contains work that is not included in your branch, and will incorrectly appear in basic diffs.

If your Upstream may have moved, you should do this:

git fetch
git diff origin/master...

Just using git diff master can include, or fail to include, relevant changes.

Alex Brown
  • 41,819
  • 10
  • 94
  • 108
11

If you are using IntelliJ IDEA, you can also compare any branch with your current working branch. See http://www.jetbrains.com/idea/webhelp/merging-deleting-and-comparing-branches.html#d288093e3827 for more info. This is available in the free edition as well.

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
9

There are plenty of answers here, but I wanted to add something that I commonly use. IF you are in one of the branches that you would like to compare I typically do one of the following. For the sake of this answer we will say that we are in our secondary branch. Depending on what view you need at the time will depend on which you choose, but most of the time I'm using the second option of the two. The first option may be handy if you are trying to revert back to an original copy -- either way, both get the job done!

This will compare master to the branch that we are in (which is secondary) and the original code will be the added lines and the new code will be considered the removed lines

git diff ..master

OR

This will also compare master to the branch that we are in (which is secondary) and the original code will be the old lines and the new code will be the new lines

git diff master..
Full Stack Alien
  • 11,244
  • 1
  • 24
  • 37
8

There are two branches lets say

  • A (Branch on which you are working)
  • B (Another branch with which you want to compare)

Being in branch A you can type

git diff --color B

then this will give you a output of

enter image description here

The important point about this is

  1. Text in green is inside present in Branch A

  2. Text in red is present in Branch B

selftaught91
  • 7,013
  • 3
  • 20
  • 26
2

If you are using Github / Github Enterprise, you can use the Web UI by hitting the url /compare of your repository path, for instance, https://github.com/http4s/http4s/compare. You can select the branch / commit / tag that you want to compare: Github Compare Screenshot

And the diff will be presented in the github interface at the url /compare/{x1}...{x2} where are x2 and x1 are the branch / commit / tag you want to compare, for instance: https://github.com/http4s/http4s/compare/main...dotty

You can see more in the Github Doc.

Valy Dia
  • 2,781
  • 2
  • 12
  • 32
1

OP wanted some other option, but probably it can be helpful to somebody: it is possible to see modified file list linked to commits

git log --name-status other-branch..

It is also works with commits

git log --name-status commit1..commit2
mechnicov
  • 12,025
  • 4
  • 33
  • 56
0
git diff revision_n revision_m

if revision_n and revision_m are successive commits then it outputs same as git show revision_m

Robert
  • 5,278
  • 43
  • 65
  • 115
Jovo Skorupan
  • 237
  • 2
  • 5
  • 4
    Perfect for my use case. I needed only the file names, so I used --name-only to get a list of changed files. `git diff --name-only rev_old rev_new` – Rahul Jawale Jan 15 '21 at 12:40
0

For people who are looking for a GUI solution, Git Cola has a very nice "Branch Diff Viewer (Diff -> Branches..).

kerner1000
  • 3,382
  • 1
  • 37
  • 57
0

I made a pipeline that outputs a list of all filenames that have been modified between 2 branches. This is useful for piping to another program, not for viewing the diffs directly as other responses.

yes n | git difftool main..develop | grep V |sed "s#Viewing ([0-9]*/[0-9]*): ##g"

Or, if you simply want the difference between the current branch and another one:

yes n | git difftool develop | grep V |sed "s#Viewing ([0-9]*/[0-9]*): ##g"

git difftool develop will prompt you for opening files in your difftool. I use yes n to refuse everything and then simply format the text with grep and sed.

  • Probably it's better to remove quotes in output – mechnicov Apr 19 '23 at 17:01
  • @mechnicov may I ask why? I prefer leaving the quotes just in case there is some badly named file with spaces or other weird characters. If you want to remove them, here's the pipeline: `yes n | git difftool develop | grep V |sed "s#Viewing ([0-9]*/[0-9]*): ##g" | sed "s/'//g" ` – Felipe Lorenzzon May 29 '23 at 12:36
  • It's easy to copy-paste file names without them – mechnicov May 29 '23 at 12:45
0

Only file paths without commit msg for parsing

All the other answers were giving me list of paths to files but also commit messages between file paths. Below are solutions that produce file paths but without commit msg. They are handy if you want to parse file paths later and don't care about the commit msg.

file paths prefixed with modification status

git log --name-status --pretty='' commit1..commit2

only file paths

git log --name-only --pretty='' commit1..commit2

only unique file paths

git log --name-only --pretty='' commit1..commit2 | sort -u
Jimmix
  • 5,644
  • 6
  • 44
  • 71
-1

You can also use a visual diff.

For example, if you are using Sourcetree, you can simply select any two commits in log view.

(I personally prefer using a GUI in most cases for this, and I'm posting this for those who may not be familiar with GUI options.)

sonnyb
  • 3,194
  • 2
  • 32
  • 42
-2

If you like GUI and are using Windows, here is an easy way.

  1. Download WinMerge
  2. Check out the two branches into different folders
  3. Do a folder by folder compare using WinMerge. You can also easily make modifications if one of the branches is the one you are working on.
Marius Matioc
  • 537
  • 6
  • 7
-3

You can also easily compare branches for changed files using for example TortoiseGit. Just click on Browse References and pick the branches you want to compare.

For example if you compare your branch with master you will get as a result list of files that will be changed in master if you decide to merge your-branch into master.

Remmber that you will have different result if you compare master with your-branch and your-branch with master.

Piotr
  • 9
  • 3