johannix
johannix

Reputation: 29658

Showing which files have changed between two revisions

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:

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):

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

Upvotes: 2375

Views: 918083

Answers (21)

JasonSmith
JasonSmith

Reputation: 73752

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 are more options to git diff in the official documentation (and specifically the --name-status option).

Upvotes: 2926

Jimmix
Jimmix

Reputation: 6536

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

Upvotes: 0

mechnicov
mechnicov

Reputation: 15308

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

Upvotes: 1

Felipe Lorenzzon
Felipe Lorenzzon

Reputation: 21

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.

Upvotes: 0

Gerry
Gerry

Reputation: 11214

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

Upvotes: 448

Valy Dia
Valy Dia

Reputation: 2851

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.

Upvotes: 2

sonnyb
sonnyb

Reputation: 3514

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.)

Upvotes: -1

kerner1000
kerner1000

Reputation: 3558

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

Upvotes: 0

Jovo Skorupan
Jovo Skorupan

Reputation: 267

git diff revision_n revision_m

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

Upvotes: 0

Marius Matioc
Marius Matioc

Reputation: 557

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.

Upvotes: -2

Piotr
Piotr

Reputation: 9

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.

Upvotes: -3

Full Stack Alien
Full Stack Alien

Reputation: 12541

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..

Upvotes: 9

selftaught91
selftaught91

Reputation: 7471

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

Upvotes: 9

mannuscript
mannuscript

Reputation: 4951

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

Upvotes: 21

Alex Brown
Alex Brown

Reputation: 42912

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.

Upvotes: 15

Yantao Xie
Yantao Xie

Reputation: 12906

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.

Upvotes: 46

rsilva4
rsilva4

Reputation: 1955

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.

Upvotes: 40

Wim Deblauwe
Wim Deblauwe

Reputation: 26888

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.

Upvotes: 11

Paulino III
Paulino III

Reputation: 1862

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

git diff master..otherbranch > myDiffFile.diff

Upvotes: 61

Eric Anderson
Eric Anderson

Reputation: 3792

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.

Upvotes: 172

David Plumpton
David Plumpton

Reputation: 1927

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.

Upvotes: 29

Related Questions