# [PATCH] git-clone-pack: do not silently overwrite an existing branch 'origin'

3 messages from 2005-12-22 to 2005-12-22. Participants: Johannes Schindelin, Junio C Hamano.
Thread: https://gitlist.dev/t/2909

## Johannes Schindelin, 2005-12-22 17:59

Subject: [PATCH] git-clone-pack: do not silently overwrite an existing branch 'origin'
Message-ID: <Pine.LNX.4.63.0512221859110.20025@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0512221859110.20025%40wbgn013.biozentrum.uni-wuerzburg.de

```

When cloning a repository which already contains a branch called 'origin',
do not silently overwrite it with the remote 'master' ref.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	This happened to me with a project I track via git-cvsimport.
	The clone strangely suffered a time warp ;-)

 git-clone.sh |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

f0d4ef72573488410a1e8eb0198e347630d6e2c9
diff --git a/git-clone.sh b/git-clone.sh
index 280cc2e..e988964 100755
--- a/git-clone.sh
+++ b/git-clone.sh
@@ -204,6 +204,10 @@ then
 	head_points_at=`git-symbolic-ref HEAD`
 	case "$head_points_at" in
 	refs/heads/*)
+		if test -e .git/refs/heads/origin; then
+			chmod a-w .git/refs/heads/origin
+			echo "Warning: branch 'origin' exists already"
+		fi
 		head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
 		mkdir -p .git/remotes &&
 		echo >.git/remotes/origin \
-- 
1.0.0

```

## Junio C Hamano, 2005-12-22 21:12

Subject: Re: [PATCH] git-clone-pack: do not silently overwrite an existing branch 'origin'
Message-ID: <7vu0d0hm53.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vu0d0hm53.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.63.0512221859110.20025@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> When cloning a repository which already contains a branch called 'origin',
> do not silently overwrite it with the remote 'master' ref.

If we consider the "origin is special and is used to track
upstream" convention pretty much ingrained, then unconditionally
overwriting origin without saying anything is the right
approach.  After all, you do not care what upstream your
upstream is keeping track of, and you would want to use 'origin'
for the usual purpose of keeping track of upstream.

Otherwise, if we do not treat "origin" specially, we could
do something like this.  If "origin" exists, then we do not
overwrite upon clone, nor subsequent fetch.

I have a mild aversion to this change, though.  I'd much prefer
the third approach I am too lazy to code, which lets you say
what local branch name instead of 'origin' to keep track of
upstream from 'git clone' command line.

---

diff --git a/git-clone.sh b/git-clone.sh
index 280cc2e..edebee7 100755
--- a/git-clone.sh
+++ b/git-clone.sh
@@ -206,17 +206,22 @@ then
 	refs/heads/*)
 		head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
 		mkdir -p .git/remotes &&
-		echo >.git/remotes/origin \
-		"URL: $repo
-Pull: $head_points_at:origin" &&
-		cp ".git/refs/heads/$head_points_at" .git/refs/heads/origin &&
+		echo "URL: $repo" >.git/remotes/origin &&
+		if test -f ".git/refs/heads/origin"
+		then
+		    echo "Pull: $head_points_at:"
+		else
+		    echo "Pull: $head_points_at:origin"
+		    cp ".git/refs/heads/$head_points_at" \
+		        .git/refs/heads/origin &&
+		fi >>.git/remotes/origin &&
 		find .git/refs/heads -type f -print |
 		while read ref
 		do
-			head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
-			test "$head_points_at" = "$head" ||
-			test "origin" = "$head" ||
-			echo "Pull: ${head}:${head}"
+		    head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
+		    test "$head_points_at" = "$head" ||
+		    test "origin" = "$head" ||
+		    echo "Pull: ${head}:${head}"
 		done >>.git/remotes/origin
 	esac
 

```

## Johannes Schindelin, 2005-12-22 22:37

Subject: Re: [PATCH] git-clone-pack: do not silently overwrite an existing branch 'origin'
Message-ID: <Pine.LNX.4.63.0512222325330.12241@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0512222325330.12241%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <7vu0d0hm53.fsf@assigned-by-dhcp.cox.net>

```
Hi,

On Thu, 22 Dec 2005, Junio C Hamano wrote:

> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > When cloning a repository which already contains a branch called 'origin',
> > do not silently overwrite it with the remote 'master' ref.
> 
> If we consider the "origin is special and is used to track
> upstream" convention pretty much ingrained, then unconditionally
> overwriting origin without saying anything is the right
> approach.  After all, you do not care what upstream your
> upstream is keeping track of, and you would want to use 'origin'
> for the usual purpose of keeping track of upstream.

As I said, I use git to keep track of a few CVS projects. I like to fetch 
all branches, one being the upstream branch, so that I can compare what 
changes not yet in upstream.

> I have a mild aversion to this change, though.

So don't.

> I'd much prefer the third approach I am too lazy to code, which lets you 
> say what local branch name instead of 'origin' to keep track of upstream 
> from 'git clone' command line.

---

[PATCH] git-clone: Support changing the origin branch with -o

Earlier, git-clone stored upstream's master in the branch named 'origin',
possibly overwriting an existing such branch.

Now you can change it by calling git-clone with '-o <other_name>'.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	Totally untested, but obviously correct :-)

	It still overwrites any existing ref of the same name silently.

 git-clone.sh |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/git-clone.sh b/git-clone.sh
index 280cc2e..87ad477 100755
--- a/git-clone.sh
+++ b/git-clone.sh
@@ -67,6 +67,7 @@ use_local=no
 local_shared=no
 no_checkout=
 upload_pack=
+origin=origin
 while
 	case "$#,$1" in
 	0,*) break ;;
@@ -75,6 +76,8 @@ while
         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
           local_shared=yes; use_local=yes ;;
 	*,-q|*,--quiet) quiet=-q ;;
+	1,-o) usage;;
+	*,-o) origin="$2"; shift;;
 	1,-u|1,--upload-pack) usage ;;
 	*,-u|*,--upload-pack)
 		shift
@@ -208,14 +211,14 @@ then
 		mkdir -p .git/remotes &&
 		echo >.git/remotes/origin \
 		"URL: $repo
-Pull: $head_points_at:origin" &&
-		cp ".git/refs/heads/$head_points_at" .git/refs/heads/origin &&
+Pull: $head_points_at:$origin" &&
+		cp ".git/refs/heads/$head_points_at" .git/refs/heads/$origin &&
 		find .git/refs/heads -type f -print |
 		while read ref
 		do
 			head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
 			test "$head_points_at" = "$head" ||
-			test "origin" = "$head" ||
+			test "$origin" = "$head" ||
 			echo "Pull: ${head}:${head}"
 		done >>.git/remotes/origin
 	esac

```
