Shibin
Shibin

Reputation: 248

Git - Selective Branching

I have a Git repository at github with files, file_1 and file_2 in the master branch. I need to have another branch 'selective' with only file_2 in it. What I did was that, I created a new branch with the checkout command. Then I deleted file_1 and committed. Now master branch has 2 files, file_1 and file_2 and selective branch only has file_2. I can specifically make changes and commit file_2 for both branches. I got what I wanted. Unless I merge selective with master, there is no problem whatsoever. Is this the way to do it? Or Is there any other better way available?

My aim is that I want my client to access only certain files in my codebase, i.e. my master branch. I need only want those files which I want the client to access in the other branch.

Thanks in advance

Upvotes: 12

Views: 447

Answers (2)

Nikhil Gupta
Nikhil Gupta

Reputation: 291

Use below code:

$ git checkout -b selective_branch
$ git branch
master
* selective_branch

$ git checkout master -- file_2
$ git commit -m "Update file_2 from master"

$ # make changes in file2 in master branch
$ git checkout master
$ git branch
* master
selective_branch
$ git status -s
M file_2
$ git add file_2

$ git commit -m 'Added abc function in file_2'
$ git push origin master


$ # make changes in file2 in selective branch
$ git checkout selective_branch
$ git branch
master
* selective_branch
$ git status -s
M file_2
$ git add file_2

$ git commit -m 'Added abc function in file_2'
$ git push origin selective_branch


$ # Now merge the changes from selective_branch to master branch. Switch the branch to master
$ git checkout master
$ git branch
* master
selective_branch
$ git merge origin/selective_branch
$ # After Merge Master and selective_branch will point to same Commit ID
$ # After testing you can push the changes to the master branch
$ git push origin master

Upvotes: 0

VonC
VonC

Reputation: 1324278

I would add to that setup a:

git checkout selective
git merge -s ours master

That will record a merge between selective and master (while retaining selective changes, here specifically the deletion of file1)

That will ensure that the next merge from master to selective will update only file2, and would not restore file1 in selective branch.

Upvotes: 10

Related Questions