user7836593
user7836593

Reputation: 1

Git diff to copy output by ignoring .git files

I am creating a Jenkins pipeline for which I have to clone two branches master and devlop branch to workspace into different directories and compare the files and pull only the changed files to a different directory. I am using the below command git diff --name-only GIT-develop-branch GIT-master-master-branch

How can I use the above command to copy and pasted the changed files from dir1 and dir2 to dir3 by ignore .git files. or else please share if there is any other way.

Upvotes: 0

Views: 243

Answers (1)

Mark Adelsberger
Mark Adelsberger

Reputation: 45649

I think you mean that master is assumed to be unchanged, and you want to copy the develop version to your third directory. (I'm not sure about that; asked for clarification in comments. If it's something different, I'll update once we have the clarification.)

Update for your comment below:

First, you didn't address the clarification I requested. So I am continuing to assume that you want the develop versions of the files that are different. It is still unclear what you want to do if a file is deleted or renamed; so the guidance I provided previously on that issue remains unchanged.

1) You said

i have cloned those 2 branches to separate directories in Jenkins work space

Yes, and as I said before, that doesn't seem like a good use of resources. As I said before, you probably don't need both checked out, and even if you do it would be more efficient to use git worktree instead of creating two complete repositories.

2) You said

as i have made or added few files in the directory in develop branch i want to only pull those files which i have changed instead of pulling everything else to the third directory

That is what the original answer does.

3) You said

and also i would like to pull all files only without any .git logs .

A checkout is always just the content. The history is in the repo database and is only duplicated if you make multiple clones (hence point 1 above).

4) You said

i am cloning master branch for comparison purpose only so that i can diff between two branches

Again, you don't need a clone to do a comparison. You don't even need a checked-out working copy. This is all covered in the original answer.


First thing, you can diff the branches without checking them both out. If you need the two working copies for something else, that's fine, but I still wouldn't use them for purposes of creating the diff. (Also, it sounds like you're making two clones of the whole repo; you might want to consider using git worktree instead, to attach multiple work trees to a single repo. But that doesn't have much to do with your question...)

git diff --name-only master develop

Depending on how you would want to handle a file being moved/renamed (or changes that git might mistakenly interpret as a file being moved/renamed) you might want to add the --no-renames option.

git diff --name-only --no-renames master develop

This would ensure that any path involved in a change is shown, whereas without this option if git thinks fileA was moved to fileB, only fileB will be included in the list. If you're just trying to get the changed subset of the develop directory, that might be what you want... but again, it depends what you're trying to do.

Related to that issue is the question of how you'd want to deal with files that are in master, but not in develop. In the above rename example, fileA would be such a file; but also you might have file5 which simply got deleted in the develop branch. While the --no-renames option (or lack thereof) controls whether fileA appears, file5 would appear in the list by default. You could suppress it; if you don't want to see filenames for deleted items (or "before" filenames for moved files), you can use

git diff --name-only --diff-filter d master develop

(If you try to combine --diff-filter d with --no-renames, the "before" name of a moved file will appear as a delete and will then be suppressed by the diff-filter option. So again, the point is you need to know what you want in your list.)

Now to copy the develop versions of the listed files into a work tree, you can just use git checkout; and you can tie it all together with xargs

git diff --name-only --diff-filter d master develop |xargs git checkout develop --

Upvotes: 1

Related Questions