2

I have a website on my ubuntu server. I normally write the code on my laptop and then copy it over using scp. I decided I could just push to a git repository on my laptop and pull it on my server. My git repository on both machines is in /var/www/html (being served by apache2).

The problem:

On my server, I have a symbolic link from /home/files to /var/www/html/files. I also have a file /var/www/html/images/image.png. This file and symbolic link don't exist on my laptop, so when I do a git push from the laptop, they don't get added to the repository.

I want:

to do a git pull on my server and have it replace all the the current webpages such as /var/www/html/home.php with the new version from the repository, while not changing the local files that don't exist in the repository such as /var/www/html/files/* and /var/www/html/images/image.png

When I do a git pull on my server, git seems to replace the parent directory of /var/www/html/images/image.png (/var/www/html/images) and so I can't find /var/www/html/image.png

anymore.

What I did (on the laptop):

$ nano /var/www/html/home.php (update the webpage)

$ git push

What I did (on the server):

$ ln -s /home/files /var/www/html/files 
$ cp /home/some/random/image.png /var/www/html/images/image.png
$ git pull

I expected: to find a new version of /var/www/html/home.php and to still find the old /var/www/html/images/image.png

Instead: I found the new version of /var/www/html/home.php but no /var/www/html/images/image.png

None
  • 21
  • 1
  • See [this question](https://stackoverflow.com/questions/57178348/how-to-not-to-override-changes-in-specific-directory-when-pushing-to-git-reposit/57178728#57178728). – Calum Halpin Jul 28 '19 at 12:20

2 Answers2

0

As Calum Halpin points links this answer, you can easily create a file named .gitignore in the root of your repository that contains images/image.png. With this, git does not track the image and won't delete it on your server.

You can also write a script that automatically pulls and creates the Symlink.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Okay, so you are saying that If I gitignore images/image.png, I can do a git pull and it will overwrite home.php but not images/image.png? What command can I use to make git overwrite current files with the newer version from the repository? – None Jul 29 '19 at 10:44
  • Git pull overwrites the files that changed with the newer version from the repository. After you added images/image.php git will ignore the image and won't overwrite it. So, the command is `git pull`. – dan1st Jul 29 '19 at 13:14
0

I found it: I had to add the files I want to remain untouched (namely images/image.png) to the gitignore file, then I did git fetch --all and then git reset --hard origin/master. This will update (and overwrite) all files in current directory to their new state on the remote repository (yes, this will overwrite any new changes you made, so remember to git stash if you don't want to lose your work). At the same time, any files in your .gitignore file will remain untouched :-). I wrote a little script: git fetch --all && git reset --hard origin/master.

Sources: The first answer here: How do I force "git pull" to overwrite local files?

None
  • 21
  • 1