Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git How to: pull diff between branches

I would like to download the files that are the difference between two branches into a local folder. Is it possible with Git?

Given the files are only added to the source branch, they are not changed.

like image 309
Wojtek Turowicz Avatar asked Dec 12 '11 15:12

Wojtek Turowicz


People also ask

How do I compare changes in two branches in git?

How do I compare two different branches in my Git repository? Using git-diff you can compare two branches by viewing a diff between them, or you can use git-log to view a list of commits that comprise the difference between them. Compare two branches with git diff branch1.. branch2 .

Can we compare two branches in git?

Compare Branches in a Single Commandgit diff is a useful command that allows us to compare different types of git objects, such as files, commits, branches, and many more. This makes git diff a good choice when we need to compare the differences between two branches.

How do you compare two branches and codes?

To compare your currently checked out branch with other branches using Visual Studio, you can utilize the branch picker hosted in the status bar and the Git changes tool window to choose any local or remote branch to compare with. Right click the branch you are targeting and select Compare with Current Branch.

How do I run git diff?

How Do I Find the Difference Between Two Branches? For comparing two branches in Git, you simply run git diff <source-branch-name>.. <destination-branch-name> . Of course, you can replace the current branch name with HEAD.


2 Answers

You can get all changes between branches with something around these lines:

git diff origin/master origin/develop > my_diff.diff

If you only add [text] files, then it would be trivial to parse the diff file and break it into individual files. (I'd say, it's a ruby script under 50 lines of code)

like image 53
Sergio Tulentsev Avatar answered Sep 19 '22 11:09

Sergio Tulentsev


git archive --format=tar --prefix="exported/" -o export.tar br2 $(git diff --name-only br1 br2)

Assuming you're on br2 right now and br1 is lagging behind it, the part inside the brackets (git diff... )will give you the list of the files changed between the two heads. The git archive command will export these files as they are on br2 (i.e. on your current head) to a tar file called export.tar inside a directory called exported/.

This assumes (like you stated in your question) that you've only added new files and that all the diffs are adds. The command will also export modified files but you claim not to have any.

like image 40
Noufal Ibrahim Avatar answered Sep 20 '22 11:09

Noufal Ibrahim