Git for DevOps: Branches, Merges and Fixing Mistakes
What you build: a real Git repository on a free e2-micro VM where you practise branches, merges, a merge conflict, a rejected push, reflog rescue and every common fix. Every command tested.

✍️ Translate this blog into Hindi, Tamil or Telugu
In this lab you learn Git the way DevOps engineers use it every day: commits, branches, merges, and above all fixing mistakes. You build a tiny website repository on a free Google Cloud VM, cause the problems people really hit (a merge conflict, a rejected push, a password committed by mistake, commits "lost" with reset --hard) and fix each one with the right command.
Who it is for: beginners who have used git add and git commit a little and get nervous when Git prints an error. Time: about 1 to 1.5 hours. Cost: free. An e2-micro VM in us-east1 is in Google Cloud's Always Free tier (one per account), and we delete it at the end.
Every command below was run for real by the CareerByteCode Team on a fresh Google Cloud project, and every output and screenshot is the real result we got (only commit ids differ on your machine). No GitHub account is needed: a second folder on the same VM plays the role of GitHub, which behaves exactly the same for these commands.
🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโน499Build the projects →Part A: Git in plain English
Git is a version control system: it remembers every saved version of your project, who made it and why, and lets many people work on the same files without overwriting each other. In DevOps, Git is where everything starts: application code, Dockerfiles, Kubernetes YAML, Terraform, pipeline files. A push to Git is what starts most CI/CD pipelines.

Words you will use:
- Repository (repo): the project folder plus its hidden
.gitfolder, which holds the whole history. - Commit: a saved snapshot with a message and an id like
85ebb01. Real-life example: a page glued into a photo album with a caption. - Staging area: the list of changes that will go into the next commit (
git addputs them there). - Branch: a separate line of work.
mainis the main line;feature/contactis a side line for one change. Real-life example: a draft copy of a document you edit before it goes into the final copy. - Merge: bring a branch's commits into another branch. Merge conflict: both branches changed the same line, so Git asks you to choose.
- Remote (
origin): the shared copy of the repo, usually on GitHub, GitLab or Bitbucket. Push sends your commits there; pull brings others' commits to you. - HEAD: "where you are now", usually the newest commit of your current branch. Reflog: Git's private diary of every place HEAD has been, your safety net.
Before you start
- A Google Cloud account (the free trial works) and a project.
- Open Cloud Shell (the
>_icon at the top right of the Google Cloud console) and set your project:
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
gcloud services enable compute.googleapis.comCloud Shell already has Git, so you can also do Steps 2 to 10 right there. We use a VM so you also practise the server side, as on a real build machine.
Part B: the lab
Step 1: a free e2-micro VM
Why: a small Linux server to practise on. --no-service-account --no-scopes gives the VM no Google Cloud permissions at all, because Git needs none. Then connect with SSH (in the console: Compute Engine > VM instances > SSH next to git-lab opens a terminal in your browser).
gcloud compute instances create git-lab --zone=us-east1-b --machine-type=e2-micro \
--image-family=debian-12 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard \
--no-service-account --no-scopes 2>&1 | tail -2
until gcloud compute ssh git-lab --zone=us-east1-b --tunnel-through-iap --command="echo ssh ok" 2>/dev/null | grep -q ok; do sleep 10; done; echo "SSH works"NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
git-lab us-east1-b e2-micro 10.142.0.4 EXTERNAL_IP RUNNING
SSH worksgcloud compute ssh git-lab --zone=us-east1-bEverything from Step 2 runs inside the VM, in a folder ~/lab271 (mkdir -p ~/lab271 && cd ~/lab271).
Step 2: install Git and say who you are
Why: every commit records a name and email. init.defaultBranch main makes new repos start on main (the modern default name).
# install Git and tell it who you are (these name and email values go into every commit)
sudo apt-get update -qq > /dev/null && sudo apt-get install -y -qq git tree > /dev/null 2>&1
git --version
git config --global user.name "Asha Dev"
git config --global user.email "asha@example.com"
git config --global init.defaultBranch main
git config --global --listgit version 2.39.5
user.name=Asha Dev
user.email=asha@example.com
init.defaultbranch=mainStep 3: a repository and the first commits
Why: git init makes the repo; git status --short shows ?? for files Git does not track yet; git add stages them; git commit -m saves them. git diff shows what changed before you commit, and git commit -am stages and commits files Git already tracks in one go.
# a new repository for a tiny website, and the first two commits
rm -rf site && mkdir site && cd site
git init
echo "<h1>Nimbus Shop</h1>" > index.html
printf "port=8080\nmax_users=50\n" > app.conf
git status --short
git add index.html app.conf
git commit -m "Add home page and app config"
echo "<p>Open 9 am to 6 pm</p>" >> index.html
git diff
git commit -am "Add opening hours"
git log --onelineInitialized empty Git repository in /home/you/lab271/site/.git/
?? app.conf
?? index.html
[main (root-commit) e14b619] Add home page and app config
2 files changed, 3 insertions(+)
create mode 100644 app.conf
create mode 100644 index.html
diff --git a/index.html b/index.html
index e513239..fa5268e 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
<h1>Nimbus Shop</h1>
+<p>Open 9 am to 6 pm</p>
[main a6b1dfc] Add opening hours
1 file changed, 1 insertion(+)
a6b1dfc Add opening hours
e14b619 Add home page and app config
Step 4: a branch, a hotfix on main, and a merge
Why: this is the daily flow. You start feature/contact for a new page. Meanwhile an urgent fix lands on main. When you merge, Git combines both, because they touched different files.
# a feature branch for a contact page, a hotfix on main at the same time, then merge both
cd site
git switch -c feature/contact
echo "<h1>Contact us</h1>" > contact.html
git add contact.html && git commit -m "Add contact page"
git switch main
sed -i "s/max_users=50/max_users=100/" app.conf
git commit -am "Hotfix: allow 100 users"
git branch
git merge feature/contact --no-edit
git log --oneline --graph --all
git branch -d feature/contactSwitched to a new branch 'feature/contact'
[feature/contact 90b3027] Add contact page
1 file changed, 1 insertion(+)
create mode 100644 contact.html
Switched to branch 'main'
[main ac0d727] Hotfix: allow 100 users
1 file changed, 1 insertion(+), 1 deletion(-)
feature/contact
* main
Merge made by the 'ort' strategy.
contact.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 contact.html
* 56b2382 Merge branch 'feature/contact'
|\
| * 90b3027 Add contact page
* | ac0d727 Hotfix: allow 100 users
|/
* a6b1dfc Add opening hours
* e14b619 Add home page and app config
Deleted branch feature/contact (was 90b3027).
Merge made by the 'ort' strategy means Git made a merge commit with two parents. In the graph (--graph) you can see the fork and the join. After the merge, delete the branch: its commits are safe in main.
Break it: a merge conflict
Two branches change the same line: one sets the port to 9090, the other to 8000. Git cannot know which is right.
# Break it: two branches change the SAME line, so Git cannot merge by itself (a merge conflict)
cd site
git switch -c feature/port
sed -i "s/port=8080/port=9090/" app.conf && git commit -qam "Use port 9090"
git switch -q main
sed -i "s/port=8080/port=8000/" app.conf && git commit -qam "Use port 8000"
git merge feature/port
git status --short
cat app.confSwitched to a new branch 'feature/port'
Auto-merging app.conf
CONFLICT (content): Merge conflict in app.conf
Automatic merge failed; fix conflicts and then commit the result.
UU app.conf
<<<<<<< HEAD
port=8000
=======
port=9090
>>>>>>> feature/port
max_users=100
UU app.conf means "both changed, unmerged". Inside the file, the lines between <<<<<<< HEAD and ======= are yours (main), the lines down to >>>>>>> feature/port are the branch's. Nothing is broken: Git is waiting for you.
Fix it: choose, remove the markers, commit
Why: you decide the right content (here the team agreed on 9090), write the file without the markers, git add it to say "resolved", and git commit --no-edit finishes the merge with Git's default message.
# Fix it: choose the right line, remove the markers, add the file and finish the merge
cd site
printf "port=9090\nmax_users=100\n" > app.conf
git add app.conf
git commit --no-edit
git log --oneline --graph -6
git branch -d feature/port[main b28851d] Merge branch 'feature/port'
* b28851d Merge branch 'feature/port'
|\
| * 7725f95 Use port 9090
* | 99b729e Use port 8000
|/
* 56b2382 Merge branch 'feature/contact'
|\
| * 90b3027 Add contact page
* | ac0d727 Hotfix: allow 100 users
|/
Deleted branch feature/port (was 7725f95).
If you get lost in a conflict, git merge --abort puts everything back as it was before the merge.
Step 5: everyday fixes
Why: these four cover most "oops" moments:
| Mistake | Command | What it does |
|---|---|---|
| Typo in the last commit message (not pushed yet) | git commit --amend -m "..." | Replaces the last commit with a corrected one |
| Staged a file by mistake | git restore --staged notes.txt | Takes it out of the next commit, keeps the file |
| Broke a file and want the last saved version | git restore index.html | Throws away your edit (careful: it is gone) |
| A bad commit is already shared | git revert HEAD | Adds a NEW commit that undoes it; history stays honest |
# everyday fixes: a wrong commit message, a file added by mistake, an unwanted edit, a bad commit already shared
cd site
echo "<p>Call 044 1234 5678</p>" >> contact.html
git commit -am "Add phnoe number"
git commit --amend -m "Add phone number" | head -1
echo "draft notes" > notes.txt
git add notes.txt && git status --short
git restore --staged notes.txt && git status --short && rm notes.txt
echo "BROKEN" > index.html
git restore index.html && head -1 index.html
git revert --no-edit HEAD
git log --oneline -3[main 9abe19f] Add phnoe number
1 file changed, 1 insertion(+)
[main f9030b2] Add phone number
A notes.txt
?? notes.txt
<h1>Nimbus Shop</h1>
[main 9f87e20] Revert "Add phone number"
Date: Wed Nov 18 18:28:29 2026 +0000
1 file changed, 1 deletion(-)
9f87e20 Revert "Add phone number"
f9030b2 Add phone number
b28851d Merge branch 'feature/port'
A notes.txt (staged) became ?? notes.txt (untracked) after restore --staged. Rule: amend and reset only change commits nobody else has; for anything already pushed, use revert.
Step 6: a password file committed by mistake
Why: it happens to everyone. git rm --cached .env stops tracking the file but keeps it on disk, and .gitignore stops it coming back (!! .env means "ignored").
# a password file committed by mistake: stop tracking it, ignore it from now on
cd site
echo "DB_PASSWORD=lab-only-123" > .env
git add .env && git commit -qm "Add env file" && git log --oneline -1
git rm --cached .env
echo ".env" > .gitignore
git add .gitignore && git commit -m "Stop tracking .env and ignore it"
git status --short --ignored
git log --oneline --all -- .env18a186b Add env file
rm '.env'
[main 5a0684c] Stop tracking .env and ignore it
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 .env
create mode 100644 .gitignore
!! .env
5a0684c Stop tracking .env and ignore it
18a186b Add env file
Look at the last output line: the commit Add env file is still in history, so anyone with the repo can still read the password. If a real secret was ever pushed, treat it as leaked: change the password or key first, then clean history with a tool such as git filter-repo if needed. Keep secrets out of Git from the start, for example in Secret Manager.
Step 7: "lost" commits and the reflog
Why: git reset --hard HEAD~2 moves the branch back 2 commits and throws away the files' changes. It feels like the commits are gone. They are not: the reflog remembers where HEAD was.
# Break it and fix it: reset --hard "loses" 2 commits; the reflog still knows them
cd site
git log --oneline -3
git reset --hard HEAD~2
git log --oneline -2
git reflog -4
git reset --hard HEAD@{1}
git log --oneline -35a0684c Stop tracking .env and ignore it
18a186b Add env file
9f87e20 Revert "Add phone number"
HEAD is now at 9f87e20 Revert "Add phone number"
9f87e20 Revert "Add phone number"
f9030b2 Add phone number
9f87e20 HEAD@{0}: reset: moving to HEAD~2
5a0684c HEAD@{1}: commit: Stop tracking .env and ignore it
18a186b HEAD@{2}: commit: Add env file
9f87e20 HEAD@{3}: revert: Revert "Add phone number"
HEAD is now at 5a0684c Stop tracking .env and ignore it
5a0684c Stop tracking .env and ignore it
18a186b Add env file
9f87e20 Revert "Add phone number"
HEAD@{1} means "where HEAD was one move ago". Resetting to it brought both commits back. The reflog is local to your machine and keeps entries for about 90 days by default.
Step 8: a remote and a teammate
Why: real work is shared. git init --bare makes an empty shared repo (exactly what GitHub makes when you click New repository). You push main to it; a teammate clones it, adds a page and pushes. On GitHub, the only difference is the address: git remote add origin https://github.com/you/site.git.
# a shared remote (a bare repo, like an empty GitHub repo) and a teammate's clone
cd ~/lab271
rm -rf remote teammate && git init -q --bare remote/site.git
cd site && git remote add origin ~/lab271/remote/site.git 2>/dev/null; git push -u origin main 2>&1 | tail -2
cd .. && git clone -q remote/site.git teammate && cd teammate
git config user.name "Ravi Ops" && git config user.email "ravi@example.com"
echo "<h1>Free delivery on big orders</h1>" > delivery.html && git add delivery.html && git commit -qm "Add delivery page" && git push -q origin main
git log --oneline -1 * [new branch] main -> main
branch 'main' set up to track 'origin/main'.
d27ee93 Add delivery pageBreak it: push rejected
You commit a footer and push, but the remote already has your teammate's new commit, which you do not have.
# Break it: you push while the remote has your teammate's new commit, so Git refuses
cd site
echo "<footer>Nimbus Shop 2026</footer>" >> index.html && git commit -qam "Add footer"
git push origin mainTo /home/you/lab271/remote/site.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '/home/you/lab271/remote/site.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.[rejected] main -> main (fetch first): Git refuses so it never overwrites your teammate's work. Never "fix" this with git push --force on a shared branch: that deletes their commit from the remote.
Fix it: pull with rebase, then push
Why: git pull --rebase downloads the teammate's commit and replays your commit on top of it, so history stays a straight line.
# Fix it: take the teammate's commit first, put yours on top (rebase), then push
cd site
git pull --rebase origin main 2>&1 | tail -1
git push origin main 2>&1 | tail -1
git log --oneline --graph -4Rebasing (1/1) Successfully rebased and updated refs/heads/main.
d27ee93..68b9fa7 main -> main
* 68b9fa7 Add footer
* d27ee93 Add delivery page
* 5a0684c Stop tracking .env and ignore it
* 18a186b Add env file
Step 9: stash and tags
Why: git stash parks half-done work so you can switch to something urgent with a clean folder, and git stash pop brings it back. A tag gives a commit a permanent name, such as a release v1.0; pipelines often build and deploy on tags.
# stash: park half-done work to fix something urgent; tag: name a release
cd site
echo "<p>WIP: new banner</p>" >> index.html
git stash push -m "banner work"
git status --short && echo "(clean, banner work parked)"
git stash list
git stash pop | tail -1
git checkout -- index.html
git tag -a v1.0 -m "First release"
git push -q origin v1.0
git tag -n
git log --oneline --decorate -3Saved working directory and index state On main: banner work
(clean, banner work parked)
stash@{0}: On main: banner work
Dropped refs/stash@{0} (42f5b0465a818c201dc2462cbc738fc4bf6e4e91)
v1.0 First release
68b9fa7 (HEAD -> main, tag: v1.0, origin/main) Add footer
d27ee93 Add delivery page
5a0684c Stop tracking .env and ignore it
POC: prove it works
- A repository with 2 branches merged, one by a merge commit with a real conflict resolved by hand.
- A wrong message amended, a file unstaged, an edit undone, a shared commit reverted.
- A committed password file untracked and ignored, with the honest warning that history still has it.
- Two "lost" commits brought back with the reflog.
- A rejected push fixed with
pull --rebase, and av1.0tag on the shared remote.
Quiz task: check what you learned
Try these on your own in the same repo, then check the answers:
- Make a branch
feature/about, addabout.html, merge it intomain. *(Answer:git switch -c feature/about, add and commit,git switch main,git merge feature/about.)* - You committed with the message "fix" and have not pushed. Make it "Fix footer year". *(Answer:
git commit --amend -m "Fix footer year".)* - A commit already on the remote broke the page. Undo it safely. *(Answer:
git revert <commit-id>thengit push.)* - Where is HEAD 3 moves ago? *(Answer:
git reflog, entryHEAD@{3}.)*
Clean up (delete everything)
Why: the e2-micro is free only for one VM per account and the disk counts too. Exit the VM (exit), then in Cloud Shell:
gcloud compute instances delete git-lab --zone=us-east1-b --quiet 2>&1 | tail -1
echo "git-lab VMs left: $(gcloud compute instances list --filter="name=git-lab" --format="value(name)" | wc -l)"
echo "git-lab disks left: $(gcloud compute disks list --filter="name=git-lab" --format="value(name)" | wc -l)"Deleted [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-east1-b/instances/git-lab].
git-lab VMs left: 0
git-lab disks left: 0
FAQ
Merge or rebase? Both combine work. Merge keeps the real history with merge commits; rebase rewrites your own unpushed commits so history is a straight line. Rule of thumb: rebase your own local work, merge shared branches, and never rebase commits others already have.
**What is the difference between reset, restore and revert?** restore changes files (undo edits, unstage). reset moves the branch to another commit (rewrites history, local only). revert adds a new commit that undoes an old one (safe for shared history).
**Is git push --force ever OK?** Only on your own branch that nobody else uses, and then prefer git push --force-with-lease, which refuses if someone else pushed meanwhile.
How do I connect this to GitHub? Create an empty repo on GitHub, then git remote add origin <its URL> and git push -u origin main. GitHub needs a personal access token or an SSH key to sign in. We did not test the GitHub sign-in in this lab.
What should never go into Git? Passwords, keys, tokens, .env files, large binaries and build output. Add them to .gitignore on day one.
What branch names do teams use? Commonly main, plus short-lived feature/..., fix/... or hotfix/... branches merged through pull requests.
Troubleshooting
| You see | Why | Fix |
|---|---|---|
Please tell me who you are | No name or email set | git config --global user.name "..." and user.email |
CONFLICT (content): Merge conflict in x | Both branches changed the same lines | Edit the file, remove the markers, git add x, git commit (or git merge --abort) |
! [rejected] main -> main (fetch first) | The remote has commits you do not have | git pull --rebase origin main, then push |
error: Your local changes would be overwritten | Uncommitted edits block a switch or pull | Commit them, or git stash, then try again |
fatal: not a git repository | You are outside the repo folder | cd into the folder that has .git |
Commits gone after reset --hard | The branch moved back | git reflog, then git reset --hard HEAD@{n} |
What next
- Get comfortable on the server first: Linux for Cloud Beginners: 50 Commands on a Free VM.
- Automate with scripts: Bash Scripting for DevOps: Automate Boring Tasks.
- Let a Git push build and deploy for you: CI/CD for Beginners: Build and Deploy on Every Git Push.
- More hands-on posts: ByteBlog. Practice in ByteLabs and see the courses.
Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.
⭐ Keep learning with CareerByteCode
Hand-picked to go deeper on this topicGet CareerByteCode Team's next blog by email
One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.
We email you once to confirm. By subscribing you agree to the privacy policy.
Related blogs
CareerByteCode TeamLinux File Permissions Explained with Real Mistakes
What you build: a script, a team folder, an SSH key and a config file that break with Permission denied, then work with the right chmod, chown and group, on a free VM.
#bash
CareerByteCode TeamBash Scripting for DevOps: Automate Boring Tasks
What you build: five Bash scripts (arguments, checks, backups with rotation, a log report) made safe with set -euo pipefail and scheduled with cron, on a free Linux VM. Every step tested.
#beginners
CareerByteCode TeamCron Jobs and Log Rotation on Linux
What you build: a disk report job run by cron every minute with its log rotated by logrotate, on a free VM, plus 3 real mistakes: relative paths, the % sign and an insecure log folder.
#beginners
CareerByteCode TeamGoogle Cloud Free Tier Explained: What Is Really Free
What you build: a tour of the Always Free products (VM, bucket, BigQuery, Cloud Run, Pub/Sub, Secret Manager) with a real price-list cost check, the bigger-VM trap and a budget alert.
💬 Comments (0)
Top · NewestSign in to comment