DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
VGSources
Blog

Essential Git Commands to Use on Your Linux VPS

A production-safe Git workflow for Linux VPS operators, covering repository checks, SSH or HTTPS remotes, fetch versus pull, merge and rebase policies, push errors, release tags, recovery, and deployment checklists.
Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The safest Git routine on a Linux VPS is: verify the directory, branch, and remote; fetch before integrating; update with an explicit fast-forward, merge, or rebase policy; review staged changes; and record the exact commit you deploy. The commands below cover the complete day-to-day workflow, from the first clone through rollback, conflict recovery, tags, and remote cleanup.

1. Verify where you are before changing anything

A VPS may contain several applications, staging checkouts, and release directories. Git operates on whichever repository the shell is currently inside, so begin every maintenance session by checking location and state.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
pwd
git status --short --branch
git branch --show-current
git remote -v
  • pwd confirms the absolute path. Make sure it is the intended application checkout, not a similarly named staging or backup directory.
  • git status --short --branch shows the current branch and whether files are modified, staged, or untracked. A clean result is easiest to reason about; if it is not clean, understand every change before pulling.
  • git branch --show-current prints the branch that would receive an update. Production should remain on the branch your deployment process expects.
  • git remote -v displays the fetch and push URLs. Confirm the repository and host before you download or publish anything.

Do not proceed merely because a command succeeds. A successful pull in the wrong directory or on the wrong branch can still deploy the wrong code.

2. Clone a repository or correct its remote

Clone into the application path

git clone [email protected]:ORG/REPO.git /srv/app
cd /srv/app

The final argument to git clone is the target directory. Choose a path owned by the deployment account rather than routinely operating as root.

#1 Best Overall

Add or replace a remote in an existing checkout

git remote add origin [email protected]:ORG/REPO.git
git remote set-url origin [email protected]:ORG/REPO.git

origin is the conventional remote name, but the name itself is not special. SSH remotes require a key pair and repository permission for the account running Git. HTTPS can be appropriate where outbound SSH is blocked or where an organization mandates an HTTPS credential policy. The unauthenticated git:// protocol should not be used for sensitive operations.

GitHub documents SSH URLs both for cloning and for securely deploying code to production servers. Keep the remote in the checkout that actually runs the service, and verify it again immediately before a release.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Inspect branches and recent history

git branch -a
git switch main
git switch -c feature/name
git log --oneline --decorate --graph -n 20
git show --stat --oneline HEAD

Choose the intended branch

git branch -a lists local and remote-tracking branches. Switch a production checkout explicitly:

git switch main

Use the branch name required by your deployment process; it may be main, a release branch, or another project-specific name. For isolated work, git switch -c feature/name creates and checks out a new branch in one operation. Avoid creating feature branches inside a production checkout unless that is deliberately part of your deployment design.

Read what is about to be deployed

git log --oneline --decorate --graph -n 20 gives a compact view of recent ancestry and branch pointers. git show --stat --oneline HEAD summarizes the current commit and the paths it changed. These checks help you identify an unexpected branch or a release that already contains changes you did not intend to run.

4. Stage and commit only deliberate changes

git diff
git add path/to/file
git diff --cached
git commit -m "Describe the change"

Review unstaged work

git diff shows edits that are present in the working tree but are not staged. On a VPS, those edits may be emergency diagnostics, generated files, or secrets accidentally written into the checkout. Identify them before any integration command.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create a precise snapshot

Use git add path/to/file to stage selected paths instead of automatically adding everything. Then run git diff --cached. This is the exact snapshot the next commit will contain, so check configuration files, credentials, generated assets, and unrelated edits one last time. A commit records the staged snapshot; it does not automatically include every modified file in the directory.

Write a message that identifies the change, such as git commit -m "Rotate API timeout handling". Keep secrets out of Git. Store environment-specific values in an environment file or secret manager excluded by .gitignore.

5. Fetch first, then decide how to integrate

git fetch origin
git log --oneline HEAD..origin/main
git diff HEAD...origin/main

git fetch downloads objects and updates remote-tracking references such as origin/main; it does not merge those commits into the checked-out branch or modify your current files. That makes it the safer inspection step for production. The two inspection commands show commits present remotely but not locally and the combined difference between the local tip and the remote branch.

Fetching also exposes a local checkout that has drifted without immediately changing the running code. Once you understand the difference, select an integration policy explicitly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

6. Pull with an explicit integration policy

git pull --ff-only origin main
# or, when your project policy allows a merge:
git pull --no-rebase origin main
# or, when your project policy requires rebasing:
git pull --rebase origin main
Policy History rewritten? Can create a merge commit? Conflict presentation Production suitability
--ff-only No No Stops when the local branch cannot be advanced directly; no automatic conflict resolution occurs. Safest default for a checkout that should mirror a tested linear branch.
--no-rebase No existing commits are rewritten Yes, when local and remote histories diverge Conflicts are resolved during the merge, followed by a merge commit. Use only when the project intentionally records merge commits.
--rebase Local, unpublished commits are replayed with new IDs No merge commit for the replayed work Conflicts appear one commit at a time during the rebase. Appropriate only when the project requires a rebased history and the local commits are safe to rewrite.

A pull normally performs a fetch and then integrates the selected remote branch. Git supports configuring the same choice with pull.rebase=false for merge behavior or pull.rebase=true for rebase behavior, but an explicit command is clearer during a release. Do not let an implicit default decide how a shared production branch changes.

Fast-forward-only update procedure

  1. Confirm a clean or understood working tree with git status --short --branch.
  2. Fetch with git fetch origin.
  3. Inspect git log --oneline HEAD..origin/main and git diff HEAD...origin/main.
  4. Run git pull --ff-only origin main.
  5. Record the resulting commit with git rev-parse --short HEAD and run the application’s deployment checks.

7. Push changes and establish upstream tracking

git push -u origin main
git push
git push origin feature/name

git push -u origin main publishes the local main branch and records origin/main as its upstream. Later, an argument-less git push can use that association. To publish a differently named branch without changing the current branch, use git push origin feature/name.

Push from the account and checkout intended for the operation. Before publishing, inspect git diff --cached if you have a commit staged, and inspect git log to ensure you are not about to send emergency or environment-specific files.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

8. Fix a non-fast-forward push rejection

A non-fast-forward rejection means the remote branch contains commits your local branch does not have. The remote must be fetched and integrated before Git will accept a normal push.

git fetch origin
git log --oneline --left-right HEAD...origin/main
git rebase origin/main   # or merge, according to project policy
# resolve conflicts, then:
git add <resolved-files>
git rebase --continue    # if rebasing
git push

Choose rebase or merge deliberately

  • Use rebase only for local commits that the project permits rewriting. Resolve each conflict, stage the corrected file, and continue with git rebase --continue.
  • Use a merge when the repository policy preserves the existing branch topology or when rewriting local history is not acceptable.
  • After either approach, inspect the resulting log and test the application before pushing.

Do not answer a rejection with an unreviewed force push to a shared production branch. Force-pushing can discard commits that other operators rely on; it should require explicit review and a documented recovery plan.

9. Tag releases and remove obsolete remote branches

git tag -a v1.2.0 -m "Release v1.2.0"
git push origin v1.2.0
git push origin --tags
git push origin :old-branch

The first two commands create and publish one annotated release tag. git push origin --tags publishes every local tag, so use it only when that broad action is intended. The colon form deletes a remote branch. Treat both tag publication and branch deletion as controlled release operations: verify the name, branch, and approvals first, and record what was changed.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

10. Recover from mistakes without guessing

git diff                 # unstaged changes
git diff --cached        # staged changes
git restore path/to/file # discard unstaged edits in a path
git reflog               # locate previous HEAD positions
git reset --hard HEAD    # discard all local tracked edits; destructive
git clean -nd             # preview untracked files that clean would remove

Safe inspection

Use the two git diff forms to determine whether a problem is unstaged or staged. git reflog records previous positions of HEAD and can help locate a commit after an accidental reset, branch switch, or rebase.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Destructive cleanup

git restore path/to/file discards unstaged edits only in the named path. git reset --hard HEAD discards all local tracked edits, so verify the status and preserve any production-only configuration first. git clean -nd is a dry-run preview of untracked files that a clean operation would remove; never skip that preview on a server containing generated or uploaded files.

Keep backups or deployment artifacts when a checkout contains files that do not come from the repository. Git cannot recover data that was never committed or otherwise copied.

11. VPS operating practices that prevent outages

  • Use a dedicated unprivileged deployment account. The account should own the checkout and run Git; avoid mixing root-owned files with user-owned files.
  • Separate code from secrets. Keep environment files and credentials outside version control, exclude them with .gitignore, and inspect the staged diff before every commit.
  • Prefer repeatable deployment. A bare repository with a deployment hook, or a pull-based deployment script, makes releases auditable and repeatable. Do not manually edit a checkout and then overwrite those edits with an unreviewed pull.
  • Verify branch and remote immediately before release. Run git status --short --branch, git branch --show-current, and git remote -v again if the session has been open for a while.
  • Record the deployed revision. Run git rev-parse --short HEAD after the update and save that value in the deployment log so an operator can identify exactly what is running.

12. SSH or HTTPS for the VPS remote?

Factor SSH remote HTTPS remote
Authentication Key pair and repository permissions for the deployment account. HTTPS credentials or a token managed according to organizational policy.
Firewall compatibility Requires outbound SSH access to the Git host. Often works where firewalls allow outbound web traffic but block SSH.
Credential handling Use a dedicated deploy key or account key with limited access; protect the private key. Avoid embedding long-lived credentials in remote URLs; use the approved credential helper or secret mechanism.
Unattended deployment Convenient once keys and permissions are correctly configured. Useful when HTTPS is mandated, provided non-interactive credentials are securely supplied.

Neither scheme removes the need to verify the repository and branch. Choose SSH when your network and key-management policy support it; choose HTTPS when firewall or credential requirements make it the operationally safer route.

13. A concise production update checklist

  1. Change to the intended directory and run pwd.
  2. Check git status --short --branch, the current branch, and git remote -v.
  3. Confirm secrets and production-only files are not tracked or about to be overwritten.
  4. Run git fetch origin and inspect incoming commits and the diff.
  5. Integrate with git pull --ff-only, or the explicitly approved merge/rebase policy.
  6. Run the application’s tests, build, migration, and service-restart checks required by your deployment process.
  7. Record git rev-parse --short HEAD in the release log.
  8. If a push is required, establish tracking with git push -u and never force-push a shared production branch without review.

What each command protects you from

  • status, branch, and remote checks prevent operating on the wrong checkout or destination.
  • fetch plus inspection prevents an unseen remote change from being merged into production blindly.
  • Explicit pull policies prevent accidental merge commits or history rewrites.
  • Selective staging and diff --cached keep secrets and unrelated edits out of commits.
  • Upstream tracking makes routine pushes predictable.
  • Reflog, dry-run cleaning, and recorded commit IDs make recovery and auditing possible.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.