Skip to content

Chapter 3 of 7

Branching, Merging, and History Shaping

Branches are Git's mechanism for parallel development, allowing multiple lines of work to coexist without interfering. The command git branch <branch-name> creates a new branch that points to the current commit, while git checkout -b <branch-name> both creates a branch and immediately switches to it. To move between existing branches, git checkout <branch> updates the working directory to reflect the selected branch's state. Listing branches is done with git branch for local branches, git branch -r for remote ones, and git branch -a for everything; the active branch is always marked with an asterisk.

Once work in a branch is complete, it must be integrated back into another branch, typically the main line of development. The command git merge <branch> incorporates the named branch's changes into the current one. When the target branch has not diverged, Git performs a fast-forward merge by simply moving the branch pointer forward, producing no additional commit. The --no-ff option forces a merge commit even in this situation, preserving evidence that a branching workflow took place. To remove a branch that is no longer needed, git branch -d <branch> safely deletes it if its changes have already been merged; -D forces deletion of unmerged branches.

An alternative to merging is git rebase <branch>, which replays the current branch's commits on top of another branch to create a linear history. Because rebasing rewrites commit hashes, it should be avoided on branches that others have already pulled or cloned. Compared with merge, which preserves the exact branching topology using a merge commit, rebase produces a cleaner timeline but alters commit identifiers. When developers need to set aside incomplete work temporarily, git stash saves uncommitted changes and reverts the working directory to match the last commit; the work can later be restored with git stash pop. Stashes can be reviewed with git stash list and applied selectively using indexed entries like stash@{0}, while git stash drop <stash> permanently removes an entry.

All chapters
  1. 1Introduction to Git and Its Core Concepts
  2. 2Essential Daily Commands
  3. 3Branching, Merging, and History Shaping
  4. 4Collaborating with Remote Repositories
  5. 5Undoing, Reverting, and Recovering
  6. 6Tags, Submodules, and Productivity Tools
  7. 7Customizing and Maintaining Your Repository

Drill it

Reading is not remembering. These come from the Git Version Control deck:

Q

What is Git?

Git is a distributed version control system that tracks changes in source code during software development, allowing multiple developers to collaborate efficien...

Q

How do you initialize a new Git repository?

Use the command git init in the root directory of your project.This creates a hidden .git subdirectory containing all repository data.

Q

What does <code>git clone</code> do?

git clone &lt;url&gt; creates a copy of an existing remote repository on your local machine.It includes all branches, tags, and history.

Q

What is the purpose of <code>git add</code>?

git add &lt;file&gt; stages changes in the working directory for the next commit.You can stage all changes with git add . or specific files.