git merge: Usage, Options & Common CI Errors
git merge combines the changes from another branch into your current branch.
Merging integrates work and, when histories diverged, can produce conflicts that a script must detect and stop on.
What it does
git merge brings the commits from a named branch into the current branch, creating a merge commit unless the merge can fast-forward.
Common usage
git merge feature
git merge --no-ff feature # always create a merge commit
git merge --squash feature # combine changes, no merge commit
git merge --abort # bail out of a conflicted mergeOptions
| Flag | What it does |
|---|---|
| --no-ff | Force a merge commit even if fast-forward is possible |
| --ff-only | Refuse to merge if a merge commit is needed |
| --squash | Stage the combined result without committing |
| --abort | Restore pre-merge state on conflict |
| -X ours / -X theirs | Auto-resolve conflicting hunks one side |
Common errors in CI
CONFLICT (content): Merge conflict in <file> / "Automatic merge failed; fix conflicts and then commit the result" - the merge stops with conflict markers. In automation, detect it (the command exits non-zero), then either resolve, choose a side with -X, or git merge --abort to leave history clean.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
- uses: actions/checkout@v4
with:
fetch-depth: 0 # history, tags, and git describe all need this
- run: |
git rev-parse --is-shallow-repository # expect false
git rev-parse --abbrev-ref HEAD # prints HEAD when detached