Home > Mobile >  git pull and skip file that I already have locally
git pull and skip file that I already have locally

Time:09-16

Say I have the below setup

mkdir repo1
touch repo1/file{1,2}
cd repo1
git init
git add *
git commit -am first
cd ..

mkdir repo2
cp repo1/file1 repo2/ # <<< important
cd repo2
git init
git remote add origin ../repo1

Now if I run the below git pull in repo2

git pull origin master

I get the error:

From ../repo1
 * branch            master     -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
    file1
Please move or remove them before you merge.
Aborting

I want to tell git to just use the file1 copy in repo2 that I've already downloaded instead of getting it again. I already know that git uses hardlinks to avoid duplicating files when cloning locally to save disk space. My situation is that I've already performed the copy and don't want to delete repo2/file1 to keep going.

CodePudding user response:

Since they seem to be two different repositories, probably, if the file it's untracked, you first have to commit it, and then, manage eventually the merge.

CodePudding user response:

So based on @RobyB, I just committed the file and then let the merge figure out that the file already exists.

cd repo2
git add file1
git commit -am 'add file1'

git pull origin master --allow-unrelated-histories

At this stage, the git log looks a bit different, but a squash would do:

# git log
commit eec8 (HEAD -> master)
Merge: 61be626 beeefd6
Date:   Wed Sep 15 09:23:40 2021 -0400

    Merge branch 'master' of ../repo1

commit 61be62
Date:   Wed Sep 15 09:23:13 2021 -0400

    add file1

commit beeef (origin/master)
Date:   Wed Sep 15 09:09:58 2021 -0400

    first
  •  Tags:  
  • git
  • Related