Thread (3 messages) flat view 3 messages, 3 authors, 2016-06-15

[RFC] Detached-HEAD reminder on commit?

From: Pieter de Bie <hidden>
Date: 2016-06-15 22:45:16
Subsystem: the rest · Maintainer: Linus Torvalds

Sometimes I work on a detached HEAD and then forget about it. If I then create
some commits and checkout another branch, I have to dig through my reflog to
find the older commits. I know that "git commit" adds has a "Not currently on
any branch", but it's not very noticeable and also doesn't work when you
specify a commit message on the command line.

I suggest to add some extra output to the STDOUT after a commit if we're on a
detached HEAD. The quick patch below adds output like:

Vienna:git pieter$ ./git commit --allow-empty -m"test"
Created commit 6ce62c8b: test
You are on a detached head, so this commit has not been recorded in a branch.
If you don't want to lose this commit, checkout a branch and then run:
	git merge 6ce62c8bfcfb341106f3587d1c141c3955c2544c

Are there any comments to this / strong opinions against such a change?

- Pieter
diff --git a/builtin-commit.c b/builtin-commit.c
index ec65ac5..bfe25f9 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -845,6 +845,8 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 {
        struct rev_info rev;
        struct commit *commit;
+       unsigned char sha[20];
+       const char* head = resolve_ref("HEAD", sha, 0, NULL);
 
        commit = lookup_commit(sha1);
        if (!commit)
@@ -877,6 +879,15 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
                printf("%s\n", buf.buf);
                strbuf_release(&buf);
        }
+
+       /* Are we on a detached HEAD? */
+       if (!strcmp("HEAD", head))
+               printf("You are on a detached head, so this commit "
+                       "has not been recorded in a branch.\n"
+                       "If you don't want to lose this commit, checkout a "
+                       "branch and then run:\n"
+                       "       git merge %s\n", sha1_to_hex(sha1));
+
 }
 
 static int git_commit_config(const char *k, const char *v, void *cb)
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help