Git for DevOps Teams: Branches, Rebase, Stash, Tags and Pull Requests

DevOps and Cloud Engineering: Complete Course from Beginner to Advanced · Git, GitHub and First Pipeline · 8 min read · Updated

A change in GitHub flow

  1. You do the work on a short-lived branch.
  2. You open a pull request. Tests run and a teammate reviews it.
  3. After approval, it merges into main, which stays deployable.
  4. A tag marks the release commit, so you can deploy it, and roll back to an earlier tag.

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 main often. 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: main is 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 develop and main branches 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 stash puts your unfinished changes on a shelf so you can switch branches with a clean folder. git stash pop brings them back, and git stash list shows the shelf.
  • git restore file throws away changes to a file that you have not committed. git reset --soft HEAD~1 undoes the last commit but keeps the changes ready to commit again. git reset --hard destroys 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 init starts a repository and git clone <url> copies an existing one.
  • git add stages changes, git commit -m "message" records them.
  • git fetch downloads new work without changing your files. git pull is fetch plus merge. git push uploads your commits.
  • git status, git log --oneline and git diff show 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
  1. Open the file and decide what the final text should be: one side, the other, or a mix.
  2. Delete the three marker lines so only the final text remains.
  3. Run git add <file>, then git commit (or git rebase --continue during a rebase).
  4. 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 --graph shows 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 add and 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 name to keep your work.
  • Lost commits after reset. git reflog can find them for a while.

Best practices

  • Short-lived branches and small pull requests.
  • Clear commit messages.
  • Rebase private branches, never shared history.
  • Protect main with required checks.
  • Tag releases.

Interview questions with simple answers

  1. Merge vs rebase? Merge joins histories with a merge commit. Rebase replays your commits on top of another branch for a straight history.
  2. What is a pull request? A request to merge a branch, with review and checks.
  3. revert vs reset? Revert adds a commit that undoes an old one. Reset moves the branch back.
  4. What is cherry-pick? Copying one commit onto another branch.
  5. What is git stash? Shelving unfinished changes.
  6. What is a tag? A fixed name for a commit, often a release.
  7. What is trunk-based development? Everyone merges small changes into main often.
  8. What is Git flow? A model with develop, release and hotfix branches.
  9. How do you resolve a conflict? Edit the file, remove markers, add and commit.
  10. 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.

  1. Make a branch, two commits and rebase it on main. Hint: git rebase main.
  2. Create a conflict on purpose and fix it. Hint: Edit the same line on two branches.
  3. Undo a pushed commit with revert. Hint: git revert hash.
  4. Tag a version and list tags. Hint: git tag -a.
  5. Use stash to switch branches mid-change. Hint: git stash and git 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.

Frequently asked questions

Should I use merge or rebase?

Use rebase to keep your own private branch up to date, and merge or squash-merge when the pull request is accepted. Never rebase commits that others have already pulled.

What is the difference between git revert and git reset?

Revert adds a new commit that undoes an earlier one and is safe on shared branches. Reset moves the branch pointer back and can erase history, so use it only on your own unshared work.

What should I do if I committed a secret?

Treat it as leaked: revoke or rotate the secret straight away, then remove it from the code. Deleting the file in a later commit does not remove it from history.

Practise with interview questions