What’s the proper full-cycle workflow?
——- LOCAL —–
git init – create repo
git add README.md
git commit -mInitial
git remote add origin
git push origin main <– pre-configured to allow push to create
git checkout -b feature/test
git add README.md
git commit -m”Updated README”
git push origin feature/test

—- REMOTE —
create pull request from feature/test -> merge to main

—- LOCAL —–
git checkout main
git fetch origin
git pull origin main

git checkout -b feature/test2
cycle repeats?

Checking the status of a local branch relative to main is a common workflow, but since Git is decentralized, your local machine doesn’t automatically “know” what happened on GitHub or GitLab until you sync up.

Here is the most reliable way to check if your branch’s work has been safely merged into main.


1. The “Merged” Command (Quickest)

The most direct way to check if a branch’s commits are already contained within your local main branch is using the --merged flag.

  1. Update your local view of the remote repository:git fetch origin
  2. Switch to your main branch:git checkout main
  3. List merged branches:git branch --merged

If your local branch name appears in that list, its changes are officially part of main.


2. Using git cherry (The “Is my work there?” check)

If you want to see if the specific commits from your feature branch exist on main (even if they were squashed during the PR process), use git cherry.

  • Command: git cherry -v main <your-branch-name>
  • Reading the output:
    • If the output is empty, all your changes are already in main.
    • If you see lines starting with +, those commits are not in main yet.

3. Visualizing the Relationship

Sometimes it’s easier to see the “divergence” or “convergence” in a graph format.

  • Command: git log --oneline --graph --dot --decorate main.. <your-branch-name>

This shows you exactly what commits are sitting on your branch that haven’t made it to main. If it returns nothing, your branch has no unique commits left to merge.


4. Checking PR Status via CLI

If you use the GitHub CLI (gh), you can skip the git math and ask the API directly:

  • Command: gh pr status
  • Command: gh pr list --state merged --head <your-branch-name>

This will tell you the literal status of the Pull Request linked to that branch.

Summary Table

MethodBest For…
git branch --mergedChecking if the branch is safe to delete.
git cherryChecking if specific work exists on main (even if squashed).
gh pr listConfirming the administrative status of the PR itself.

Pro Tip: Once you’ve confirmed a branch is merged, you can clean it up and sync your local list with git fetch -p (prune), which removes references to remote branches that no longer exist.

The Refined Full-Cycle Workflow

1. The Setup (Once per project)

  • git init
  • git remote add origin <url>
  • git push -u origin main (The -u sets up “upstream” tracking so you can just type git push later).

2. The Feature Cycle (The “Inner Loop”)

  • Update Main first: Always start from the freshest code.
    • git checkout main
    • git pull origin main
  • Branch: git checkout -b feature/new-idea
  • Work: git add / git commit
  • Push: git push -u origin feature/new-idea

3. The “Sync” Step (What was missing)

If you are working on a feature for several days, main might move forward (someone else merges a PR). To avoid a mess later, pull main into your feature branch occasionally:

  • git fetch origin
  • git merge origin/main (or git rebase main if you prefer a clean history).

4. The Remote Phase

  • Create PR: Via GitHub/GitLab UI.
  • Merge PR: Using the “Squash and Merge” or “Merge” button.

5. The Cleanup (What was missing)

After the PR is merged, your local machine still has that old branch. Clean it up so your git branch -vv stays readable:

  • git checkout main
  • git pull (Get the new merge commit you just created on the remote).
  • Delete local branch: git branch -d feature/new-idea
  • Prune remote tracking: git fetch -p (This tells your local git: “If the branch was deleted on GitHub, forget about it here too”).

Comparison: Your Workflow vs. The Pro Workflow

ActionYour VersionThe “Pro” AdditionWhy?
New Branchgit checkout -bgit pull origin main firstPrevents starting work on an outdated version.
Pushinggit pushgit push -uSets up a permanent link between local and remote.
MergingPR on Remotegit fetch -pClears out “ghost” branches from your local list.
Old BranchesKeep themgit branch -dPrevents the “left behind” branches you saw earlier.

One Small Correction

In your example, you did:

git fetch origin

git pull origin main

Note: git pull actually performs a fetch and a merge in one command. If you are on main, you usually only need git pull origin main.

Summary Checklist for feature/test2:

  1. git checkout main
  2. git pull
  3. git branch -d feature/test (Kill the old one!)
  4. git checkout -b feature/test2

Leave a Reply