Kamafeather
Kamafeather

Reputation: 9845

How to git-commit from a branch to a detached HEAD?

My ref HEAD points the branch foo, that points to the commit 123abc; and I have some staged work.

How to git commit that work, moving the HEAD to the newly created commit, but without advancing the branch?

(hence: leaving foo point to 123abc)

Is it possible to do it with a single git command?

Upvotes: 1

Views: 365

Answers (3)

ElpieKay
ElpieKay

Reputation: 30868

If HEAD is directly pointing to commit 123abc, it's already on detached HEAD state instead of on foo. git commit will create a new commit and move HEAD to the new commit, leaving foo unmoved.

If HEAD points at refs/heads/foo and refs/heads/foo points at 123abc, you can run git checkout 123abc and then make the commit.

Upvotes: 1

Quentin
Quentin

Reputation: 63124

Simply detach, then commit:

git checkout --detach
git commit -m "Commit as usual"

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

You could just commit and then do a reset:

# from foo branch
git commit -m 'your work here'
git reset --soft HEAD~1

This would move the HEAD pointer back one commit to 123abc, but would also stage all the work in that commit.

There are other types of reset (mixed, hard), which do variants of the above. Without knowing your end goal, it isn't clear what kind of reset you should use. In general, though, moving the HEAD back one commit is something you would do if you wanted to rewrite the prior HEAD commit.

Upvotes: 0

Related Questions