The Commands You Will Use Most
Run these inside a repository, the folder that holds a .git directory:
| Command | What it does |
|---|---|
git status | Shows changed, staged, and new files. |
git add file | Stages a file for the next commit. |
git commit -m "msg" | Records the staged changes. |
git log --oneline | Lists commits, one per line. |
git diff | Shows changes not yet staged. |
git switch -c name | Creates a branch and moves to it. |
git pull | Fetches and applies new commits. |
git push | Sends your commits to the remote. |
Install Git and Set Your Identity
Install it with sudo apt install git on Debian and Ubuntu, or sudo dnf install git on AlmaLinux and Rocky Linux. Every commit records a name and an e-mail address; set them once per user account:
git config --global user.name "Deploy User"
git config --global user.email "YOUR_EMAIL"
git config --global init.defaultBranch main
git --version
The third line names the first branch of new repositories main.
Create or Copy a Repository
To start tracking an existing folder, such as a set of configuration files:
cd /srv/app
git init
To copy a repository that already exists elsewhere, clone it. Git creates a folder named after the project, or uses the folder you name:
git clone https://github.com/example/app.git
git clone https://github.com/example/app.git /var/www/app
For a private repository on a server, a read-only deploy key or access token is safer than your personal login.
Record Changes: status, add, and commit
Git works with your files, the staging area (what goes into the next commit), and the history. Check where things stand first:
git status
Then stage and commit:
git add nginx.conf
git add .
git commit -m "Raise upload limit to 20M"
git add . stages everything in the current folder, including new files, so read the output of git status before you run it. If you staged a file by mistake, see how to unstage files in Git.
Look at History and Differences
git log --oneline -10
git show a1b2c3d
git diff
git diff --staged
- git log: lists commits, newest first; press
qto leave the pager. - git show: prints one commit, named by the short hash from the log.
- git diff: shows edits you have not staged yet.
- git diff --staged: shows what the next commit will contain.
Work with Branches
git branch
git switch -c fix-login
git switch main
git merge fix-login
git branch -d fix-login
git branch lists branches and marks the current one. git switch -c creates a branch and moves to it; without -c it moves to an existing one. Older tutorials use git checkout for this, since switch arrived in Git 2.23. git branch -d deletes only a merged branch, so it does not lose work.
Sync with a Remote: remote, push, and pull
git remote -v
git remote add origin https://github.com/example/app.git
git push -u origin main
git pull
git remote -v lists the remotes; a cloned repository already has one called origin. Add -u to the first push of a branch: it links the branch to the remote one, so later a plain git push or git pull knows where to go. If both sides have new commits, current Git versions make git pull stop and ask how to combine them: add --rebase or --no-rebase. When the same lines changed on both sides, Git marks a conflict; edit those lines, run git add, and continue as Git tells you.
Keep Files Out with .gitignore
A file named .gitignore in the repository lists paths Git should not track. Typical entries on a server:
.env
*.log
node_modules/
vendor/
uploads/
Never commit passwords or keys: deleting the file later does not remove them from earlier commits. .gitignore affects only untracked files; a tracked file must be removed from the index with git rm --cached, as the unstaging guide explains.
Using Git on a Server
- Deploy by pulling: clone once, then run
git pullto update. Keep the.gitfolder out of the web server's reach. - Long operations: a large clone survives a dropped connection when you run it inside tmux; see the tmux cheat sheet.
- Commit messages:
git commitwithout-mopens your configured editor (core.editororEDITOR) or the system default, often nano or Vim. For Vim, see how to save and quit in Vim.
If you have not connected to the server yet, start with how to use PuTTY to SSH into a server.