DevOps & Deployment

Git Setup & Deployment Guide

Version control for every project – install and configure Git, commit, branch and merge, then deploy releases from Git to a server.

Updated Official documentation ↗

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 needVersion / notes
Operating systemWindows, macOS or Linux
TerminalGit Bash on Windows, Terminal on macOS/Linux
EditorVS Code (built-in Git panel)

3. Local setup

  1. 1Install Git

    Windows: “Git for Windows” from git-scm.com (includes Git Bash). macOS: xcode-select --install or brew install git. Linux: your package manager.

    BASH
    sudo apt install -y git     # Ubuntu/Debian
    git --version
  2. 2Set your identity

    Commits are labelled with this name and email (use the email of your GitHub/GitLab account). Set main as the default branch name.

    BASH
    git 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 false
  3. 3Create an SSH key

    SSH keys let you push without typing a password. Add the public key (.pub) to your Git host.

    BASH
    ssh-keygen -t ed25519 -C "you@example.com"
    cat ~/.ssh/id_ed25519.pub      # copy this to GitHub/GitLab → SSH keys

4. Create & run a project

  1. 1Start a repository and make the first commit

    Stage files with add, save a snapshot with commit.

    BASH
    mkdir my-project && cd my-project
    git init
    echo "# My Project" > README.md
    git add README.md
    git commit -m "Initial commit"
    git log --oneline
  2. 2Ignore files that must not be committed

    Dependencies, builds and secrets stay out of Git.

    .gitignore
    node_modules/
    dist/
    .env
    .venv/
    *.log
    .DS_Store
  3. 3Work on a branch and merge it

    Create a feature branch, commit, then merge back into main.

    BASH
    git 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/login
  4. 4Connect a remote and push

    Create an empty repository on GitHub/GitLab first, then:

    BASH
    git remote add origin git@github.com:you/my-project.git
    git push -u origin main
  5. 5Undo safely

    Common fixes without losing work.

    BASH
    git 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
# ~/.gitconfig (created by git config --global)
[user]
    name = Your Name
    email = you@example.com
[init]
    defaultBranch = main

Key dependencies

Package / toolUsed for
gitThe version control tool
SSH keyAuthentication with remotes
.gitignoreFiles Git should never track
GitHub / GitLab / BitbucketRemote hosting, reviews, CI
VS Code / GitHub DesktopGraphical Git clients

Folder structure

project structure
my-project/
├── .git/            # repository data (do not edit)
├── .gitignore
├── README.md
└── src/

Essential commands

CommandWhat it does
git statusWhat changed
git add . && git commit -m "msg"Save a snapshot
git switch -c nameNew branch
git pull / git pushSync with the remote
git log --oneline --graphHistory
git stashPut 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

RequirementRecommendation
ServerLinux VPS with Git installed
AccessRead-only deploy key for the repository
ReleasesTags (v1.2.0) for every version you deploy
RollbackAbility to check out the previous tag

Deployment steps

  1. 1Tag the release

    Use semantic versions and push the tag.

    BASH
    git tag -a v1.0.0 -m "First release"
    git push origin v1.0.0
  2. 2Prepare 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.

    BASH
    ssh 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_IP
  3. 3Add 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).

    BASH
    ssh-keygen -t ed25519 -f ~/.ssh/deploy_key -N ""
    cat ~/.ssh/deploy_key.pub
    # ~/.ssh/config
    Host github.com
      IdentityFile ~/.ssh/deploy_key
  4. 4Option A: pull a tag on the server

    Clone once, then fetch and check out each release.

    BASH
    git 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 commands
  5. 5Option B: push to the server (bare repo + hook)

    Push from your PC and let a post-receive hook 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 main
  6. 6Roll back

    Check out the previous tag and rebuild/restart.

    BASH
    git 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.

terminal$ git commit -m "Initial commit"[main (root-commit) 3f2a1c9] Initial commit 1 file changed, 1 insertion(+) create mode 100644 README.md$ git push -u origin mainTo github.com:you/my-project.git * [new branch] main -> mainbranch 'main' set up to track 'origin/main'.
Figure 1: terminal output of the sample Git project (section 4).
1Commit on branch2Merge to main3Tag v1.0.04Server pulls /hook5Live release
Figure 2: production deployment flow (section 6).
my-project/├── .git/ # repository data (do not edit)├── .gitignore├── README.md└── src/
Figure 3: project folder structure (section 5).

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.