fatal: remote <Repository> already exists - bash

As part of the development of a CI & CD flow for the company I work at, I am building a command line program (Bash script on OSX) that
creates a new local Git repo
adds some default branches to this repo
Then adds a new repo to Bitbucket using the next code:
gitUserName = Joris <-- provided by the user, this is an example
projectName = TestProject <-- provided by the user, this is an example
git remote add $projectName "https://bitbucket.org/$gitUserName/$projectName.git"
After running this command, I don't see the repository on my Bitbucket account on the website. When I try to re-run this command, it says the repository already exists.
Also, when I run git push $projectName master it says fatal: repository 'https://bitbucket.org/Joris/TestProject.git/' not found
This behavior seems inconsistent, and I have followed the Atlassian guide to set this up so I don't really understand why it doesn't add the repository as expected. I do realize that I can also just go on the BitBucket website and add the repository manually, but the purpose of my program is that it generates a fully set-up repository for a user based on as little commands as possible.

The git remote add documentation says that the command adds a remote to the local repo. This terminology is, IMO, a bit off; it would better to say it adds a remote configuration to the local repository (i.e. configures the repo to access a remote). This does not actually create the remote repo; that must be done separately.
In the case of bitbucket, the "normal" thing to do is to go to the website and create the repo through their UI. Because you're trying to automate things, you don't want to do that; so in that case, you would need to use the BitBucket REST API, which is documented here: https://developer.atlassian.com/server/bitbucket/reference/rest-api/
The "Core API" section talks about repositories and permissions, so you should be able to script out requests to (if necessary) check if the repo exists and set it up if it doesn't. You'll just need a way for your script to send HTTP requests and receive the responses.

In your machine:
Create repo:
git init
Add branches:
git checkout -b branchX
git checkout -b branchY
git checkout -b branchZ
In Bitbucket website:
Create new repository named TestProject, allow write permissions to user Joris in settings and save. Finally copy the url of the repository, this must be something like bitbucket.mydomain:port/nameofproject/testproject.git (Notice this is all in lowcase)
In your machine:
git remote add origin theURL
git push origin *:*
git push origin --tags
The last is the command to push all your local repo, this will overwrite the history and tags in your remote repo, but since is a new repo it doesn't matter.

Related

Backing up full remote git repository with history to another remote server

Assume that I have to GIT servers behind a fence, say at git.mycompany.com (with gitea UI) and at git.myclient.com (with github-like UI), both heavily security with VPN, multi factor-authentication etc. I want to deliver my full repository myProduct to git.myclient.com/alice/myProduct since Alice is my point of contact.
Can I do that directly without the detour over a local repository on my computer?
Since I am working remote, and the uplink of mycompany.com is much faster than my own...
My current lengthy and slow approach
In detail, the detour over my computer looks as follows:
Using the (github-like) user-interface, create an empty repository at git.myclient.com called myProduct.
Make sure that my local repository is up to date with git pull.
Check my current remote origin with git config --get remote.origin.url, see e.g. an answer to How can I determine the URL that a local Git repository was originally cloned from?
In my case, the result simply is https://git.mycompany.com/b--rian/myProduct.git
Change this configuration to the destination repository with git remote set-url origin git#git.myclient.com:alice/myProduct.git.
Generate a key-value pair for ssh using ssh-keygen -o, see https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key
Ask Alice to navigate to Settings > GPG and SSH keys (usually at
https://git.myclient.com/settings/keys) and ask her to add my New SSH key from the previous step.
Make sure that the SSH agent is running on my windows box, if not start it with eval $(ssh-agent -s) inside the GIT Bash, see https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
Now, finally, I can push most things to the client using git push --all origin. This is the slow step which I would like to speed up.
The tags have to be pushed separately, I heard: git push origin --tags, see How do you push a tag to a remote repository using Git?
Wind back everything by setting the remote.origin.url to back what it was in step 3, in my case it is a git remote set-url origin https://git.mycompany.com/b--rian/myProduct.git.
Is there an easier way?
Maybe you could use Git mirroring on git.myclient.com.
It's up to clients VCS (Git). Most implementations have mirroring options.
For example, if the client is using Gitlab:
https://docs.gitlab.com/ee/user/project/repository/repository_mirroring.html
I think it would be much simpler to add a second remote... and then push into that remote:
git remote add second url-to-new-repo
git push second master develop # push master and develop to second
And so on.

How to push a new code to an existing git repository in github

I need to push my modified new java code to my old git repository in github but I do not have old code in my pc. How to do that?
I had push a code before my github account before. Now I don't have that old code in my pc. How do I pull the project into my pc and after making changes, push again to the same repository?
I do not have much experience in github, so please help me to improve skills on github.
Check your remote first to see where it is pointing to by
$ git remote -v
origin ssh://git#<old-git-url>/<project>.git (fetch)
origin ssh://git#g<old-git-url>/<project>.git (push)
Change the pointing to GitHub
$ git remote set-url origin ssh://git#<github-url>/<project>.git
Now your repo is pointing to Github
Now you can make your changes and then add them and do a commit and finally push to remote branch; say you are on master.
git add <file>
git commit -m <commit message>
git push origin master
I had to do just that and achieved it like this.
Setup. I assume you have a new remote repository with files that may not cause a conflict with the directory you currently have locally.
Git clone from the git repository you need to push to. Just make sure you create a new directory for the cloned code.
Copy the contents of the cloned repository into the local directory that has your current code. Make sure to copy the .git (hidden) file.
cd into your local directory and run git remote -v. You should see the remote repository git address.
git add -A to add whatever change you require and commit it.
Finally git push
You need to make sure that your local repository (the one that is on your computer) is connected to the remote repository (the one that is on the GitHub servers).
After this, you need to add the modified file to the staging area. Say, you have a file test.txt that you have modified, you would add it to the staging area by typing
git add test.txt
After that you would need to commit those changes. You can do that by
git commit -m "commit message"
And that's it, you have now saved those changes and recorded them in the version control. But the changes that you made have only been recorded in your local repository and you would need to push these changes to the remote repository (the GitHub servers). You can do this by
git push origin master
It would take a few seconds (depending on your internet speed and the project file size) to push these changes to the remote servers. Once it's done, you can open that repository on GitHub and see the changes for yourself.
Just to amend this a bit. I believe "master" is now "main" in some instances or platforms. If this answer isn't working for you, try swapping that out. It worked for me.

Can't fetch from same repository in Bitbucket Pipelines

I'm trying to do the following with Bitbucket Pipelines when i push to my test brand:
- git fetch
- git checkout master
- git pull origin test
- git push origin master
But i get the following message on git fetch: Permission denied (publickey). I was following this tutorial https://confluence.atlassian.com/bitbucket/use-ssh-keys-in-bitbucket-pipelines-847452940.html so i already added an ssh key, but cannot understand what are the next steps that i need for the execution to have permissions to connect to the repository.
If “from same repository” really means that you want to access the repository in which the pipeline runs, the answer is: you don’t need that. When the pipeline runs, it starts the Docker image you defined in your YAML configuration and automatically checks out the commit you pushed. This means that at the moment when when your command (git fetch) is executed, the sources are already waiting for you in path /project.
BitBucket Pipelines automatically checks out the repository upon running. However, if you want to make changes to the repository (e.g. git tag or git push) you will need to add the SSH keys according to the post you have already found (https://confluence.atlassian.com/bitbucket/use-ssh-keys-in-bitbucket-pipelines-847452940.html). That works for our environment.
Can you post the full bitbucket-pipelines.yml file?

Creating a github repository from command line

I am creating a new app and I want to be able to create a repository from the command line to be able to add commits without having to go to github and create a repo. I am totally noobian. is it possible? and if so how? Thank you!
for github you could try hub - is a command line tool that wraps git in order to extend it with extra features and commands that make working with GitHub easier.
Examples from doc:
$ git create
[ repo created on GitHub ]
> git remote add origin git#github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ git create -d 'It shall be mine, all mine!'
$ git create recipes
[ repo created on GitHub ]
> git remote add origin git#github.com:YOUR_USER/recipes.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git#github.com:sinatra/recipes.git
If you want to use git, then it is fairly straightforward, but you won't go far without going through some tutorial. I highly recommend Git Book by Scott Chacon. At least go through chapter 2 and perhaps chapter 3. To answer your questions though: Git is a distributed versioning system so you definitely do not need a repo in GitHub - you can create a repo on your own harddrive and then push it to any other repos (i.e. you could create a GitHub repository later and publish your repository there - it will be an exact clone!).
To create a repo you invoke a command: git init or git init repoName - the former creates the repo in current folder, the latter creates a new one named "repoName".
When you are ready to create a first commit invoke git add . to add all files to index (think of it as an area where you prepare what will go into the next commit) and the git commit -m "Commit message". nstead of adding all files you could also choose files to commit individualy by git add path/to/file
You will need at least some basics with git covered though before you can start using it comfortably.

How to push in heroku?

I clone my repo down and change something and commit,
but when I want to push like the tutorial:
git push heroku master
it tell me wrong:
fatal: 'heroku' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I'm sure I have log in,
so how can I push my repo correctly and deploy it?
From Git Reference (http://gitref.org/remotes/#remote):
"So that you don't have to use the full URL of a remote repository every time you want to synchronize with it, Git stores an alias or nickname for each remote repository URL you are interested in. You use the git remote command to manage this list of remote repos that you care about."
You can check if remote named "heroku" exists for your git repo using:
git remote -v
If it doesn't exist, you need to add it before you can push updates as follows:
git remote add heroku git#heroku.com:appname.git
where appname should be the name of your app.

Resources