SSH git — How to pull a folder from repo, but not delete other directories & files on deployment server [closed]

Enter my question: When I SSH into the Linux web server where my website is hosted, how do I perform a git pull origin dev so that git doesn’t delete my entire WordPress site, replacing it with only the folders/files in the repo?

git pull origin dev

git pull does not erase or replace untracked files.

Proof

For example, here is an example git repo with a README.md: https://github.com/KalobTaulien/example-repo

Any repository will do however, so lets do the following:

  • clone the repository into a folder
  • create an untracked file in that folder
  • run git pull

If you are correct, the untracked file will be deleted.

This is the result:

~
❯ cd /tmp
/tmp 
❯ git clone https://github.com/KalobTaulien/example-repo
Cloning into 'example-repo'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 18 (delta 1), reused 1 (delta 0), pack-reused 12
Receiving objects: 100% (18/18), done.
Resolving deltas: 100% (3/3), done.
/tmp 
❯ cd example-repo/
/tmp/example-repo ᚴ:master 
❯ touch test.txt
/tmp/example-repo ᚴ:master 
❯ ls
README.md test.txt
/tmp/example-repo ᚴ:master 
❯ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    test.txt

nothing added to commit but untracked files present (use "git add" to track)
/tmp/example-repo ᚴ:master 
❯ git pull origin master
From https://github.com/KalobTaulien/example-repo
 * branch            master     -> FETCH_HEAD
Already up to date.
/tmp/example-repo ᚴ:master 
❯ ls
README.md test.txt

As you can see, test.txt was not erased.

In Conclusion

How do you pull files down without erasing files and folders not tracked by git in the same folder? git pull. git pull does not erase files and folders not tracked by git.

Your theory that git pull is responsible is incorrect.

Why?

git pull is shorthand for these commands:

git fetch
git merge FETCH_HEAD

Neither of those commands touch untracked files. fetch retrieves information about the remote branch. merge applies new commits to the current working directory.

As for why your WordPress files and folders are deleted, we don’t have enough information to reproduce the problem or diagnose the cause. Being able to see the git repo itself, and all of the commands used, might help diagnose the problem.

For example, it may be that before pulling, your scripts do a hard reset and clean. Or that you aren’t doing the git pull at all, but rather a tool is doing it. It could also be possible that the git repository has script hooks that run on pull that run additional commands that you have not told us about.

However this is not a WordPress problem, it’s a git question. You should ask about this on Stack Overflow.