How to compare GIT and server files
Challenge: detect file changes in a cloned repository on the server
Solution: use git clean and git diff
It’s a common scenario to use git clone or git init to initialize the website on the server. Nowadays, hostings offer access to an SSH connection and GIT is already installed. Programmers work locally on changes, then commit and push files to the main repository. One command – git pull – will update production files instantly.
Files edited manually
GIT is awesome and improves old workflow where FTP was used to move files between the local drive and hosting server. However, sometimes even GIT causes problems. Imagine a situation where we have cloned repository on production and also FTP access. One day, we noticed that some files had been edited manually via FTP.
We have a problem. The files were overwritten manually, and GIT files do not match anymore. The reason for this might be different:
- The webmaster with FTP access just didn’t know about the GIT repo. He needed to change footer links and edited files directly on production.
- Or a more serious situation where a malicious PHP script infected our website. File changes with javascript redirection to the infected website were applied.
Brute-force Solution
The simplest solution for reverting the changes that comes to mind is to:
- remove all old files
- and clone a fresh repository again
Sure, it will work. We know that the original files with the newest changes are stored in our GIT repository. Cloning files again will resolve the issue. The only downside is that we will not know what changes were applied on the server.
The state of the cloned repo
First, it would be good to know what was happening and what changes were made on production. My personal favorites are two git commands: git clean and git diff. Running them in the SSH console can tell you a lot. Using these 2 commands can show us the “current state” of the repository: the list of all local changes compared to the master branch. Let’s explore the details. See new files.
To display file names that are NOT under version control:
git clean -fdxn
Make sure to use the -n option. It’s dry-run. Untracked files will not be removed, but displayed in the console. The Clean command is really handy. You will see all new files added, you can review them and delete manually.
Compare GIT files with server files
If you would like to see changes in the tracked files (files added in GIT) – git diff is the command to use.
To display all changes in the GIT files just use:
git diff .
Make sure to add a single dot at the end. The output will display the list of all changed files and even code changes line-by-line. Here is a screenshot result that shows the differences between local changes and changes committed to GIT:
Summary
It’s good to track file changes. GIT is really good at it. If some unintentional changes are made on the server, we can always investigate the problem. And with the help of GIT commands, we can track all changes that have been made directly using FTP.
Keep in mind that, the GIT commands presented here display only new files / content changes. Your job is to decide what to do with that. Remove files manually or initialize a fresh repository again – these are some of the possible options.
Overwrite changes
If you feel comfortable with GIT and know what you’re doing, you can overwrite all local changes. Local changes meaning uncommitted changes. This will do the job:
git reset --hard
git pull
For untracked local files (files that do not exist in GIT) we can use git clean with proper arguments. The following command will remove untracked files and directories:
git clean -df
That’s it for today’s tutorial, be sure to subscribe to our newsletter to stay up-to-date with other tips and guidelines.