diff --git a/dvc/scm.py b/dvc/scm.py
index e49039721..e32ed2913 100644
--- a/dvc/scm.py
+++ b/dvc/scm.py
@@ -151,7 +151,8 @@ class Git(Base):
 
     def _get_gitignore(self, path):
         assert os.path.isabs(path)
-        entry = os.path.basename(path)
+        # NOTE: using '/' prefix to make path unambiguous
+        entry = '/' + os.path.basename(path)
         gitignore = os.path.join(os.path.dirname(path), self.GITIGNORE)
 
         if not gitignore.startswith(self.root_dir):

gitignore: use unambiguous paths
E.g. `/dir` instead of `dir`.
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 4e46fcde0..228dbffc7 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -205,8 +205,8 @@ class TestGitIgnoreBasic(CheckoutBase):
 
         self.assertEqual(len(ignored), 2)
 
-        self.assertIn(fname1, ignored)
-        self.assertIn(fname2, ignored)
+        self.assertIn('/' + fname1, ignored)
+        self.assertIn('/' + fname2, ignored)
 
 
 class TestGitIgnoreWhenCheckout(CheckoutBase):
@@ -232,13 +232,13 @@ class TestGitIgnoreWhenCheckout(CheckoutBase):
         ignored = self.read_ignored()
 
         self.assertEqual(len(ignored), 1)
-        self.assertIn(fname_master, ignored)
+        self.assertIn('/' + fname_master, ignored)
 
         self.dvc.scm.checkout(branch_1)
         ret = main(['checkout'])
         self.assertEqual(ret, 0)
         ignored = self.read_ignored()
-        self.assertIn(fname_branch, ignored)
+        self.assertIn('/' + fname_branch, ignored)
 
 
 class TestCheckoutMissingMd5InStageFile(TestRepro):
diff --git a/tests/test_scm.py b/tests/test_scm.py
index 2e134c24c..fd24fbf11 100644
--- a/tests/test_scm.py
+++ b/tests/test_scm.py
@@ -45,7 +45,8 @@ class TestIgnore(TestGit):
     def _count_gitignore(self):
         with open(Git.GITIGNORE, 'r') as fd:
             lines = fd.readlines()
-            return len(list(filter(lambda x: x.strip() == 'foo', lines)))
+            return len(list(filter(lambda x: x.strip() == '/' + self.FOO,
+                                   lines)))
 
     def test(self):
         git = Git(self._root_dir)

pip install -e .[all]
parse_log_pytest
requirements.txt
pytest>=3.3.1 nose>=1.3.7 mock>=2.0.0 coverage>=4.5.1 codecov>=2.0.15 xmltodict>=0.11.0 awscli>=1.14.18 google-compute-engine Pygments collective.checkdocs
apt-get update apt-get install -y gcc
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
altgraph==0.17.4
asciimatics==1.15.0
awscli==1.38.23
azure-common==1.1.28
azure-nspkg==3.0.2
azure-storage-blob==1.3.0
azure-storage-common==1.3.0
azure-storage-nspkg==3.1.0
bcrypt==4.3.0
boto==2.49.0
boto3==1.7.4
botocore==1.10.84
cachetools==5.5.2
certifi==2025.1.31
cffi==1.17.1
charset-normalizer==3.4.1
codecov==2.1.13
collective.checkdocs==0.2
colorama==0.4.6
configobj==5.0.9
configparser==7.2.0
coverage==7.8.0
cryptography==44.0.2
decorator==5.2.1
distro==1.9.0
docutils==0.16
-e git+https://github.com/iterative/dvc.git@88ed542b94d5cc983511c16c89f479d938dd33a8#egg=dvc
exceptiongroup==1.2.2
future==1.0.0
gitdb==4.0.12
GitPython==3.1.44
google-api-core==1.34.1
google-auth==2.38.0
google-cloud-core==0.28.1
google-cloud-storage==1.13.0
google-compute-engine==2.8.13
google-crc32c==1.7.1
google-resumable-media==2.7.2
googleapis-common-protos==1.69.2
grandalf==0.6
idna==3.10
iniconfig==2.1.0
jmespath==0.10.0
jsonpath-rw==1.4.0
macholib==1.16.3
mock==5.2.0
nanotime==0.5.2
networkx==3.2.1
nose==1.3.7
packaging==24.2
paramiko==3.5.1
pefile==2024.8.26
pillow==11.1.0
pluggy==1.5.0
ply==3.11
protobuf==3.20.3
pyasn1==0.6.1
pyasn1_modules==0.4.2
pycparser==2.22
pydot==3.0.4
pyfiglet==1.0.2
Pygments==2.19.1
PyInstaller==3.3.1
PyNaCl==1.5.0
pyparsing==3.2.3
pytest==8.3.5
python-dateutil==2.9.0.post0
PyYAML==6.0.2
requests==2.32.3
rsa==4.7.2
s3transfer==0.1.13
schema==0.7.7
six==1.17.0
smmap==5.0.2
tomli==2.2.1
urllib3==1.26.20
wcwidth==0.2.13
xmltodict==0.14.2
zc.lockfile==3.0.post1

name: dvc
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - altgraph==0.17.4
      - asciimatics==1.15.0
      - awscli==1.38.23
      - azure-common==1.1.28
      - azure-nspkg==3.0.2
      - azure-storage-blob==1.3.0
      - azure-storage-common==1.3.0
      - azure-storage-nspkg==3.1.0
      - bcrypt==4.3.0
      - boto==2.49.0
      - boto3==1.7.4
      - botocore==1.10.84
      - cachetools==5.5.2
      - certifi==2025.1.31
      - cffi==1.17.1
      - charset-normalizer==3.4.1
      - codecov==2.1.13
      - collective-checkdocs==0.2
      - colorama==0.4.6
      - configobj==5.0.9
      - configparser==7.2.0
      - coverage==7.8.0
      - cryptography==44.0.2
      - decorator==5.2.1
      - distro==1.9.0
      - docutils==0.16
      - exceptiongroup==1.2.2
      - future==1.0.0
      - gitdb==4.0.12
      - gitpython==3.1.44
      - google-api-core==1.34.1
      - google-auth==2.38.0
      - google-cloud-core==0.28.1
      - google-cloud-storage==1.13.0
      - google-compute-engine==2.8.13
      - google-crc32c==1.7.1
      - google-resumable-media==2.7.2
      - googleapis-common-protos==1.69.2
      - grandalf==0.6
      - idna==3.10
      - iniconfig==2.1.0
      - jmespath==0.10.0
      - jsonpath-rw==1.4.0
      - macholib==1.16.3
      - mock==5.2.0
      - nanotime==0.5.2
      - networkx==3.2.1
      - nose==1.3.7
      - packaging==24.2
      - paramiko==3.5.1
      - pefile==2024.8.26
      - pillow==11.1.0
      - pluggy==1.5.0
      - ply==3.11
      - protobuf==3.20.3
      - pyasn1==0.6.1
      - pyasn1-modules==0.4.2
      - pycparser==2.22
      - pydot==3.0.4
      - pyfiglet==1.0.2
      - pygments==2.19.1
      - pyinstaller==3.3.1
      - pynacl==1.5.0
      - pyparsing==3.2.3
      - pytest==8.3.5
      - python-dateutil==2.9.0.post0
      - pyyaml==6.0.2
      - requests==2.32.3
      - rsa==4.7.2
      - s3transfer==0.1.13
      - schema==0.7.7
      - six==1.17.0
      - smmap==5.0.2
      - tomli==2.2.1
      - urllib3==1.26.20
      - wcwidth==0.2.13
      - xmltodict==0.14.2
      - zc-lockfile==3.0.post1
prefix: /opt/conda/envs/dvc

tests/test_checkout.py::TestGitIgnoreBasic::test tests/test_checkout.py::TestGitIgnoreWhenCheckout::test tests/test_scm.py::TestIgnore::test
tests/test_checkout.py::TestCheckoutMissingMd5InStageFile::test
tests/test_checkout.py::TestCheckout::test tests/test_checkout.py::TestCheckoutSingleStage::test tests/test_checkout.py::TestCheckoutCorruptedCacheFile::test tests/test_checkout.py::TestCheckoutCorruptedCacheDir::test tests/test_checkout.py::TestCmdCheckout::test tests/test_checkout.py::TestRemoveFilesWhenCheckout::test tests/test_checkout.py::TestCheckoutSelectiveRemove::test tests/test_checkout.py::TestCheckoutEmptyDir::test tests/test_checkout.py::TestCheckoutNotCachedFile::test tests/test_checkout.py::TestCheckoutWithDeps::test tests/test_scm.py::TestSCM::test_git tests/test_scm.py::TestSCM::test_none tests/test_scm.py::TestSCMGit::test_commit tests/test_scm.py::TestSCMGit::test_is_repo tests/test_scm.py::TestSCMGitSubmodule::test_commit_in_submodule tests/test_scm.py::TestSCMGitSubmodule::test_git_submodule tests/test_scm.py::TestSCMGitSubmodule::test_is_submodule
dvc/scm.py
dvc/scm.py