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

Re: [PATCH/RFC] Convenient support of remote branches in git-checkout

From
Junio C Hamano <junkio@cox.net>
Date
Nov 7, 2006, 00:13 UTC
Message-ID
<7vd580azbb.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<200611070026.16425.Josef.Weidendorfer@gmx.de>
Josef Weidendorfer <Josef.Weidendorfer@gmx.de> writes:
Show 12 quoted lines
> Example to hack on git's next branch:
>
>  git-clone --use-separate-remote http://www.kernel.org/pub/scm/git/git.git
>  cd git
>  git-checkout origin/next
>  <hack on next>
>  git pull (to merge patches from remote 'next')
>
> The checkout creates local branch 'next' to checkout read-only
> remote branch 'remotes/origin/next'. Additionally, it sets up
> 'remotes/origin/next' from remote repository 'origin' as
> default merge source for the new development branch.
I am disturbed by an inconsistency here.
Show 6 quoted lines
> +	if git-show-ref --verify --quiet -- "refs/heads/$newbranch"
> +	then
> +		echo "Proposed new branch '$newbranch' to checkout...
> +		echo "To checkout, specify a new branch name with -b"
> +		exit 1
> +	fi

This logic is guarding against already having a local branch that is called 'next', and that is why the "Proposed new branch" message needs to be there. One explanation of why 'next' exists in the local branch namespace in the first place is probably there are other remote branches than origin that have 'next' and the user previously checked it out. Or perhaps the user has already done this "checkout origin/next" once already.

I wonder if it is more consistent and easy to use to just make this:

	git checkout origin/next
a synonym to:
	git checkout -b origin/next remotes/origin/next
when remotes/origin/next exists and heads/origin/next does not.

Then "git checkout origin/next" would always mean "I want to switch to the branch I use to hack on the branch 'next' Junio has". Do it once and you will get exactly my tip, hack on it, switch out of it and then do it again and you won't lose your previous work but just switch to that branch.

That is, something like this...
---
diff --git a/git-checkout.sh b/git-checkout.sh
index 119bca1..f6486c6 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -4,6 +4,16 @@ USAGE='[-f] [-b <new_branch>] [-m] [<bra
 SUBDIRECTORY_OK=Sometimes
 . git-sh-setup
 
+# Automatic forking of local branch based on remote
+if test $# = 1 &&
+   git show-ref --verify --quiet -- "refs/remotes/$1" &&
+   ! git show-ref --verify --quiet -- "refs/heads/$1"
+then
+	set x -b "$1" "remotes/$1"
+	echo >&2 "* Forking local branch $1 off of remotes/$1..."
+	shift
+fi
+
 old_name=HEAD
 old=$(git-rev-parse --verify $old_name 2>/dev/null)
 new=
Previous: Josef WeidendorferNext: Josef Weidendorfer
Message 3 of 14 in “Convenient support of remote branches in git-checkout”
  1. Convenient support of remote branches in git-checkoutJosef Weidendorfer, Nov 6, 2006
  2. Josef WeidendorferNov 6, 2006
  3. Junio C HamanoNov 7, 2006
  4. Josef WeidendorferNov 7, 2006
  5. Junio C HamanoNov 7, 2006
  6. Junio C HamanoNov 7, 2006
  7. Josef WeidendorferNov 7, 2006
  8. Junio C HamanoNov 7, 2006
  9. Josef WeidendorferNov 7, 2006
  10. Karl HasselströmNov 7, 2006
  11. Josef WeidendorferNov 7, 2006
  12. Karl HasselströmNov 7, 2006
  13. Josef WeidendorferNov 7, 2006
  14. Junio C HamanoNov 7, 2006

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.