Is there a way to import my local git repos to SourceTree and push them to my Bitbucket account, having new repos identical to my local repos be created on my account? Or do I have to make a repo online first and push to that? Github has a way to publish your local repos directly from it's client, and it creates it automatically when you publish, but it has limited private repos. I'm just doing homework so there's no reason for it to be public, hence why I'm trying to use bitbucket.
问题:
回答1:
It does seem possible to "publish" a local repository to BitBucket. Using the instructions from Atlassian, simply create a new BitBucket repository, copy the repository url to the clipboard (in your BitBucket repository, go to Actions>Clone, and it'll show you your repo url in the form git clone <repository url>
), and then add that repository as a new remote to your local repository:
Using CLI
cd /path/to/my/repo
git remote add origin ssh://git@bitbucket.org/<username>/<reponame>.git
git push -u origin --all
Using SourceTree
- Repository>Add Remote...
- Paste the BitBucket repository url (
git@bitbucket.org/<username>/<reponame>.git
)
UPDATE: Creating & Registering SSH Keys
BitBucket is great for private repos, but you'll need to set up an ssh key to authorize your computer to work with your BitBucket account. Luckily Sourcetree makes it relatively simple:
Creating a Key In SourceTree:
- In
Tools
>Options
, make sureSSH Client:
is set toPuTTY/Plink
under theGeneral
tab - Select
Tools
>Create or Import SSH Keys
- In the popup window, click
Generate
and move your mouse around to give randomness to the key generator You should get something like whats shown in the screenshot below. Copy the public key (highlighted in blue) to your clipboard
- Click
Save private Key
andSave public key
to save your keys to wherever you choose (e.g. to<Home Dir>/putty/ssk-key.ppk
and<Home Dir>/putty/ssh-key.pub
respectively) before moving on to the next section
Registering The Key In BitBucket
- Log in to your BitBucket account, and on the top right, click your profile picture and click
Settings
- Go to the
SSH Keys
tab on the left sidebar - Click
Add SSH Key
, give it a name, and paste the public key you copied in step 4 of the previous section
That's it! You should now be able to push/pull to your BitBucket private repos. Your keys aren't just for Git either, many services use ssh keys to identify users, and the best part is you only need one. If you ever lose your keys (e.g. when changing computers), just follow the steps to create and register a new one.
Sidenote: Creating SSH Keys using CLI
Just follow this tutorial
回答2:
Actually there is a more simple solution (only on Mac version). Just four steps:
- Right click on the repository and select "Publish to remote..."
- Next window will ask you were to publish (github, bitbucket, etc), and then you are done.
- Link the remote repository
- Push
回答3:
As this video illustrates, creating a repo online first is the usual way to go.
The SourceTree Release Notes do mention for SourceTree 1.5+:
Support creating new repositories under team / organisation accounts in Bitbucket.
So while there is no "publishing" feature, you could create your online repo from SourceTree.
The blog post "SourceTree for Windows 1.2 is here" (Sept 2013) also mention:
Now you can configure your Bitbucket, Stash and GitHub accounts in SourceTree and instantly see all your repositories on those services. Easily clone them, open the project on the web, and even create new repositories on the remote service without ever leaving SourceTree.
You’ll find it in the menu under View > Show Hosted Repositories, or using the new button at the bottom right of the bookmarks panel.
回答4:
I used this and it worked out well for me. If your directory is
"repo" and your project is "hello" copy the project there
cd /path/to/my/repo
Initialize your directory
git init
Stage the project
git add hello
commit the project
git commit
Add configurations using the email and username you are using in Bitbucket
git config --global user.email
git config --global user.name
Add comment to the project
git commit -m 'comment'
push the project now
git push origin master
Check out of the master
git checkout master
回答5:
Bitbucket supports a REST API you can use to programmatically create Bitbucket repositories.
Documentation and cURL sample available here: https://confluence.atlassian.com/bitbucket/repository-resource-423626331.html#repositoryResource-POSTanewrepository
$ curl -X POST -v -u username:password -H "Content-Type: application/json" \
https://api.bitbucket.org/2.0/repositories/teamsinspace/new-repository4 \
-d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks" }'
Under Windows, curl is available from the Git Bash shell.
Using this method you could easily create a script to import many repos from a local git server to Bitbucket.
回答6:
Setup Bitbucket Repository (Command Line with Mac)
Create New APPLICATION from starting with local reposity :
- Terminal -> cd ~/Documents (Paste your APPLICATION base directory path)
- Terminal -> mkdir (create directory with )
- Terminal -> cd (change directory with directory)
- BitBucket A/C -> create repository on bitBucket account
- Xcode -> create new xcode project with same name
- Terminal -> git init (initilize empty repo)
- Terminal -> git remote add origin (Ex. https://app@bitbucket.org/app/app.git)
- Terminal -> git add .
- Terminal -> git status
- Terminal -> git commit -m "IntialCommet"
- Terminal -> git push origin master
Create APPLICATION clone repository :
- Terminal -> mkdir (create directory with )
- Terminal -> cd (change directory with directory)
- Terminal -> git clone (Ex. https://app@bitbucket.org/app/app.git)
- Terminal -> cd
- Terminal -> git status (Show edit/updated file status)
- Terminal -> git pull origin master
- Terminal -> git add .
- Terminal -> git push origin master
回答7:
GIT serves it's purpose well for version control and team projects if commits and branches are maintained properly.
Step 1: Clone your local repo using cli as mentioned by above answers
$ cd [path_to_repo]
$ git remote add origin ssh://git@bitbucket.org//.git
$ git push -u origin --all
Step 2: You can follow any of the above steps to push/pull your works. Easy way is to use git gui. It provides Graphical Interface so that it's easy to stage(add)/unstage, commit/uncommit and push/pull. Beginners can easily understand the git process.
$ git gui
(OR)
Step 2: As mentioned above. Cli codes will do the work.
$ git status
$ git add [file_name]
$ git commit _m "[Comit message"]"
$ git push origin master/branch_name
回答8:
As an update to Joe's answer, in that you can script the creation of the repository using the API, only the API call I needed was different. This may be because we are using bitbucket server, and not bitbucket cloud.
To create a new repo within a project on our server, I used:
curl -X POST -v -u USER:PASSWORD -H "Content-Type: application/json" \
http://SERVER/rest/api/1.0/projects/PROJECTNAME/repos/ \
-d '{"scmid":"git", "name":"REPONAME"}'
where USER
, PASSWORD
, SERVER
, PROJECTNAME
and REPONAME
were of course the desired/required values.
The call is documented in the API reference.
/rest/api/1.0/projects/{projectKey}/repos
Create a new repository. Requires an existing project in which this repository will be created. The only parameters which will be used are name and scmId.
The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.