# [PATCH] fix reflog entries for "git-branch"

1 messages from 2007-01-19 to 2007-01-19. Participants: Junio C Hamano.
Thread: https://gitlist.dev/t/6438

## Junio C Hamano, 2007-01-19 19:51

Subject: [PATCH] fix reflog entries for "git-branch"
Message-ID: <7vy7nyg5ha.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vy7nyg5ha.fsf%40assigned-by-dhcp.cox.net

```
Even when -l is not given from the command line, the repository
may have the configuration variable core.logallrefupdates set,
or an old-timer might have done ": >.git/logs/refs/heads/new"
before running "git branch new".  In these cases, the code gave
an uninitialized msg[] from the stack to be written out as the
reflog message.

This also passes a different message when '-f' option is used.
Saying "git branch -f branch some-commit" is a moral equilvalent
of doing "git-reset some-commit" while on the branch.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
diff --git a/builtin-branch.c b/builtin-branch.c
index c760e18..bbac9dc 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -316,6 +316,7 @@ static void create_branch(const char *name, const char *start_name,
 	struct commit *commit;
 	unsigned char sha1[20];
 	char ref[PATH_MAX], msg[PATH_MAX + 20];
+	int forcing = 0;
 
 	snprintf(ref, sizeof ref, "refs/heads/%s", name);
 	if (check_ref_format(ref))
@@ -326,6 +327,7 @@ static void create_branch(const char *name, const char *start_name,
 			die("A branch named '%s' already exists.", name);
 		else if (!strcmp(head, name))
 			die("Cannot force update the current branch.");
+		forcing = 1;
 	}
 
 	if (start_sha1)
@@ -342,11 +344,15 @@ static void create_branch(const char *name, const char *start_name,
 	if (!lock)
 		die("Failed to lock ref for update: %s.", strerror(errno));
 
-	if (reflog) {
+	if (reflog)
 		log_all_ref_updates = 1;
+
+	if (forcing)
+		snprintf(msg, sizeof msg, "branch: Reset from %s",
+			 start_name);
+	else
 		snprintf(msg, sizeof msg, "branch: Created from %s",
 			 start_name);
-	}
 
 	if (write_ref_sha1(lock, sha1, msg) < 0)
 		die("Failed to write ref: %s.", strerror(errno));

```
