HOWTO · Git
How to Move Git Branch Pointer to Different Commit
Move a checked-out Git branch with git reset, or move another unchecked branch with git branch -f or git update-ref. Learn what each command changes and how to choose a target commit.
On this page
To move a Git branch pointer to a chosen commit, use git reset when the branch is checked out. To move a different branch while staying on your current branch, use git branch -f or git update-ref. git branch -f refuses to move a branch checked out in any worktree. git update-ref updates the reference directly without updating the index or working tree; using it on a checked-out branch can leave those files at the previous commit while HEAD points to the new one. Replace <target-commit> below with a commit hash, tag, or other revision expression that identifies the commit you want.
Move the checked-out branch to a commit with git reset
git reset moves the current branch to the selected commit. Choose its mode based on what should happen to the index and working tree. A soft reset moves the branch and leaves both as they are. The default mixed mode moves the branch and updates the index while leaving working-tree files as they are. A hard reset moves the branch and makes the index and tracked working-tree files match the target commit.
Before using git reset --hard, save any tracked changes you need: this command can discard uncommitted changes to tracked files. Untracked files are generally left alone, but Git can remove or overwrite them when they obstruct writing tracked paths from the target commit. The following self-contained example creates a temporary repository, makes two commits on main, and moves the checked-out branch back to the first commit:
set -eu
repo_dir=$(mktemp -d)
trap 'rm -rf "$repo_dir"' EXIT
cd "$repo_dir"
git init -q -b main
git config user.name Example
git config user.email example@example.com
printf 'first version\n' > note.txt
git add note.txt
git commit -q -m first
first_commit=$(git rev-parse HEAD)
printf 'second version\n' > note.txt
git commit -q -am second
git reset --hard "$first_commit" >/dev/null
printf 'Branch: %s\n' "$(git branch --show-current)"
printf 'Commit: %s\n' "$(git log -1 --format=%s)"
Branch: main
Commit: first
Use a revision such as HEAD~1 or a commit hash as the target when applying the command in your own repository. If you need to keep local tracked changes, consider git reset --soft <target-commit> or git reset --mixed <target-commit> and review the resulting index and working tree before committing. A reset changes the current branch only; it does not directly move another branch name.
Move a different, unchecked branch to a commit
To move a branch other than the one checked out, keep the target branch unchecked in the current worktree. git branch -f force-moves the named branch reference to the selected commit. For example, git branch -f feature <target-commit> moves feature to that commit. Confirm the branch name and target before using -f, since it deliberately replaces the branch’s current tip. It will refuse to move a branch checked out in a worktree.
You can also update the branch reference directly with git update-ref. Use the full ref name and a revision that resolves to the target commit: git update-ref -m "move feature to target" refs/heads/feature <target-commit>. The -m option records the supplied message in the reflog, which helps explain why the reference moved later. For this task, feature is a different, unchecked branch, so either command moves it while your current branch stays checked out. Unlike git branch -f, git update-ref does not refuse a checked-out branch: it changes the reference but leaves the index and working-tree files untouched. If you update the current branch this way, HEAD resolves to the new commit while the index and files still reflect the old one, so they can appear changed relative to the new commit.
git checkout -b feature <target-commit> creates and checks out a new branch; it does not move an existing feature branch. Checking out a commit directly detaches HEAD. Rebase has a different purpose: it replays commits onto a new base and can create new commit IDs, so it is not a direct way to point an existing branch at an arbitrary commit. Choose reset for the checked-out branch, or one of the reference commands above for a different unchecked branch.