Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping two Git branches different by one file

Tags:

git

I am certain two branches of my Git repo should only ever be different in a single file. The simplest way to ensure this is to do all work in branch1, and merge it into branch2 whenever I switch to it (wrapped into a shell script, so I only need one command per switch). However, is there a way to do this with even less work?

like image 649
Alexey Romanov Avatar asked Oct 29 '10 09:10

Alexey Romanov


People also ask

How does git store different branches?

Git stores all references under the . git/refs folder and branches are stored in the directory . git/refs/heads. Since branch is a simple text file we can just create a file with the contents of a commit hash.

Can I work on 2 branches simultaneously?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.

Why should you have different branches in git?

Branching allows each developer to branch out from the original code base and isolate their work from others. It also helps Git to easily merge versions later on.


1 Answers

Yes, use a git filter driver, with a smudge script intelligent enough to:

alt text

  • recognize it is dealing with that single file (reminder: such a script only deals with file content, not filename)
  • put the right content depending on the current branch.

But the question is: do you need two branches at all?
If this is a config file, as mentioned in "Git: how maintain (mostly) parallel branches with only a few difference?", storing templates might be better. That same question proposes other alternatives.

like image 200
VonC Avatar answered Oct 09 '22 04:10

VonC