0

I have a master branch with 3 files:

feature1.txt
feature2.txt
feature3.txt

Now I create new branch call feature3 based on the master branch.

I will only work on file feature3.txt on this branch. I don't need the other 2 files. I want to remove them, but if I do, they will be removed from the master branch as well once I do a merge of the feature3 branch into the master branch.

What do I need to do to have these 2 files removed/hidden from branch feature3, without that the master branch is impacted?

Tjerk
  • 90
  • 6
  • Perhaps a `sparse-checkout`? https://git-scm.com/docs/git-sparse-checkout – eftshift0 Aug 04 '22 at 12:20
  • Your workflow seems strange. If I was working on `feature3` branch with the intention of updating `feature3.txt`, I would make the updates to that file and leave the others untouched. Why do you necessarily want to remove the other files on your branch? Typically, I will make commits where I update only a few files in my repo. I will leave the other 10000 files untouched. I do not delete them just because they are not related to my updates. – Alderath Aug 04 '22 at 14:31

3 Answers3

2

You can‘t create a branch for a single file in Git. When you create a branch, you create a branch for the entire repo (like a copy from the repo).

It is quite normal that not all available files in a branch are always processed. You only change those files that are necessary to complete the activities in this branch. So I see no problem that the other two files (feature1.txt and feature2.txt) are in this branch, although these are not changed.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
1

As long as you don't commit the removal of files, you won't impact your master branch. If you would prefer not to see the deleted files showing as needing to be committed in git status, you can run the following:

git update-index --assume-unchanged  path/to/file

This causes git ignore all changes (including a deletion) for a given file or files. Unfortunately, this approach is not limited to a single branch so you'll need to revert this setting when you switch to a different branch:

git update-index --no-assume-unchanged path/to/file

There are some useful git aliases for managing files that are flagged in this way detailed elsewhere on StackOverflow.

Brian Phillips
  • 12,693
  • 3
  • 29
  • 26
0

I can understand that it might be useful to work on a branch with just a few files, or perhaps missing some specific files.

One way to do this uses cherry-pick rather than merge

git checkout -b feature3
rm feature1.txt feature2.txt
git commit -am "Commit removing temporarily unwanted files"
git tag start-point
#
# develop feature3.txt committing as often as is needed
#
git commit -am "Development of feature3 complete"
#
# Perhaps you want to rebase on master at this point?
#
git switch master
git cherry-pick start-point..feature3
# Perhaps you will have conflicts to resolve?
#
# Note that the unwanted files are back
# git log will not show any activity for them
#
# We are done - just tidy up
git branch -D feature3  # git doesn't know it's fully merged
git tag --delete start-point
mikado
  • 201
  • 1
  • 8