Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can remote URL instead of a name be used to check out branches?

Tags:

git

git checkout -b <name> <remote>/<branch> seems to require a named remote. Is there a way to make it work given only an URL? The reason is that people on the team have different names for remotes (e.g. gitlab and origin vs origin and github), and I'd like to ignore that difference in a script.

One possibility is to give the name to the remote in the beginning of the script and delete it in the end, but I'd prefer to avoid it.

like image 342
Alexey Romanov Avatar asked Aug 12 '15 14:08

Alexey Romanov


People also ask

How do I checkout a remote branch with a different name?

To be precise, renaming a remote branch is not direct – you have to delete the old remote branch name and then push a new branch name to the repo. Step 2: Reset the upstream branch to the name of your new local branch by running git push origin -u new-branch-name .

How do I get branches from remote repository?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

What does it mean to checkout a remote branch?

Git checkout remote branch is a way for a programmer to access the work of a colleague or collaborator for the purpose of review and collaboration. There is no actual command called “git checkout remote branch.” It's just a way of referring to the action of checking out a remote branch.


1 Answers

No there is not any way to make the second parameter of git checkout -b work with a URL as opposed to the name of a branch (in the form of <remote>/<branch> or otherwise).

From the man page:

   git checkout -b|-B <new_branch> [<start point>]
       Specifying -b causes a new branch to be created as if git-branch(1) were
       called and then checked out. In this case you can use the --track or
       --no-track options, which will be passed to git branch. As a convenience,
       --track without -b implies branch creation; see the description of
       --track below.

       If -B is given, <new_branch> is created if it doesn’t exist; otherwise,
       it is reset. This is the transactional equivalent of

           $ git branch -f <branch> [<start point>]
           $ git checkout <branch>

       that is to say, the branch is not reset/created unless "git checkout" is
       successful.

The second parameter to git branch referred to as start point needs to be something that points to a commit in the existing repo. A branch (which is possibly not even fetched) addressed by URL will not achieve this. Moreover there is no way to encode a branch into a git URL as standard (github might have something, but I think not, else golang imports would be a lot easier ...)

If you want to make this work, you will need to:

  • Parse the output of git remote -v show to get the appropriate remote name
  • Execute a git fetch to ensure the relevant branch is pulled down (git fetch --all is probably your friend)
  • Append the branch name to the remote name, and git checkout -b that.

Here's a very lightly tested bash script:

#!/bin/bash
#
# usage: scriptname repo branchname
#
# where repo is the URL, branchname is the branchname to create
repo="$1"
branchname="$2"
remote=$(git remote -v show | perl -n -e 'if (m,^(\w+)\s+(\S+)\b, && $2 eq '"'${repo}'"') {print "$1\n" ; exit 0}')
if [ "$remote" == "" ] ; then
    echo cannot find "${repo}" 1>&2
    exit 1
fi
git fetch --all && git checkout -b "${branchname}" "remotes/${remote}/${branchname}"
like image 165
abligh Avatar answered Sep 18 '22 19:09

abligh