To run git pull on a server over SSH safely, use a read-only deploy key, check with git status that the working tree is clean, update with git pull --ff-only so Git refuses any unexpected merge, and finish with systemctl restart followed by systemctl status. Four steps, always in that order.
Deploy keys for git pull on a server: one key per repository
A deploy key is an SSH key attached to a single repository on GitHub or GitLab, and read-only access is all the server needs to run git pull and nothing else. If the machine is compromised, the attacker does not get your personal account or your other repositories.
Generate the key on the server, as the user that runs the application, and use Ed25519, supported by OpenSSH since version 6.5:
ssh-keygen -t ed25519 -f ~/.ssh/deploy_myapp -C "deploy myapp" -N ""
chmod 700 ~/.ssh
chmod 600 ~/.ssh/deploy_myapp
cat ~/.ssh/deploy_myapp.pubPaste the public key into the repository settings as a deploy key, without write access. Then add a host alias in ~/.ssh/config so Git uses that key and no other:
Host github-myapp
HostName github.com
User git
IdentityFile ~/.ssh/deploy_myapp
IdentitiesOnly yescd /srv/myapp
git remote set-url origin git@github-myapp:myorg/myapp.git
ssh -T git@github-myappIdentitiesOnly yes stops ssh from offering other keys loaded in the agent. If the server hosts several projects, repeat the block with one alias and one key per repository: each deploy key is bound to a single repo.
git status before git pull: what to check on the server
git status must confirm there are no local changes before any git pull; if someone hand-edited a file in production, you want to know before Git fails halfway.
cd /srv/myapp
git fetch origin
git status
git log --oneline HEAD..@{u}
git diff --stat HEAD @{u}What to look for in the output:
- Right branch: “On branch main”, or whatever your production branch is.
- Clean tree: “nothing to commit, working tree clean”. If modified files show up, inspect them with
git difffirst. - Only behind: “Your branch is behind”. If it says “have diverged”, someone committed directly on the server.
- Expected commits:
git log HEAD..@{u}lists exactly what you are about to deploy.
Write down the current commit too; it is your way back if something breaks:
git rev-parse --short HEADgit pull --ff-only in production: why it refuses merges
git pull --ff-only only updates when the local branch can move straight ahead to the remote one; if histories have diverged, it aborts without creating a merge commit or leaving conflicts on disk. On a server that is exactly what you want: either the code matches the repository, or nothing changes.
git pull --ff-only
# Make it the default for this repository:
git config pull.ff onlyIf it fails with “Not possible to fast-forward”, do not force it. Go back to git status, find the stray local commit and resolve the divergence on your development machine, not in production. Dependency installs and migrations go after the pull and before the restart, using whatever your project needs (npm ci, pip install -r requirements.txt…).
systemctl restart after git pull: restart and verify the service
systemctl restart stops and starts the unit again, so the new code is not live until you run it and confirm the service is still active.
sudo systemctl restart myapp.service
systemctl status myapp.service --no-pager
journalctl -u myapp.service -n 50 --no-pagerIf the unit supports reloading, sudo systemctl reload-or-restart myapp.service is gentler; the systemctl manual page explains the difference. If the service fails to start, return to the commit you noted:
git reset --hard abc1234
sudo systemctl restart myapp.serviceRun the whole sequence inside tmux (tmux new -s deploy to start, tmux attach -t deploy to come back) and a dropped connection will not leave the deploy half done. The official tmux repository documents the remaining options.
Manual SSH deploy from a phone versus a desktop
OpenSSH listens on TCP port 22 by default, and the server is the same whether you connect from a laptop or a phone; what changes is the client environment.
| What is measured | iOS/Android with termi1 | Desktop |
|---|---|---|
| Network change during the pull | Mosh keeps the session when moving from Wi-Fi to mobile data | Plain SSH drops; tmux keeps the process alive |
| Ctrl, Esc, Tab and arrows | Custom keyboard with those keys and an fn layer with F1–F12 | Full physical keyboard |
| Inspecting a changed file | Built-in SFTP without leaving the session | Separate SFTP client or scp |
| Where your login private key lives | System secure storage; never leaves the device | File in ~/.ssh with 600 permissions |
| Typing long commands | AI types it at the prompt without pressing Enter | Shell history or copy and paste |
With a mobile SSH and Mosh terminal like termi1, a typed or dictated phrase such as “update the repo without merges” becomes git pull --ff-only at the prompt, with no Enter. Tag the connection as production and Production Guard ensures nothing gets Enter automatically, holding back anything that deletes, stops or changes something, such as git reset --hard or systemctl restart, before it reaches the prompt. Mosh needs UDP open, ports 60000-61000 by default; if the server has no public IP, a private mesh network without ports such as Prialo or Tailscale gets you there without opening anything.
Frequently asked questions
What is the difference between git pull and git pull --ff-only?
Plain git pull can create a merge commit or leave conflicts when the local and remote branches have diverged. git pull --ff-only only moves the branch when a straight fast-forward is possible and otherwise aborts without touching the working tree, which avoids half-finished states on a server.
Is a passphrase-less deploy key on the server safe?
A passphrase-less deploy key on the server is acceptable when it is read-only and limited to one repository. Anyone who compromises the machine can only read code that is already on its disk. Keep the key at 600 permissions and the ~/.ssh directory at 700.
Why run git status before git pull in production?
Running git status before git pull reveals local changes, the active branch and whether history has diverged from the remote. Without that check, a file edited by hand in production can block the update or get lost while you rush to fix the problem.
How do I undo a git pull that broke the service?
To undo a git pull that broke the service, run git reset --hard with the commit you noted before updating, then sudo systemctl restart the unit. Confirm with systemctl status and journalctl that the service is active again and no longer logging errors.
Can I deploy with git pull from my phone?
Deploying with git pull from a phone works exactly like it does from a desktop, because it is the same SSH session. You want a client with Ctrl, Esc and Tab keys, Mosh to survive switching from Wi-Fi to mobile data, and tmux on the server so the process continues if the connection drops.