When working with Git, sometimes you may want to create a new branch based on the commit before the current HEAD. In Git notation, HEAD^ refers to the parent of the current commit.
To create a branch from HEAD^, run:
git checkout -b <new-branch-name> HEAD^This will:
- Create a new branch named
<new-branch-name>. - Point it to the commit one step before the current
HEAD. - Switch your working directory to the new branch.
git checkout -b fix-before-change HEAD^This command creates a branch called fix-before-change starting from the commit just before your latest commit.
HEAD^means one commit beforeHEAD.HEAD^^means two commits before.- Alternatively, you can use the tilde (
~) notation:HEAD~3means three commits before.
If you only want to move temporarily to the parent commit (without creating a branch), you can use:
git checkout HEAD^This places you in a detached HEAD state.