1. Introduction & use cases
Git is a distributed version control system. It records every change to your files as commits, lets you work on branches in parallel and merge them, and makes it easy to undo mistakes. Every developer has the full history locally; remote hosts such as GitHub and GitLab share it with the team.
Git is also the starting point of almost every deployment pipeline: a push to the main branch triggers tests and deployment.
Where it is used
- Tracking changes in code, configs and documentation
- Team collaboration with branches and pull/merge requests
- Releasing versions with tags
- Triggering CI/CD and deployments
2. Prerequisites
| You need | Version / notes |
|---|---|
| Operating system | Windows, macOS or Linux |
| Terminal | Git Bash on Windows, Terminal on macOS/Linux |
| Editor | VS Code (built-in Git panel) |
3. Local setup
1Install Git
Windows: “Git for Windows” from git-scm.com (includes Git Bash). macOS:
xcode-select --installorbrew install git. Linux: your package manager.BASHsudo apt install -y git # Ubuntu/Debian git --version2Set your identity
Commits are labelled with this name and email (use the email of your GitHub/GitLab account). Set
mainas the default branch name.BASHgit config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global init.defaultBranch main git config --global pull.rebase false3Create an SSH key
SSH keys let you push without typing a password. Add the public key (
.pub) to your Git host.BASHssh-keygen -t ed25519 -C "you@example.com" cat ~/.ssh/id_ed25519.pub # copy this to GitHub/GitLab → SSH keys
4. Create & run a project
1Start a repository and make the first commit
Stage files with
add, save a snapshot withcommit.BASHmkdir my-project && cd my-project git init echo "# My Project" > README.md git add README.md git commit -m "Initial commit" git log --oneline2Ignore files that must not be committed
Dependencies, builds and secrets stay out of Git.
.gitignorenode_modules/ dist/ .env .venv/ *.log .DS_Store3Work on a branch and merge it
Create a feature branch, commit, then merge back into
main.BASHgit switch -c feature/login # ...edit files... git add . git commit -m "Add login form" git switch main git merge feature/login git branch -d feature/login4Connect a remote and push
Create an empty repository on GitHub/GitLab first, then:
BASHgit remote add origin git@github.com:you/my-project.git git push -u origin main5Undo safely
Common fixes without losing work.
BASHgit restore file.txt # discard uncommitted changes in a file git restore --staged file.txt # unstage git commit --amend # fix the last commit message (before pushing) git revert <commit> # new commit that undoes an old one (safe after pushing)
✅ When it works you should see output like Figure 1.
5. Development configuration
Environment variables
Git does not use environment variables for projects, but it must never store your .env files – list them in .gitignore. If a secret was committed, rotate it: deleting the file later does not remove it from history.
# ~/.gitconfig (created by git config --global)
[user]
name = Your Name
email = you@example.com
[init]
defaultBranch = mainKey dependencies
| Package / tool | Used for |
|---|---|
git | The version control tool |
SSH key | Authentication with remotes |
.gitignore | Files Git should never track |
GitHub / GitLab / Bitbucket | Remote hosting, reviews, CI |
VS Code / GitHub Desktop | Graphical Git clients |
Folder structure
my-project/
├── .git/ # repository data (do not edit)
├── .gitignore
├── README.md
└── src/Essential commands
| Command | What it does |
|---|---|
git status | What changed |
git add . && git commit -m "msg" | Save a snapshot |
git switch -c name | New branch |
git pull / git push | Sync with the remote |
git log --oneline --graph | History |
git stash | Put unfinished work aside |
git tag -a v1.0.0 -m "v1.0.0" | Mark a release |
6. Production deployment
Git itself is not a hosting platform, but it is a simple way to ship code to a server: tag a release, then either pull it on the server with a read-only deploy key, or push to a bare repository on the server whose hook checks the code out. For automated deployments use GitHub Actions or GitLab CI/CD.
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Server | Linux VPS with Git installed |
| Access | Read-only deploy key for the repository |
| Releases | Tags (v1.2.0) for every version you deploy |
| Rollback | Ability to check out the previous tag |
Deployment steps
1Tag the release
Use semantic versions and push the tag.
BASHgit tag -a v1.0.0 -m "First release" git push origin v1.0.02Prepare an Ubuntu server
Create a VPS (AWS EC2, DigitalOcean, Hostinger VPS, Linode …) with Ubuntu 24.04 LTS, at least 1 vCPU and 1–2 GB RAM. Log in with SSH, update the system and create a non-root user for the app.
BASHssh root@YOUR_SERVER_IP apt update && apt upgrade -y adduser deploy usermod -aG sudo deploy # copy your SSH key to the new user, then log in as deploy rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy exit ssh deploy@YOUR_SERVER_IP3Add a read-only deploy key
On the server create a key and add the public part to the repository as a Deploy key (read-only).
BASHssh-keygen -t ed25519 -f ~/.ssh/deploy_key -N "" cat ~/.ssh/deploy_key.pub # ~/.ssh/config Host github.com IdentityFile ~/.ssh/deploy_key4Option A: pull a tag on the server
Clone once, then fetch and check out each release.
BASHgit clone git@github.com:you/my-project.git /var/www/my-project cd /var/www/my-project git fetch --tags git checkout v1.0.0 # then run the project's build/restart commands5Option B: push to the server (bare repo + hook)
Push from your PC and let a
post-receivehook update the live folder.BASH# on the server git init --bare /srv/git/my-project.git cat > /srv/git/my-project.git/hooks/post-receive <<'EOF' #!/bin/sh git --work-tree=/var/www/my-project --git-dir=/srv/git/my-project.git checkout -f main EOF chmod +x /srv/git/my-project.git/hooks/post-receive # on your PC git remote add production deploy@YOUR_SERVER_IP:/srv/git/my-project.git git push production main6Roll back
Check out the previous tag and rebuild/restart.
BASHgit checkout v0.9.0
🗺 The whole path from your code to the live site is drawn in the deployment diagram.
7. Images & screenshots
Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.
8. Common errors & solutions
Permission denied (publickey)
Your SSH key is not added to the Git host, or the wrong key is used. Test with ssh -T git@github.com and add ~/.ssh/id_ed25519.pub to your account.
! [rejected] main -> main (fetch first)
The remote has commits you do not have. Run git pull (resolve conflicts if any), then push again.
Merge conflict
Open the files with <<<<<<< markers, keep the right code, remove the markers, then git add and git commit.
Committed a secret (.env, API key)
Rotate the secret immediately. Removing it needs history rewriting (git filter-repo) and a force-push – and copies may already exist.
fatal: not a git repository
You are outside the project folder, or git init was never run.
Best practices
- Small, focused commits with clear messages.
- Never commit secrets, builds or
node_modules. - Protect
main; work on branches and use pull requests. - Tag every production release.
- Pull before you start work each day.