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

I'm at a point in my git education where I've refined my .gitignore file such that when I push from my local development machine to a remote repository (which happens to be on Azure, but it could just as easily be on github), the folders and files in the repo are precisely how I want them to be. That is:

  • wp-content/plugins/my-custom-plugins
  • wp-content/themes/my-custom-theme

...and that's basically it. I don't want the wp-config in the mix, or any of the stock WP folders like wp-includes or wp-admin. No cache, no wp-content/uploads, etc.

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?

What I've Tried: First of all, when I perform git pull origin dev, the operation does, indeed, wipe out all of my WordPress files and leave only the pulled repo. Unacceptable, to say the least.

Since this is the development server, I can afford to play around with it and get it wrong, because I can always FTP the website back to the server. FTP'ing is very time consuming, so I don't want to get this wrong too many times. And when it comes time to do it on the production server, I cannot get it wrong even once.

I am answering the question myself, because for some reason it is attracting other comments and answers that are unrelated to the original question. I guess you can have a high rep and still have low reading comprehension.

Topic ssh git Wordpress

Category Web


A wordpress.stackexchange moderator closing a question after he answers it? Nobody else sees the self-serving conflict of interest? Hmmm...

Tom J. Nowell's comments and answer completely misstate the problem, and his revenge downvoting, then answering the question, and then CLOSING the question should be beneath the dignity of a moderator on this site. Such behavior does nothing to edify credibility. I suppose now I will be subject to retribution on any other question I post or respond to on this site.

The other answer posted by a user with a higher rep than mine, although long-winded, actually does not address my original question.

For a better answer than that given, refer to the answer marked as correct here: SSH git — How to pull a folder from repo, but not delete other directories & files on deployment server

So, in the interest of maintaining focus on the actual question asked, I will answer my own question. I don't think this is a perfect answer, and I invite better answers.

What I've Tried: First of all, when I perform git pull origin dev, the operation does, indeed, wipe out all of my WordPress files and leave only the pulled repo. Unacceptable, to say the least.

  • Repo comprises the wp-content folder, and only the wp-content folder
  • SSH into my remote web folder root.
  • git pull origin dev

Result: My WordPress installation is wiped out, leaving only the wp-content folder with wp-content/plugins/{custom_plugins} and wp-content/themes/{active_theme}.

THAT is the problem.

The solution (really, my solution of the moment) is to create a temp folder in my webroot, and pull the repo into the temp folder, and then use a bash command like mv -u (move only updated files) or cp to move or copy the new repo files from the temp folder to the working folder. This preserves all the other WP folders and files.

Another answer:

  • I created a folder called "test" on the web server.
  • Using ssh and bash, I cp -r (copied) all WP from my remote's web folder to an empty "test" folder.
  • I wasn't allowed to do a pull from the repo until I did a git pull origin dev --allow-unrelated-histories

Now, my web folder still has all my WP files and folders, with the custom plugins in wp-content/plugins and custom theme in wp-content/themes correctly overwritten by the repo files.


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.