Search code examples
gitgit-mergegit-reset

git - start tracking untracked remote files/directories


Let say I do this:

mkdir some_repo
cd some_repo
git init
git touch dummy.txt
git commit -m 'dummy commit'
git remote add -f some_other_remote_repo path/to/.git
git merge --no-ff --no-commit merge remotes/some_other_remote_repo/master

No entering git status I see something like:

new: f1/some.txt
new: f1/some11.txt
new: f2/some2.txt
new: f3/some3.txt

So now I only want to add specific directories and their contents. I can do this:

git reset f2/
git reset f3/

now running git status:

new: f1/some.txt
new: f1/some11.txt

untracked: f2/some2.txt
untracked: f3/some3.txt

So it does what I need and I can commit and remove locally added f2 and f3 directories. But what to do if later I decide I now want to track for example f2 directory and its contents?

No if I run git merge --no-ff --no-commit merge remotes/some_other_remote_repo/master again, it will say everything is up to date. So it now ignores those untracked directories. Is their a way to tell git to track specific directories that are in remote only?


Solution

  • Solution

    Merge the remote branch again. This pulls in the files (again).

    Disclaimer

    I consider this very bad practice. After your first merge with deleted files, you have a successful merge in your git history - however, this is clearly not the case.

    Recommendation

    Instead, you should create a new branch some_feature on top of remote/master, delete those files, and then merge some_feature. If you decide you want all files, fully merge remote/master.

    If the files some.txt, some1.txt, … are all added in different commits, you may also cherry-pick those commits if they are only a few.