I changed things and GitHub changed too: pull, merge, push

careful beginner #pull#merge#push#main#sync#diverged

When to use this: You worked directly on main locally, GitHub also has new commits (another PC, a merged PR), and you need to combine them and push.

Be careful with this one. A merge can stop with conflicts. That is normal. Do not push until git status is clean.

Before you start

Steps

  1. Commit what you have

    terminal
    git add -A
    git commit -m "Describe my local change"

    A pull needs a clean working tree, or your edits may be overwritten or block the merge.

  2. Preview what is coming

    terminal
    git fetch
    git log HEAD..origin/main --oneline

    See the remote commits you do not have yet. Optional but it removes surprises.

  3. Pull and merge GitHub's changes into your main

    terminal
    git pull --no-rebase

    Fetches and merges origin/main into your main. Git opens an editor for the merge commit message: accept it (save and close). If Git reports CONFLICT, go to the merge-conflict scenario, then come back.

    Expected output
    Merge made by the 'ort' strategy.
     src/pages/index.astro | 3 +++
  4. Test it

    terminal
    npm run build

    Make sure the combined result still works before you publish it.

  5. Push the result back

    terminal
    git push

    Uploads your commits and the merge commit. Vercel will deploy main to production.

How to verify it worked

  • git status -sb shows neither ahead nor behind.
  • git log --oneline --graph -n 10 shows both lines of work joined by a merge commit.

How to undo it

Merged but have NOT pushed, and want to go back to before the pull:

terminal
git reset --hard ORIG_HEAD

Already pushed a bad merge: do not rewrite history. Revert the merge commit with git revert -m 1 <merge-commit-id> and push.

Common errors and fixes

CONFLICT (content): Merge conflict in <file> Open the file, choose the final text, remove the conflict markers, git add <file>, then git commit. See the merge-conflict scenario.
rejected ... fetch first when pushing Someone pushed again while you worked. Repeat the pull, then push.
Editor opened (Vim) and you are stuck Press Esc, type :wq, press Enter to save and close. Or :q! to abort the merge commit text.

Q&A

Why did Git create a merge commit?

Both histories had new commits, so Git needs a commit that joins them. If you had no local commits it would just fast-forward.

Should I merge or rebase when pulling?

Merge is the safe default: it never rewrites commits. Rebase gives a straighter history but rewrites your local commits, so only use it on commits that are not pushed yet.

Is it safe to work directly on main?

For a solo project, yes, but every push to main deploys to production. A branch plus pull request gives you a preview first.

Practice these as flashcards →

Terms in this scenario

Related scenarios

My notes

Saved in this browser only.