Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create branch-specific files on GitHub

I would like to have branch-specific files with the same name. I need to be able to merge from my development branch to master without changes made in this file.

For example:
Let's assume that I would like to have two different readme.md files. In one I would like to have content: MASTER and in another DEV. But if I try to do it, while creating pull-request GitHub will try to merge this file, which is exactly my problem. I don't want GitHub to merge this file each time I make changes.

What would be the best way to solve this problem?

like image 238
Wojciech Kulik Avatar asked Oct 11 '15 16:10

Wojciech Kulik


People also ask

How do I create a specific branch in GitHub?

You can clone a specific branch from a Git repository using the git clone –single-branch –branch command. This command retrieves all the files and metadata associated with one branch. To retrieve other branches, you'll need to fetch them later on.

Can we create branch from specific commit?

First, checkout the branch that you want to take the specific commit to make a new branch. Then look at the toolbar, select Repository > Branch ... the shortcut is Command + Shift + B. And select the specific commit you want to take. And give a new branch name then create a branch!


1 Answers

Let's say project is a name of the branch to be merged, and README.md is a name of the file that keep branch specific information.

I would suggest the following steps:

  1. Merge project branch, but make sure changes are not committed, and not fast-forwarded

    $ git merge --no-commit --no-ff project
    
  2. Unstage README.md file and checkout its current branch version

    $ git checkout HEAD -- README.md
    
  3. Complete merge

    $ git commit
    

Also, it makes sense to install merge driver that will keep branch specific version of a file in case of merge conflict. In such case, you will never need to resolve conflicts in branch specific files manually.

Such merge driver is usually called ours and defined as:

$ git config --global merge.ours.driver true

Now, you can specify in .gitattributes file, when this merger should be used.

In our case, it is needed to add the following rule to .gitattributes and commit it:

README.md merge=ours

like image 76
Stas Avatar answered Oct 20 '22 05:10

Stas