Fast-forward or merge commit: learn what Git changes, and which branch receives the work.
First switch to the receiving branch. Then name the branch whose work you want to bring in. git merge feature/greeting while on main updates main, not feature/greeting.
git status
git switch main
git merge feature/greetingStart with a clean working tree. Committing or stashing beforehand makes it easier to recover if you change your mind. Merging is local: it does not push anything to GitHub.
Choose a history shape. This is a visual model, not terminal output.
A → B → C main moves from A to C. No new commit.
Main has no separate commits. Git only moves its pointer forward.
Main was behind the feature branch. Moving the main pointer includes the new commits without making another commit.
Both histories have new work. Git combines their changes and records a commit with two parents.
Git cannot combine some changes automatically. It pauses for you to resolve them. Chapter 12 walks through that.
This continues the Chapter 10 repository. After the fast-forward, create feature/footer and commit a new footer.txt containing goodbye. Back on main, commit a different new file, title.txt, containing our project. Those non-overlapping changes produce a merge commit.
The transcript was recorded locally on Git 2.34.1. File edits and the two intermediate commits are described above rather than printed. Hashes and strategy wording may differ in your version.
$ git switch main
Switched to branch 'main'
$ git merge feature/greeting
Updating af9f623..d1ebb55
Fast-forward
greeting.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline --decorate
d1ebb55 (HEAD -> main, feature/greeting) Add welcome
af9f623 Start greeting
$ git switch -c feature/footer
Switched to a new branch 'feature/footer'
$ git switch main
Switched to branch 'main'
$ git merge feature/footer --no-edit
Merge made by the 'ort' strategy.
footer.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 footer.txt
$ git log --graph --oneline --all --decorate
* 23b3088 (HEAD -> main) Merge branch 'feature/footer'
|\
| * 69be6d2 (feature/footer) Add footer
* | 5e0cf4a Add title
|/
* d1ebb55 (feature/greeting) Add welcome
* af9f623 Start greeting
git merge --ff-only feature/greeting permits only a fast-forward; it refuses a diverged history. git merge --no-ff feature/greeting asks for a merge commit even when a fast-forward would be possible. Neither is the right default for every team. Follow your repository's convention.
To check the result, use git log --graph --oneline --all --decorate and run your project's tests. A merge that completes is not proof that the combined application works.
No. It advances the receiving branch to an existing commit.
Main. Git merges into the current branch.
Official git merge documentation · Git book: Basic Branching and Merging