GIT – List Remote Branches

GIT – List Remote Branches

While working in a version control system is a normal to use branches. It actually is a good practice to work on a code by creating a branch with several different people can work on their branch without actually disturbing the actual code used in production.

Sometime you have to find out the branches available on the remote server before you can figure out the branch that you indend to work on.

Note: The commands here basically assume that you have cloned a remote repository and the current working directory is where the repo is cloned.

So once you have clone you the GIT repository you can check the current branch you have cloned. By default it should be master.

Git – Check the current branch

The simplest way to check the existing branch you are in is using the git branch

[[email protected] my_repo]# git branch

Output:

[[email protected] my_repo]# git branch
* master

Git – List Remote Branches

Using the -r flag for git branch we can check the remotely available branches for the current repository.

[[email protected] my_repo]# git branch -r

Output:

[[email protected] my_repo]# git branch -r
  origin/v1-2-stable
  origin/v2-0-stable
  origin/v2-3-patch
  origin/v3-0-stable
  origin/v3-1-beta
  origin/v3-2-rc
  origin/HEAD
  origin/master

Git – List Local and Remote Branches

Using the -a flag for git branch we can list all the branches available locally as well as remotely for the current repository.

[[email protected] my_repo]# git branch -a

Output:

[[email protected] my_repo]# git branch -a
* master
  remotes/origin/v1-2-stable
  remotes/origin/v2-0-stable
  remotes/origin/v2-3-patch
  remotes/origin/v3-0-stable
  remotes/origin/v3-1-beta
  remotes/origin/v3-2-rc
  remotes/origin/HEAD
  remotes/origin/master

So once you know the name of the branch you intend to work on, it becomes easier for you to checkout the specific branch.

Also, there are other methods to list the remote branch in GIT.

Git – List Remote Branches – Additional methods

Git – List Remote Branches – git remote show

The following command should provide you the information on the remote branches within a repository as well.

[[email protected] my_repo]# git remote show origin

Output:

[[email protected] my_repo]# git remote show origin
* remote origin
  Fetch URL: [email protected]:my_repo.git
  Push  URL: [email protected]:my_repo.git
  HEAD branch: master
  Remote branches:
    v1-2-stable      tracked
    v2-0-stable      tracked
    v2-3-patch	     tracked
    v3-0-stable	     tracked
    v3-1-beta	     tracked
    v3-2-rc	     tracked

Git – List Remote Branches – git ls-remote

And there is another one as well.

[[email protected] my_repo]# git ls-remote –heads origin

Output:

[[email protected] my_repo]# git ls-remote --heads origin
xxxxxxxxxxxxxxSHA1HASHxxxxxxxxxxxxxxxxxx	refs/heads/v1-2-stable
xxxxxxxxxxxxxxSHA1HASHxxxxxxxxxxxxxxxxxx	refs/heads/v2-0-stable
xxxxxxxxxxxxxxSHA1HASHxxxxxxxxxxxxxxxxxx	refs/heads/v2-3-patch
xxxxxxxxxxxxxxSHA1HASHxxxxxxxxxxxxxxxxxx	refs/heads/v3-0-stable
xxxxxxxxxxxxxxSHA1HASHxxxxxxxxxxxxxxxxxx	refs/heads/v3-1-beta
xxxxxxxxxxxxxxSHA1HASHxxxxxxxxxxxxxxxxxx	refs/heads/v3-2-rc

The command ls-remote provides the SHA1 hash for the recent/latest commit to the branch. Using the option –heads above lists the branch name only, as the command ls-remote may list the tags as well.

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.