Clone, branch, commit, push, merge — and deploy with Docker
Git is the most popular version control system for tracking code changes, collaborating with others, and safely delivering features without breaking your main codebase. This guide walks you through a real-world Git workflow:
✅ Create or clone a repo
✅ Create branches for features
✅ Commit changes properly
✅ Push to a remote (GitHub/GitLab)
✅ Open a Pull Request / Merge changes
✅ Deploy the updated app in a Docker environment
We’ll use a generic project name:
Project:
my-sample-project
1) Prerequisites (Quick Setup)
Make sure you have these installed:
- Git (
git --version) - A code editor (VS Code is common)
- Optional but recommended: Docker + Docker Compose for deployment
Configure Git identity (do this once):
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
2) Understand the Core Git Terms (Simple Version)
- Repository (repo): Your project tracked by Git (local + remote).
- Commit: A saved snapshot of changes.
- Branch: A separate line of development (safe space to work).
- Remote: Shared repo on GitHub/GitLab.
- Push: Upload local commits to remote.
- Pull: Download changes from remote to your local.
- Merge: Combine changes from one branch into another (usually into
main).
3) Option A — Clone an Existing Repository
Use this when a project already exists remotely and you want a local copy:
git clone <repository-url>
cd my-sample-project
What it does:
- Downloads the full project history
- Creates a local folder
- Automatically sets a remote called
origin
4) Option B — Create a New Repository from Scratch (Command Line)
Use this when you are starting a brand-new project.
Step 1: Create project folder and initialize Git
mkdir my-sample-project
cd my-sample-project
git init
Step 2: Add a README and commit it
echo "# my-sample-project" >> README.md
git add README.md
git commit -m "Initial commit"
Step 3: Rename default branch to main
git branch -M main
Step 4: Connect to a remote repository
Create an empty repo in GitHub/GitLab first, then connect it:
git remote add origin <repository-url>
git push -u origin main
What -u means:
- Sets upstream tracking so later you can just run
git pushwithout extra args.
5) The Standard Daily Workflow (The One You’ll Use Most)
This is the routine most dev teams follow:
Step 1: Pull latest main (start clean)
git checkout main
git pull origin main
Step 2: Create and switch to a feature branch
Branch naming examples:
feature/login-pagebugfix/fix-timeouthotfix/urgent-prod-fix
Create a branch:
git checkout -b feature/my-new-change
Step 3: Make changes in code (edit files)
Check what changed:
git status
git diff
Step 4: Stage changes
Stage specific files:
git add <file-name>
Or stage everything:
git add .
Step 5: Commit changes with a good message
git commit -m "Add user profile page layout"
Good commit messages:
- Start with a verb: Add, Fix, Update, Remove, Refactor
- Keep it specific and meaningful
Step 6: Push your branch to remote
git push origin feature/my-new-change
6) Merge Your Branch into main (Best Practice)
In most teams, you merge via a Pull Request / Merge Request in GitHub/GitLab (recommended). But the Git commands are still important.
Option A (Recommended): Merge via Pull Request
Typical flow:
- Push your branch
- Open PR/MR in GitHub/GitLab
- Code review
- Merge into
main
Option B: Merge locally using Git commands
First, update local main:
git checkout main
git pull origin main
Now merge your feature branch:
git merge feature/my-new-change
Push the updated main:
git push origin main
7) Handling Merge Conflicts (What to Do When Git Complains)
A merge conflict happens when Git can’t automatically combine changes (usually when two people edited the same lines).
When it happens:
- Git will tell you which files have conflicts
- Open those files and look for:
<<<<<<< HEAD
your main branch code
=======
your feature branch code
>>>>>>> feature/my-new-change
- Choose the correct version (or combine both)
- Then:
git add <conflicted-file>
git commit -m "Resolve merge conflict"
git push origin main
8) Useful Git Commands You’ll Use a Lot
View commit history
git log --oneline
See branches
git branch
Switch branches
git checkout <branch-name>
Delete a local branch (after merge)
git branch -d feature/my-new-change
Delete remote branch
git push origin --delete feature/my-new-change
Deploying the Project Using Docker (After You Merge)
Once your changes are merged to main, deployment often means: build and run the latest code in Docker.
Below are two common deployment styles.
9) Docker Deployment Option A — Simple Dockerfile (Single Service)
Example Dockerfile
Create Dockerfile:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
Build image:
docker build -t my-sample-project:latest .
Run container:
docker run -d --name my-sample-project -p 3000:3000 my-sample-project:latest
10) Docker Deployment Option B — Docker Compose (App + Database)
Use this when you have multiple services (example: app + PostgreSQL).
Example docker-compose.yml
services:
app:
build: .
container_name: my-sample-project-app
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:16
container_name: my-sample-project-db
environment:
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=apppass
- POSTGRES_DB=appdb
ports:
- "5432:5432"
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:
Deploy:
docker compose up -d --build
Check containers:
docker ps
docker logs -f my-sample-project-app
11) Updating the Deployment When Code Changes
Typical deployment cycle after merging into main:
On your server / VM:
cd my-sample-project
git checkout main
git pull origin main
docker compose down
docker compose up -d --build
That ensures your Docker environment is running the latest code.
Recommended “Safe” Workflow Summary (Cheat Sheet)
Start work
git checkout main
git pull origin main
git checkout -b feature/my-change
Save work
git add .
git commit -m "Describe what changed"
git push origin feature/my-change
Merge
- Open PR/MR → review → merge to
main - Or local merge:
git checkout main
git pull origin main
git merge feature/my-change
git push origin main
Deploy with Docker
git pull origin main
docker compose up -d --build
