Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if git remote exists before first push

Tags:

git

bash

I'm writing a bash script and I need a test to see whether a given remote exists.

Suppose, for concreteness, that I want to test whether the remote faraway exists. If I've pushed something to faraway, I can do if [ -d .git/refs/remotes/faraway ]; then .... But as far as I can see, the alias faraway can still be defined even if .git/refs/remotes/faraway does not exist.

One other option is to parse through the output of git remote and see if faraway appears there. But I'm wondering whether there is an easier way of checking whether faraway is defined, regardless of whether .git/refs/remotes/faraway/ exists.

like image 448
apc Avatar asked Aug 29 '12 03:08

apc


People also ask

How do I check my git remote status?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands. If you approve the changes a remote branch contains, you can merge it into a local branch with a normal git merge .

How do I know if a repository exists?

For Bitbucket, the following URL can be used: api.bitbucket.org/2.0/repositories{user}/{repo_name} If the Repository doesn't exist: it returns {'message' : Repository user/repo_name not found} If the Repository exists, it returns Forbidden .

How to fix ‘no such remote’ error in Git?

fatal: No such remote: Remote origin already exists on ‘git push’ to a new repository; Solution. There are many ways we can fix this. Remove origin name from the given existing repository. First, delete the local name origin for given remote repository using following command

How to push a local branch to a remote repository in Git?

The basic command for pushing a local branch to a remote repository is git push. This command has a variety of options and parameters you can pass to it, and in this article you'll learn the ones that you will use the most often.

Why check this list before Git push?

Check this list before git push! Every developer knows about the clean code and architecture rules, and we cannot underestimate how important it is to keep the code in the project properly organized, and at the same time readable and maintainable.

How to fix Git fatal remote origin already exists error?

Once git is initialized, we must either create a new repository or clone an existing repository to map this to the existing directory. if this directory creates in the nested folder of any git repository, the git command throws git fatal: remote origin already exists error.


2 Answers

One thought: You could test exit status on git ls-remote faraway. This will actually force communication with the remote, instead of just looking for its presence or absence locally.

if git ls-remote --exit-code faraway; then     .... fi 
like image 165
Christopher Avatar answered Oct 22 '22 00:10

Christopher


Another way to check if faraway is defined in .git/config:

if git config remote.faraway.url > /dev/null; then   … fi 

To check if faraway isn't defined, prefix the condition with !:

if ! git config remote.faraway.url > /dev/null; then   … fi 
like image 35
tig Avatar answered Oct 22 '22 01:10

tig