0

My project use git in the following manner :

  1. master => production
  2. staging => staging
  3. feature/* => feature branches

I have a some translation files (en_us.ids.json) with ids on them to allow quick editing from the browser. Those file are only relevant in feature branches, cause our QA changes the translations if needed on those branches, but those files should be ignore/delete on master and staging.

is there a simple way to remove each file names (*.ids.json) from my repository when merging to staging or master while keep the tracking of those file in branches that are feature/* ?

This should work on a pipelines, and not just on local, so editing .git folder would not really work.

Crocsx
  • 2,534
  • 1
  • 28
  • 50

1 Answers1

1

This is impossible in general. Files, in Git, are not stored in branches. Files are stored in commits. A branch name is a way Git lets you find one specific commit; that one commit then lets you and Git find earlier commit(s).

To see why it's impossible, consider the following repository that has just one branch named main. This branch name selects the third of the three commits that are in this repository:

A <-B <-C   <--main

This drawing represents the idea that name main identifies (or "points to") commit C, with C standing in for the actual hash ID of that third commit. Meanwhile commit C points to—or more precisely, stores in its metadata the hash ID of—earlier commit B; commit B points to commit A. As commit A was the very first commit, it does not point any further back: those are all the commits in the repository.

Let's now create two new branch names, staging and feature/one:

A <-B <-C   <--feature/one, main, staging

All three names select commit C. This means that the snapshot stored in commit C is what you will see regardless of which branch name you choose.

You can—easily—remove a file and commit. Suppose we use:

git switch feature/one

so that the current branch name is feature/one. We then remove somefile and commit. This makes a new commit D, and updates the name feature/one to point to new commit D:

A--B--C   <-- main, staging
       \
        D   <-- feature/one (HEAD)

(The name HEAD in parentheses indicates the current branch name, in these drawings.) If we switch back to main or staging:

git switch main

we get:

A--B--C   <-- main (HEAD), staging
       \
        D   <-- feature/one

Commit D still exists, and the name feature/one still points to it; the file we removed is still absent from the snapshot in D, and will be forever, since all commits are 100% read-only. The file we removed before making commit D is, however, still here in commit C. When we switched from commit D back to existing commit C, Git removed all the commit-D files and installed, instead, all the commit-C files.1


1This glosses over a bunch of special cases described in Checkout another branch when there are uncommitted changes on the current branch, but is good as a starting mental model: your working tree is where Git extracts the committed files, so that you can see and work on / with them. Switching commits requires removing and replacing some of these files. Note that a branch-change that doesn't switch commits, however, never requires touching any files. As long as two names both select the same commit, you can switch back and forth without consequence.


is there a simple way to remove each file names (*.ids.json) from my repository when merging to staging or master while keep the tracking of those file in branches that are feature/*?

No. It's easy enough to do manually, but to automate that, you must have your merge process do it:

This should work on pipelines ...

Git doesn't have anything called "pipelines". Other, non-Git software that does have pipelines, such as Jenkins or GitHub CI, may or may not let you do what you'd do manually. Note that each of these add-ons has something rather different, that they tend to call a "pipeline" or similar word; you've asked in a Git forum and it's impossible to guess which system you're talking about.

torek
  • 448,244
  • 59
  • 642
  • 775
  • this is a very complete explanation thank you. For the answer, I guess I should use something else than git to achieve this then ? The problem is, does PR conflict with the main branch just because of this files that I would ignore during the merge process. So no pipeline (I use bitbucket) run to delete those files – Crocsx Mar 14 '22 at 01:48
  • A bitbucket pipeline might be able to do what you want, but I haven't used Bitbucket much at all, and never used their pipeline feature myself. – torek Mar 14 '22 at 07:10