How do you list branches in Git?
git branch lists local branches, with the currently checked-out branch marked. Options can also show remote-tracking branches or both local and remote-tracking branches.
Git & GitHub interview questions 26-50: Remotes, Fetch, Pull, Push, Merge, Conflicts, Rebase & Stash.
Open a question for the full answer, code and interview guidance.
git branch lists local branches, with the currently checked-out branch marked. Options can also show remote-tracking branches or both local and remote-tracking branches.
A local branch can be renamed with git branch -m. If the renamed branch has already been published, the remote branch and upstream configuration may also need to be updated.
git branch -d deletes a local branch when Git considers the deletion safe, such as when its work has been merged appropriately. The -D option forces local deletion even when Git detects unmerged work.
A remote is a named reference to another repository location, commonly hosted on GitHub or another Git server. It stores connection information that Git uses for operations such as fetch and push.
origin is simply the conventional default name Git gives to the source remote when a repository is cloned. It is not a special Git keyword that always means GitHub or the main repository.
git remote add creates a named remote. git remote set-url changes its URL, and git remote remove deletes the remote configuration from the local repository.
git fetch downloads new commits, branches, tags, and related references from a remote without automatically merging those changes into the currently checked-out branch.
git pull retrieves changes from a remote and then integrates them into the current branch. By default this commonly means fetch followed by merge, although configuration or command options can make pull use rebase instead.
git fetch downloads remote history and updates remote-tracking references without changing the current branch's working history. git pull fetches and then attempts to integrate the fetched changes into the current branch.
git push sends local references and the commits needed to update a remote repository. A common use is publishing local branch commits to GitHub.
An upstream branch is the branch that a local branch is configured to track, commonly a remote-tracking branch such as origin/main or origin/feature/search.
A remote-tracking branch is a local reference that records Git's last known state of a branch in a remote repository. Names such as origin/main and origin/develop are common examples.
A merge combines development histories. Git finds the relevant common history and incorporates changes from another branch into the current branch.
A fast-forward merge is possible when the current branch has no new commits after the point where the other branch diverged. Git can simply move the current branch reference forward to the newer commit.
A three-way merge combines two branch tips using their common ancestor as a third reference point. Git compares how each side changed relative to that ancestor and creates a merged result.
A merge conflict occurs when Git cannot safely determine how competing changes should be combined. A common example is when two branches modify the same lines differently, although conflicts can also involve file deletion, renaming, or other incompatible changes.
First inspect git status to identify conflicted files. Open each file, understand both versions, and edit it into the correct final result. Conflict markers such as <<<<<<<, =======, and >>>>>>> must not remain unless they are intentionally part of the file content.
If a merge is in progress and you decide not to complete it, git merge --abort attempts to restore the pre-merge state.
Rebase moves or reapplies commits onto a different base commit. A common workflow takes commits from a feature branch and replays them on top of the latest main branch, producing a more linear history.
Merge combines histories while preserving their existing branch structure and may create a merge commit. Rebase reapplies commits onto a new base and rewrites those commit identities, often producing a linear history.
Interactive rebase allows selected commits to be reordered, combined, edited, reworded, or removed while rewriting a portion of history. It is commonly used to clean up local feature-branch commits before they are shared.
When a rebase stops because of a conflict, resolve the affected files and stage the resolutions. Then run git rebase --continue so Git can continue replaying commits.
git stash temporarily saves selected working-tree and index changes so you can return to a cleaner working state without creating a normal project commit. It is useful when unfinished work must be set aside briefly to switch branches or handle an urgent task.
git stash apply reapplies a stash but normally keeps the stash entry in the stash list. git stash pop reapplies it and, when successful, removes that stash entry.
git reset moves a branch reference and can also change the index and working tree depending on the selected mode. It is commonly used to adjust local history or staging, but resetting published history can disrupt collaborators.