Move a private branch

Lesson 23 of 25 · Git: records, branches, and collaboration

Reset changes where the current branch points. It is appropriate only after confirming that moving the commits will not disrupt collaborators. The three commit-form modes differ in what else they change:

Mode Current branch Index Working tree
--soft Moves to target Unchanged Unchanged
--mixed (default) Moves to target Reset to target Unchanged
--hard Moves to target Reset to target Tracked files reset to target

Assume a clean private branch ending in A──B──C, and that C has a parent. Before moving it, inspect git status, git log --oneline, and git show HEAD~1. The notation HEAD~1 means the first parent of the current commit; it fails if there is none. An unused backup branch name can protect the committed tip:

git branch backup-before-reset
git reset --soft HEAD~1
git diff --staged

Now the branch points to B while the index and working tree still represent C. With the stated clean starting state, C’s difference from B appears staged for a replacement commit. Existing staged or unstaged changes would also remain and may make the result different. A backup branch does not save uncommitted edits.

A mixed reset to a chosen commit leaves working files alone but replaces the staging arrangement; inspect git diff and git diff --staged afterwards. A hard reset is much more destructive:

Destructive: Only use the next command in a disposable repository, or after independently backing up needed work, confirming the target and accepting loss. It discards staged and unstaged tracked changes; untracked paths obstructing tracked paths may also be deleted. Ordinary untracked files are not generally removed. It cannot safely rewrite shared history by itself.

git reset --hard <commit-id>

Replace the placeholder with the inspected actual commit ID. The old tip may remain accessible temporarily; see finding a previous tip. Do not automatically force-push after a reset: remote copies and collaborators need a coordinated plan.