git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Instructions concerning detached head lead to lost local changes

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Feb 1, 2007, 23:49 UTC
Message-ID
<Pine.LNX.4.64.0702011536570.15057@woody.linux-foundation.org>
In-Reply-To
<87mz3xa3vr.wl%cworth@cworth.org>
On Thu, 1 Feb 2007, Carl Worth wrote:
Show 5 quoted lines
> 
> But I found that after "git checkout -f master" I lost the dirty
> changes I had made. And I don't see anything in the message above
> telling me a better way to get from the state I was in to the state I
> wanted.
Yeah, I think "-f" was a mistake. 

There was already real meaning for "-f": force update of all the files, regardless of whether they are dirty or unmerged or anything else.

And that meaning is totally independent of the new meaning of -f, which says "drop the old detached head". Sometimes you might want to use them together, but they shouldn't really be tied together.

Maybe something like this, which adds "--drop" as the flag to say "ok to drop the old detached head"? I'm not married to that name, it's meant more as an RFC than as anything to be applied as-is.

NOTE! This makes "-f" _imply_ "--drop" too, since it does make sense that if somebody just wants to force it, he can - regardless of any dirty state, regardless of the current state of the HEAD.

But using just "git checkout --drop new-head" will have the old "dirty state follows along with branch changes" semantics.

		Linus

PS. I don't think "--drop" really makes much sense as a name. Especially when written out like that, it sounds like we're dropping the *new* branch, not the old one. Maybe something like "--no-check" instead?

---
diff --git a/git-checkout.sh b/git-checkout.sh
index deb4795..8e852df 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -11,6 +11,7 @@ oldbranch=$(git-symbolic-ref $old_name 2>/dev/null)
 new=
 new_name=
 force=
+drop=
 branch=
 newbranch=
 newbranch_log=
@@ -36,10 +37,14 @@ while [ "$#" != "0" ]; do
 		;;
 	"-f")
 		force=1
+		drop=1
 		;;
 	-m)
 		merge=1
 		;;
+	--drop)
+		drop=1
+		;;
 	--)
 		break
 		;;
@@ -163,7 +168,7 @@ If you want to create a new branch from this checkout, you may do so
 elif test -z "$oldbranch" && test -n "$branch"
 then
 	# Coming back...
-	if test -z "$force"
+	if test -z "$drop"
 	then
 		git show-ref -d -s | grep "$old" >/dev/null || {
 			echo >&2 \
Previous: Carl WorthNext: Junio C Hamano
Message 2 of 9 in “Instructions concerning detached head lead to lost local changes”
  1. Carl WorthFeb 1, 2007
  2. Linus TorvaldsFeb 1, 2007
  3. Junio C HamanoFeb 2, 2007
  4. Carl WorthFeb 2, 2007
  5. Carl WorthFeb 2, 2007
  6. Nicolas PitreFeb 2, 2007
  7. Theodore TsoFeb 2, 2007
  8. Nicolas PitreFeb 2, 2007
  9. Junio C HamanoFeb 2, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.