Download changed files between 2 branches

Hi

I am trying to find a way to download only changes files between 2 branches, eg master and a release branch.

I can use the compare option in the portal to see the file diffs and also run the diff command in my PowerShell terminal, which lists the changes, but I can’t find a way to download the changed files.

Our code is for SQL server and we are only just starting to use Git, We don’t have CI/CD implemented and it’s planned for a while yet as we have a limited resource/knowledge.

Currently we have all our SQL scripts (tables, procs, views etc) checked in and I can download the entire code base easily enough. However, I can’t deploy every object into SQL as this introduces huge risk, I only want the scripts that have changes between the master branch and the release branch.

I have exhausted google and even ChatGPT for answers but can’t find anything other than downloading both branches and writing some PowerShell to compare the files or using a 3rd party tool, and the copy the changed files to another location.

It feels like there must be an easier way. Any help greatly appreciated. Thanks!

Hmm… I don’t think this is possible purely with git, but you’ll have to make your own thing. At least that’s what I would do xD

The question is where are your files now (in GitLab?) and where are you trying to download them (local laptop, server…)? And also - do you want the version from master or release branch then?

Also I’m asking myself, why changed files? Are those branches always completely different, or do you merge one into another at some point? Git normally will only download the new stuff anyway.

But, to get to your pure question…

I guess there might be two approaches, depending on how you want to do:
a) execute a script on GitLab CI and copy over files to your destination; or
b) execute a script directly on target destination… but then you don’t need to download as git will have them already via checkout & pull?

But, in any case, you need this “execute a script” part, in which “script” would consist of following steps:

  1. determining the files that have changed: after fetching all changes (git fetch), you could use tools like git diff or git rev-parse in combination with maybe awk or sed (if needed) to get a nice clean list of changed files (with included folders if they are not in root folder).

  2. Looping through that list and adding a URL in front of it that could be used to download… e.g. both GitLab and GitHub have a “raw” option for file download so maybe you could leverage that to craft a proper URL. Example:
    This file (.gitlab-ci.yml) on master over UI: https://gitlab.com/gitlab-org/gitlab/-/blob/master/.gitlab-ci.yml
    The same file in RAW: https://gitlab.com/gitlab-org/gitlab/-/raw/master/.gitlab-ci.yml

  3. Download with e.g. curl the URLs that you’ve crafted… Example:

curl https://gitlab.com/gitlab-org/gitlab/-/raw/master/.gitlab-ci.yml > .gitlab-ci.yml

Ofc, this is simplified case, no authentication, etc… but you get the point :smiley:

Hope this helps or at least gives you some inspo! Btw of course instead of pure bash stuff you can also use Go, Python… I believe it should be possible with any scripting language.

1 Like