git 如何使用 SourceTree 将本地存储库推送到 Bitbucket 而无需先在 bitbucket 上创建存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24294034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I push a local repo to Bitbucket using SourceTree without creating a repo on bitbucket first?
提问by cma0014
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.
有没有办法将我的本地 git 存储库导入 SourceTree 并将它们推送到我的 Bitbucket 帐户,在我的帐户上创建与我的本地存储库相同的新存储库?还是我必须先在线制作一个回购并推动它?Github 有一种方法可以直接从它的客户端发布您的本地存储库,它会在您发布时自动创建它,但它的私有存储库有限。我只是在做作业,所以没有理由公开它,因此我尝试使用 bitbucket。
回答by woojoo666
(updated on 3-29-2019 to use the https instead of ssh, so you don't need to use ssh keys)
(于 3-29-2019 更新为使用 https 而不是 ssh,因此您无需使用 ssh 密钥)
It seems like for BitBucket, you do have to create a repo online first. Using the instructions from Atlassian, simply create a new BitBucket repository, copy the repository url to the clipboard, and then add that repository as a new remote to your local repository (full steps below):
似乎对于 BitBucket,您必须首先在线创建一个 repo。使用来自Atlassian的说明,只需创建一个新的 BitBucket 存储库,将存储库 url 复制到剪贴板,然后将该存储库作为新的远程存储库添加到您的本地存储库(完整步骤如下):
Get Repo URL
获取回购网址
- in your BitBucket repo, choose "Clone" on the top-right
- choose "HTTPS" instead of "SSH" in the top-right of the dialog
- it should show your repo url in the form
git clone <repository url>
- 在您的 BitBucket 存储库中,选择右上角的“克隆”
- 在对话框的右上角选择“HTTPS”而不是“SSH”
- 它应该在表单中显示您的 repo url
git clone <repository url>
Add Remote Using CLI
使用 CLI 添加远程
cd /path/to/my/repo
git remote add origin https://bitbucket.org/<username>/<reponame>.git
git push -u origin --all
cd /path/to/my/repo
git remote add origin https://bitbucket.org/<username>/<reponame>.git
git push -u origin --all
Add Remote Using SourceTree
使用 SourceTree 添加远程
- Repository>Add Remote...
- Paste the BitBucket repository url (
https://bitbucket.org/<username>/<reponame>.git
)
- 存储库>添加远程...
- 粘贴 BitBucket 存储库 URL (
https://bitbucket.org/<username>/<reponame>.git
)
Old Method: Creating & Registering SSH Keys
旧方法:创建和注册 SSH 密钥
(this method is if you use the ssh url instead of the https url, which looks like ssh://[email protected]/<username>/<reponame>.git
. I recommend just using https)
(这种方法是如果你使用 ssh url 而不是 https url,看起来像ssh://[email protected]/<username>/<reponame>.git
。我建议只使用 https)
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:
BitBucket 非常适合私人存储库,但您需要设置一个 ssh 密钥来授权您的计算机使用您的 BitBucket 帐户。幸运的是 Sourcetree 让它变得相对简单:
Creating a Key In SourceTree:
在 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
- 在
Tools
> 中Options
,确保SSH Client:
设置为PuTTY/Plink
在General
选项卡下 - 选择
Tools
>Create or Import SSH Keys
- 在弹出窗口中,单击
Generate
并移动鼠标为密钥生成器提供随机性 你应该得到类似于下面屏幕截图中显示的内容。将公钥(以蓝色突出显示)复制到剪贴板
- 在继续下一部分之前,单击
Save private Key
和Save public key
将您的密钥保存到您选择的任何位置(例如分别为<Home Dir>/putty/ssk-key.ppk
和<Home Dir>/putty/ssh-key.pub
)
Registering The Key In BitBucket
在 BitBucket 中注册密钥
- Log in to your BitBucketaccount, 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
- 登录您的BitBucket帐户,然后在右上角,单击您的个人资料图片,然后单击
Settings
- 转到
SSH Keys
左侧边栏的选项卡 - 单击
Add SSH Key
,为其命名,然后粘贴您在上一节第 4 步中复制的公钥
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.
就是这样!您现在应该能够推/拉到您的 BitBucket 私人存储库。您的密钥也不仅仅用于 Git,许多服务使用 ssh 密钥来识别用户,最好的部分是您只需要一个。如果您丢失了您的密钥(例如在更换计算机时),只需按照步骤创建并注册一个新密钥即可。
Sidenote: Creating SSH Keys using CLI
旁注:使用 CLI 创建 SSH 密钥
回答by David
Actually there is a more simple solution (only on Mac version). Just four steps:
实际上有一个更简单的解决方案(仅适用于 Mac 版本)。只需四步:
- 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
- 下一个窗口会询问您是否要发布(github、bitbucket 等),然后您就完成了。
- 链接远程仓库
- 推
回答by VonC
As this video illustrates, creating a repo online first is the usual way to go.
正如本视频所示,首先在线创建一个回购协议是通常的做法。
The SourceTree Release Notesdo mention for SourceTree 1.5+:
该SourceTree发行说明对SourceTree 1.5+做提及:
Support creating new repositories under team / organisation accounts in Bitbucket.
支持在 Bitbucket 中的团队/组织帐户下创建新存储库。
So while there is no "publishing" feature, you could create your online repo from SourceTree.
因此,虽然没有“发布”功能,但您可以从 SourceTree 创建您的在线存储库。
The blog post "SourceTree for Windows 1.2 is here" (Sept 2013) also mention:
博客文章“适用于 Windows 1.2 的 SourceTree 在这里”(2013 年 9 月)也提到:
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.
现在,您可以在 SourceTree 中配置您的 Bitbucket、Stash 和 GitHub 帐户,并立即查看您在这些服务上的所有存储库。轻松克隆它们,在 Web 上打开项目,甚至无需离开 SourceTree 在远程服务上创建新存储库。
您可以在“查看”>“显示托管存储库”下的菜单中找到它,或者使用书签面板右下角的新按钮。
回答by bademba
I used this and it worked out well for me. If your directory is
我用了这个,对我来说效果很好。如果您的目录是
"repo" and your project is "hello" copy the project there
“repo”,您的项目是“hello”,将项目复制到那里
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
使用您在 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
回答by Joe Savage
Bitbucket supports a REST API you can use to programmatically create Bitbucket repositories.
Bitbucket 支持可用于以编程方式创建 Bitbucket 存储库的 REST API。
Documentation and cURL sample available here: https://confluence.atlassian.com/bitbucket/repository-resource-423626331.html#repositoryResource-POSTanewrepository
此处提供的文档和 cURL 示例: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.
在 Windows 下,curl 可从 Git Bash shell 获得。
Using this method you could easily create a script to import many repos from a local git server to Bitbucket.
使用这种方法,您可以轻松创建一个脚本,将许多存储库从本地 git 服务器导入到 Bitbucket。
回答by Shrikant Tanwade
Setup Bitbucket Repository (Command Line with Mac)
设置 Bitbucket 存储库(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://[email protected]/app/app.git)
- Terminal -> git add .
- Terminal -> git status
- Terminal -> git commit -m "IntialCommet"
- Terminal -> git push origin master
- 终端 -> cd ~/Documents(粘贴您的 APPLICATION 基目录路径)
- 终端 -> mkdir(用 来创建目录)
- 终端 -> cd(用目录更改目录)
- BitBucket A/C -> 在 bitBucket 帐户上创建存储库
- Xcode -> 创建同名的新 xcode 项目
- 终端 -> git init(初始化空仓库)
- 终端 -> git remote add origin(例如https://[email protected]/app/app.git)
- 终端 -> git add 。
- 终端 -> git 状态
- 终端 -> git commit -m "IntialCommet"
- 终端 -> git push origin master
Create APPLICATION clone repository :
创建 APPLICATION 克隆存储库:
- Terminal -> mkdir (create directory with )
- Terminal -> cd (change directory with directory)
- Terminal -> git clone (Ex. https://[email protected]/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
- 终端 -> mkdir(用 来创建目录)
- 终端 -> cd(用目录更改目录)
- 终端 -> git clone(例如https://[email protected]/app/app.git)
- 终端 -> cd
- 终端 -> git status(显示编辑/更新文件状态)
- 终端 -> git pull origin master
- 终端 -> git add 。
- 终端 -> git push origin master
回答by David Ruiz
Another Solution For Windows Users:
Windows 用户的另一种解决方案:
This uses Github as a bridge to get to Bitbucket, caused to the lack of publishing directly from the windows Sourcetree app.
这使用 Github 作为到达 Bitbucket 的桥梁,导致缺少直接从 windows Sourcetree 应用程序发布。
- Load your local repo into the Github desktop app.
- Publish the repo as a private (for privacy - if desired) repo from the Github desktop app into your Github account.
- Open your personal / team account in Bitbucket's website
- Create a new Bitbucket repo by importing from Github.
- Delete the repo in Github.
- 将您的本地存储库加载到 Github 桌面应用程序中。
- 从 Github 桌面应用程序将存储库作为私有(为了隐私 - 如果需要)存储库发布到您的 Github 帐户。
- 在 Bitbucket 的网站上打开您的个人/团队帐户
- 通过从 Github 导入来创建一个新的 Bitbucket 存储库。
- 删除 Github 中的 repo。
Once this is done, everything will be loaded into Bitbucket. Your local remotes will probably need to be configured to point to Bitbucket now.
完成此操作后,所有内容都将加载到 Bitbucket 中。您的本地遥控器现在可能需要配置为指向 Bitbucket。
回答by Adhémar Patamob
(Linux/WSL at least) From the browser at bitbucket.org, create an empty repo with the same name as your local repo, follow the instructions proposed by bitbucket for importing a local repo (two commands to type).
(至少是 Linux/WSL)在 bitbucket.org 的浏览器中,创建一个与本地存储库同名的空存储库,按照 bitbucket 建议的说明导入本地存储库(输入两个命令)。
回答by Baldrickk
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.
作为Joe's answer的更新,因为您可以使用 API 编写存储库的创建脚本,只有我需要的 API 调用不同。这可能是因为我们使用的是 bitbucket 服务器,而不是 bitbucket 云。
To create a new repo within a project on our server, I used:
为了在我们服务器上的项目中创建一个新的 repo,我使用了:
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.
其中USER
, PASSWORD
, SERVER
,PROJECTNAME
和REPONAME
当然是期望/要求的值。
The call is documented in the API reference.
该调用记录在 API 参考中。
/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.
/rest/api/1.0/projects/{projectKey}/repos
创建一个新的存储库。需要将在其中创建此存储库的现有项目。将使用的唯一参数是名称和 scmId。
经过身份验证的用户必须具有上下文项目的 PROJECT_ADMIN 权限才能调用此资源。
回答by Raj sree
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
如果正确维护提交和分支,GIT 可以很好地用于版本控制和团队项目。
步骤 1:使用上述答案中提到的 cli 克隆您的本地存储库
$ cd [path_to_repo]
$ git remote add origin ssh://[email protected]//.git
$ git push -u origin --all
$ cd [path_to_repo]
$ git remote add origin ssh://[email protected]//.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.
第2步:您可以按照上述任何一个步骤来推/拉您的作品。简单的方法是使用 git gui。它提供了图形界面,以便于暂存(添加)/取消暂存、提交/取消提交和推/拉。初学者可以轻松理解git流程。
$ git gui
$ git gui
(OR)
Step 2: As mentioned above. Cli codes will do the work.
(OR)
第 2 步:如上所述。Cli 代码将完成这项工作。
$ git status
$ git add [file_name]
$ git commit _m "[Comit message"]"
$ git push origin master/branch_name
$ git status
$ git add [file_name]
$ git commit _m "[Comit message"]"
$ git push origin master/branch_name