If you already know the basic Git commands, this lesson shows how teams use them to ship software safely. If you are new to Git, first read the free Git and GitHub course on this site, then come back, because this lesson assumes you can commit, branch and push. In DevOps, Git is not just where code lives. Pipelines start from it, releases are marked in it, and in GitOps even the live system is described in it. A few habits make that smooth.
Why do we need it?
Every pipeline starts from Git, and teams share one project. Good habits stop people overwriting each other, keep history clean, and make releases and rollbacks easy.
A real-life example
Imagine a team writing one big book. Each writer works on their own copy of a chapter, then the editor merges the chapters. If two writers changed the same sentence, the editor decides which version stays. Git is the system that tracks all copies, edits and decisions.
Branching strategies
A branching strategy is the team’s agreement about which branches exist and how work flows between them. Three are common.
- Trunk-based development: everyone works in very short branches (a day or two) and merges into
mainoften. It relies on good automatic tests and, often, feature flags to hide unfinished work. It suits teams that deploy many times a day. - GitHub flow:
mainis always deployable. For every change you create a branch, open a pull request, get a review and tests, merge, and deploy. Simple and very popular. - Git flow: long-lived
developandmainbranches plus feature, release and hotfix branches. It fits software with scheduled releases and several supported versions, but it has more overhead.
There is no single best choice. For a small team with a web application, GitHub flow is a good start. Whatever you choose, keep branches short-lived: the longer a branch lives, the harder the merge.
Merge or rebase
Both combine work from two branches. A merge adds a new commit that joins the two histories, and keeps everything as it happened. A rebase replays your commits on top of the latest main, which gives a straight line of history that is easier to read. The golden rule of rebase: never rebase commits that other people have already pulled, because you rewrite history and cause trouble for them. A safe pattern is to rebase your own private branch on main before you open the pull request, and to merge (or squash) when the pull request is accepted.
git switch -c feature/health-endpoint # start a branch
git add . && git commit -m "Add /health endpoint"
git fetch origin
git rebase origin/main # replay my work on the newest main
git push --force-with-lease -u origin feature/health-endpoint
--force-with-lease is a safer force push: it refuses if someone else pushed to your branch in the meantime. You need a force push after a rebase, because history changed.
Stash, reset and revert
git stashputs your unfinished changes on a shelf so you can switch branches with a clean folder.git stash popbrings them back, andgit stash listshows the shelf.git restore filethrows away changes to a file that you have not committed.git reset --soft HEAD~1undoes the last commit but keeps the changes ready to commit again.git reset --harddestroys uncommitted work, so think before you use it.git revert <commit>makes a new commit that undoes an old one. Use revert, not reset, to undo something that is already shared, because it keeps the history honest.
A quick refresher on the everyday commands
git initstarts a repository andgit clone <url>copies an existing one.git addstages changes,git commit -m "message"records them.git fetchdownloads new work without changing your files.git pullis fetch plus merge.git pushuploads your commits.git status,git log --onelineandgit diffshow where you are.
Merge conflicts
A conflict happens when two branches changed the same lines. Git cannot choose, so it stops and marks the file. You will see markers like these:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> feature/slow-api
- Open the file and decide what the final text should be: one side, the other, or a mix.
- Delete the three marker lines so only the final text remains.
- Run
git add <file>, thengit commit(orgit rebase --continueduring a rebase). - Run the tests before you push. A conflict that is fixed wrongly still compiles.
git merge --abort or git rebase --abort takes you safely back to before you started. Small, short-lived branches are the best cure, because they conflict less.
Cherry-pick
git cherry-pick <commit> copies one commit onto the branch you are on. DevOps teams use it when a fix made on main must also go onto a release branch, without taking all the other changes. Prefer small, focused commits, because cherry-picking a commit that mixes five things is painful.
Tags and releases
A tag is a fixed name for one commit, normally used to mark a release such as v1.4.0. Pipelines can start a release build when a tag is pushed, and rolling back means deploying the previous tag. A common numbering scheme is semantic versioning: MAJOR.MINOR.PATCH, where a patch fixes bugs, a minor adds features without breaking things, and a major may break things.
git tag -a v1.4.0 -m "Release 1.4.0"
git push origin v1.4.0
git tag --list "v1.*"
Pull requests and code review
A pull request (PR) asks for your branch to be merged and gives others the chance to look first. Good PRs are small, have a clear title and description (what changed and why, and how you tested it) and link to the ticket. Good reviews are kind, specific and quick. Reviewers look for bugs, clarity, security and tests, and they separate must-fix from nice-to-have. Automation helps: the pipeline runs tests on every PR, and branch protection rules on main can require a passing pipeline and at least one approval before merging. That is how a review step becomes a safe gate in DevSecOps, not a suggestion.
Architecture diagram
Here is the idea as a simple picture. You can use the same diagram in your own notes.
gitGraph
commit id: "start"
branch feature
checkout feature
commit id: "add /health"
commit id: "tests"
checkout main
merge feature
commit id: "release v1.0" tag: "v1.0"
How to check it worked
git log --oneline --graphshows the history you expect after the rebase.- You can explain when to use revert and when to use reset.
- You can name the strategy your own team or project uses, and why.
How companies use it
Most companies use a pull request flow with required reviews and passing tests. Releases are marked with tags, and hotfixes are cherry-picked onto release branches. Git history is also the audit trail for GitOps.
Common errors and how to fix them
- Merge conflict. Open the file, choose the final text, remove the markers,
git addand commit. - Rejected push (non-fast-forward). Pull or fetch and rebase first, then push again.
- Committed a secret. Rotate the secret at once. Deleting the file does not remove it from history.
- Detached HEAD. Create a branch with
git switch -c nameto keep your work. - Lost commits after reset.
git reflogcan find them for a while.
Best practices
- Short-lived branches and small pull requests.
- Clear commit messages.
- Rebase private branches, never shared history.
- Protect
mainwith required checks. - Tag releases.
Interview questions with simple answers
- Merge vs rebase? Merge joins histories with a merge commit. Rebase replays your commits on top of another branch for a straight history.
- What is a pull request? A request to merge a branch, with review and checks.
- revert vs reset? Revert adds a commit that undoes an old one. Reset moves the branch back.
- What is cherry-pick? Copying one commit onto another branch.
- What is git stash? Shelving unfinished changes.
- What is a tag? A fixed name for a commit, often a release.
- What is trunk-based development? Everyone merges small changes into main often.
- What is Git flow? A model with develop, release and hotfix branches.
- How do you resolve a conflict? Edit the file, remove markers, add and commit.
- What is `--force-with-lease`? A safer force push that stops if someone else pushed.
Practical exercises
Try each one yourself before you look at the hint. Struggling for a few minutes is how the skill sticks.
- Make a branch, two commits and rebase it on main. Hint:
git rebase main. - Create a conflict on purpose and fix it. Hint: Edit the same line on two branches.
- Undo a pushed commit with revert. Hint:
git revert hash. - Tag a version and list tags. Hint:
git tag -a. - Use stash to switch branches mid-change. Hint:
git stashandgit stash pop.
Mini project: Team workflow rehearsal
With a friend or two accounts, create a repository, protect main, make a pull request, review it, and merge. Record the steps and tag a release.
Knowledge test
Answer the ten questions in the quiz at the end of this lesson. You pass with 60 percent. If an answer is wrong, read the explanation, then reread the part of the lesson it points to.