无法推送到 Bitbucket 上的 Git 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16074832/
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
Cannot push to Git repository on Bitbucket
提问by Jason Dahl
I created a new repository and I'm running into a strange error. I've used Git before on Bitbucket but I just reformatted and now I can't seem to get Git to work. After doing a commit, I had to add my email and name to the globals, but then it committed just fine.
我创建了一个新的存储库,但遇到了一个奇怪的错误。我之前在 Bitbucket 上使用过 Git,但我刚刚重新格式化,现在我似乎无法让 Git 工作。提交后,我必须将我的电子邮件和姓名添加到全局变量中,但随后它提交得很好。
When I try to use the command
当我尝试使用命令时
git push origin master
it doesn't work. I get this message:
它不起作用。我收到这条消息:
$ git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I'm at a loss here. My friend whom I'm sharing this repository with, accessed it fine and pushed to it just fine, but I can't seem to get it to work.
我在这里不知所措。我与我共享此存储库的朋友可以很好地访问它并很好地推送到它,但我似乎无法让它工作。
回答by Graeme Wicksted
Writing this for those just getting started with Git and BitBucket on Windows & who are not as familiar with Bash (since this is both a common issue and a high ranking Google result when searching for the error message within the question).
为那些刚开始在 Windows 上使用 Git 和 BitBucket 并且不熟悉 Bash 的人编写这篇文章(因为在搜索问题中的错误消息时,这既是一个常见问题,又是一个高排名的谷歌结果)。
For those who don't mind HTTPS and who are looking for a quick fix, scroll to the bottom of this answer for instructions under FOR THE LAZY
对于那些不介意 HTTPS 并且正在寻找快速解决方案的人,请滚动到此答案的底部以获取有关“懒惰”下的说明
For those looking to solve the actual problem, follow the instructions below:
对于那些希望解决实际问题的人,请按照以下说明操作:
Fixing the SSH issue as fast as possible
尽快修复 SSH 问题
This is a set of instructions derived from the URL linked to by VonC. It was modified to be as resilient and succinct as possible.
这是从 VonC 链接的 URL 派生的一组指令。它被修改为尽可能有弹性和简洁。
Don't type the
$
or any lines that do not begin with$
(the$
means this is something you type into GitBash).Open GitBash
不要输入
$
或任何不以开头的行$
(这$
意味着这是您在 GitBash 中输入的内容)。打开 GitBash
Set your global info if you haven't already:
如果您还没有设置您的全局信息:
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
Check for OpenSSH:
检查 OpenSSH:
$ ssh -v localhost
OpenSSH_4.6p1, OpenSSL...
See something like that?
看到类似的东西了吗?
- Yes: Continue.
- No: Skip to the FOR THE LAZYsection or follow the linked article from VonC.
- 是:继续。
- 否:跳至FOR THE LAZY部分或遵循 VonC 的链接文章。
See if you have generated the keys already:
查看您是否已经生成了密钥:
$ ls -a ~/.ssh/id_*
If there are two files, you can skip the next step.
如果有两个文件,可以跳过下一步。
$ ssh-keygen
Leave everything as the defaults, enter a passphrase. You should now see results with this command:
将所有内容保留为默认值,输入密码。您现在应该使用此命令查看结果:
$ ls -a ~/.ssh/id_*
Check for an existing config file:
检查现有的配置文件:
$ ls -a ~/.ssh/config
If you get a result, check this file for erroneous information. If no file exists, do the following:
如果得到结果,请检查此文件中的错误信息。如果不存在文件,请执行以下操作:
$ echo "Host bitbucket.org" >> ~/.ssh/config
$ echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config
Confirm the contents:
确认内容:
$ cat ~/.ssh/config
Host bitbucket.org
IdentityFile ~/.ssh/id_rsa
- The single space before "IdentityFile" is required.
- “IdentityFile”之前的单个空格是必需的。
Check you are starting the SSH agent every time you run GitBash:
每次运行 GitBash 时检查是否启动了 SSH 代理:
$ cat ~/.bashrc
- If you see a function called
start_agent
, this step has already been completed. - If no file, continue.
- If there is a file that does not contain this function, you're in a sticky situation. It's probably safe to append to it (using the instructions below) but it may not be! If unsure, make a backup of your .bashrc before following the instructions below or skip ahead to FOR THE LAZYsection.
- 如果您看到一个名为 的函数
start_agent
,则这一步已经完成。 - 如果没有文件,继续。
- 如果有一个文件不包含这个函数,你就处于一个棘手的境地。附加到它可能是安全的(使用下面的说明),但可能不是!如果不确定,请在按照以下说明操作之前备份您的 .bashrc 或跳到FOR THE LAZY部分。
Enter the following into GitBash to create your .bashrc file:
在 GitBash 中输入以下内容以创建 .bashrc 文件:
$ echo "SSH_ENV=$HOME/.ssh/environment" >> ~/.bashrc
$ echo "" >> ~/.bashrc
$ echo "# start the ssh-agent" >> ~/.bashrc
$ echo "function start_agent {" >> ~/.bashrc
$ echo " echo \"Initializing new SSH agent...\"" >> ~/.bashrc
$ echo " # spawn ssh-agent" >> ~/.bashrc
$ echo " /usr/bin/ssh-agent | sed 's/^echo/#echo/' > \"${SSH_ENV}\"" >> ~/.bashrc
$ echo " echo succeeded" >> ~/.bashrc
$ echo " chmod 600 \"${SSH_ENV}\"" >> ~/.bashrc
$ echo " . \"${SSH_ENV}\" > /dev/null" >> ~/.bashrc
$ echo " /usr/bin/ssh-add" >> ~/.bashrc
$ echo "}" >> ~/.bashrc
$ echo "" >> ~/.bashrc
$ echo "if [ -f \"${SSH_ENV}\" ]; then" >> ~/.bashrc
$ echo " . \"${SSH_ENV}\" > /dev/null" >> ~/.bashrc
$ echo " ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {" >> ~/.bashrc
$ echo " start_agent;" >> ~/.bashrc
$ echo " }" >> ~/.bashrc
$ echo "else" >> ~/.bashrc
$ echo " start_agent;" >> ~/.bashrc
$ echo "fi" >> ~/.bashrc
Verify the file was created successfully (yours should only differ where "yourusername" appears):
验证文件是否已成功创建(您的文件应该只在“您的用户名”出现的地方有所不同):
$ cat ~/.bashrc
SSH_ENV=/c/Users/yourusername/.ssh/environment
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add
}
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
- Close GitBash and re-open it.
- You should be asked for your passphrase (for the SSH file you generated earlier).
- If no prompt, you either did not set a passphrase or GitBash isn't running the .bashrc script (which would be odd so consider reviewing the contents of it!). If you are running this on a Mac(OS X),
.bashrc
isn't executed by default -.bash_profile
is. To fix this, put this snippet in your.bash_profile
:[[ -s ~/.bashrc ]] && source ~/.bashrc
- 关闭 GitBash 并重新打开它。
- 应该要求您输入密码(对于您之前生成的 SSH 文件)。
- 如果没有提示,要么你没有设置密码,要么 GitBash 没有运行 .bashrc 脚本(这很奇怪,所以考虑检查它的内容!)。如果您在 Mac(OS X) 上运行它
.bashrc
,默认情况下不会执行 -.bash_profile
是。要解决此问题,请将此代码段放入您的.bash_profile
:[[ -s ~/.bashrc ]] && source ~/.bashrc
If you didn't enter a passphrase, you would have seen something like this when starting GitBash:
如果你没有输入密码,你会在启动 GitBash 时看到这样的内容:
Initializing new SSH agent...
succeeded
Identity added: /c/Users/yourusername/.ssh/id_rsa (/c/Users/yourusername/.ssh/id_rsa)
And the following should return results:
以下应返回结果:
$ ssh-add -l
However, if you get the following from ssh-add -l
:
但是,如果您从以下内容中获得ssh-add -l
:
Could not open a connection to your authentication agent.
It didn't spawn the SSH agent and your .bashrc is likely the cause.
它没有产生 SSH 代理,您的 .bashrc 可能是原因。
If, when starting GitBash, you see this:
如果在启动 GitBash 时,您看到:
Initializing new SSH agent...
sh.exe": : No such file or directory
That means you forgot to escape the $ with a \ when echoing to the file (ie. the variables were expanded). Re-create your .bashrc to resolve this.
这意味着您在回显到文件时忘记用 \ 转义 $(即变量已扩展)。重新创建您的 .bashrc 来解决这个问题。
Verify the agent is running and your keys have been added:
验证代理正在运行并且您的密钥已添加:
$ ssh-add -l
Should return something similar to this:
应该返回与此类似的内容:
2048 0f:37:21:af:1b:31:d5:cd:65:58:b2:68:4a:ba:a2:46 /Users/yourusername/.ssh/id_rsa (RSA)
Run the following command to get your public key:
运行以下命令以获取您的公钥:
$ cat ~/.ssh/id_rsa.pub
(it should return something starting with "ssh-rsa ......"
(它应该返回以“ssh-rsa ......”开头的内容
- Click the GitBash window icon
- Click Edit
- Click Mark
- Highlight the public key using your mouse (including the leading
ssh-rsa
bit and the trailing== [email protected]
bit) - Right-click the window (performs a copy)
- Paste your public key into Notepad.
- Delete all the newlines such that it is only a single line.
- Press
CTRL+A
thenCTRL+C
to copy the public key again to your clipboard.
- 单击 GitBash 窗口图标
- 单击编辑
- 单击标记
- 使用鼠标突出显示公钥(包括前导
ssh-rsa
位和尾随== [email protected]
位) - 右键单击窗口(执行复制)
- 将您的公钥粘贴到记事本中。
- 删除所有换行符,使其仅为一行。
- 按
CTRL+A
然后CTRL+C
再次将公钥复制到剪贴板。
Configure your private key with BitBucket by performing the following steps:
通过执行以下步骤,使用 BitBucket 配置您的私钥:
- Open your browser and navigate to the BitBucket.orgsite
- Login to BitBucket.org
- Click your avatar (top-right)
- Click Manage Account
- Click SSH Keys (under Security on the left-hand menu)
- Click Add Key
- Enter
Global Public Key
for the Label - Paste the public key you copied from Notepad
- 打开浏览器并导航到BitBucket.org站点
- 登录BitBucket.org
- 单击您的头像(右上角)
- 点击管理账户
- 单击 SSH 密钥(在左侧菜单的“安全”下)
- 单击添加密钥
- 输入
Global Public Key
标签 - 粘贴从记事本复制的公钥
A Global Public Key
entry should now be visible in your list of keys.
Global Public Key
现在应该可以在您的密钥列表中看到一个条目。
- Return to GitBash
- cd into the directory containing your project
- Change your origin to the SSH variation (it will not be if you ran the FOR THE LAZYsteps)
- 返回 GitBash
- cd 进入包含您的项目的目录
- 将您的来源更改为 SSH 变体(如果您运行FOR THE LAZY步骤则不会)
Check your remotes:
检查您的遥控器:
$ git remote -v
Switch to the SSH url:
切换到 SSH 网址:
$ git remote set-url origin [email protected]:youraccount/yourproject.git
Check things are in working order:
检查事情是否正常:
$ git remote show origin
You should see something like this:
您应该会看到如下内容:
Warning: Permanently added the RSA host key for IP address '...' to the list of known hosts.
* remote origin
Fetch URL: [email protected]:youruser/yourproject.git
Push URL: [email protected]:youruser/yourproject.git
HEAD branch: master
Remote branch:
master tracked
Local ref configured for 'git push':
master pushes to master (fast-forwardable)
DONE!
完毕!
You can opt to use HTTPS instead of SSH. It will require you to type your password during remote operations (it's cached temporarily after you type it once). Here is how you can configure HTTPS:
您可以选择使用 HTTPS 而不是 SSH。它将要求您在远程操作期间输入您的密码(您输入一次后它会被临时缓存)。以下是配置 HTTPS 的方法:
FOR THE LAZY
对于懒人
You should fix the SSH issue as described by VonC; however, if you're in a rush to commit and don't have the tools/time/knowledgeto generate a new public key right now, set your origin to the HTTPS alternative:
您应该按照 VonC 的描述修复 SSH 问题;但是,如果你在赶时间承诺和没有工具/时间/知识生成一个新的公钥现在,请将您的起源到HTTPS选择:
> https://[email protected]/accountname/reponame.git
Using a GUI tool such as TortoiseGitor command line tools.
使用 GUI 工具,例如TortoiseGit或命令行工具。
Here is the documentation of this alternative origin URL.
Command line to add an origin if one does not exist:
如果原点不存在,则添加原点的命令行:
git remote add origin https://[email protected]/accountname/reponame.git
Command line to change an existing origin:
更改现有原点的命令行:
git remote set-url origin https://[email protected]/accountname/reponame.git
NOTE: your account name is not your email.
注意:您的帐户名称不是您的电子邮件。
You may also want to set your global info:
您可能还想设置您的全局信息:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Then try your push again (no need to commit again)
然后再次尝试推送(无需再次提交)
git push origin master
回答by Matthias Braun
回答by VonC
Reformatted means you probably deleted your public and private ssh keys (in ~/.ssh).
重新格式化意味着您可能删除了您的公共和私人 ssh 密钥(在 ~/.ssh 中)。
You need to regenerate them and publish your public ssh key on your BitBucket profile, as documented in "Use the SSH protocol with Bitbucket", following "Set up SSH for Git with GitBash".
您需要重新生成它们并在您的 BitBucket 配置文件上发布您的公共 ssh 密钥,如“使用 Bitbucket 使用 SSH 协议”中所述,“使用 GitBash 为 Git 设置 SSH”。
Accounts->Manage Accounts->SSH Keys:
账户->管理账户->SSH 密钥:
Then:
然后:
Images from "Integrating Mercurial/BitBucket with JetBrains software"
回答by Ankit Arora
I solved this by removing the remote using command:
我通过使用命令删除遥控器解决了这个问题:
git remote remove origin
and then tried to add remote using https url instead of ssh
然后尝试使用 https url 而不是 ssh 添加远程
git remote add origin httpsUrl
It asks for github credentials. Enter credentials and then try pushing to git using:
它要求提供 github 凭据。输入凭据,然后尝试使用以下命令推送到 git:
git push origin master
回答by MatthewShin
Just need config file under ~/.ssh directory
ref : https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html
add bellow configuration in config file
只需要 ~/.ssh 目录
ref下的配置文件:https:
//confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html 在配置文件中添加波纹管配置
Host bitbucket.org
IdentityFile ~/.ssh/<privatekeyfile>
回答by Rafael
I had the same problem. My SSH keys were set correctly. I fixed this problem like this.
我有同样的问题。我的 SSH 密钥设置正确。我像这样解决了这个问题。
After creating new project in Bitbucket, use clone. Enter cloning command in terminal and it should clone empty project to your computer. After that you can copy your files to this directory and start committing and pushing to bitbucket.
在 Bitbucket 中创建新项目后,使用 clone。在终端中输入克隆命令,它应该将空项目克隆到您的计算机。之后,您可以将文件复制到此目录并开始提交并推送到 bitbucket。
回答by Paulo
Two small clarifications that might save someone the confusion I went through:
两个小的澄清可能会挽救某人我所经历的困惑:
1 - Connection URLs differ for HTTPS and SSH
1 - HTTPS 和 SSH 的连接 URL 不同
When connecting via https, you use
通过 https 连接时,您使用
https://[email protected]/owner-account/repo-name.git
however when connecting via SSH, the account name is always"git"
但是当通过 SSH 连接时,帐户名总是“git”
ssh://[email protected]/owner-account/repo-name.git
Attempting to connect to SSH with your account name in front will lead to the error the original poster received. This is how you can do the test connecting to git@, then mistakenly try with your username and see an error.
尝试使用前面的帐户名连接到 SSH 会导致原始发布者收到的错误。这是您如何进行连接到 git@ 的测试,然后错误地尝试使用您的用户名并看到错误。
2 - SSH Keys via team accounts will be deprecated in 2017
2 - 通过团队帐户的 SSH 密钥将在 2017 年被弃用
If you are setting up SSH keys on team accounts, they recommend switching them to personal accounts. A useful tip to avoid e
如果您在团队帐户上设置 SSH 密钥,他们建议将它们切换到个人帐户。避免e的有用提示
回答by Sarah
If you are using SourceTree (I'm using 2.4.1), I found a simpler way to generate an SSH key and add it to my Bitbucket settings. This solved the problem for me.
如果您使用的是 SourceTree(我使用的是 2.4.1),我找到了一种更简单的方法来生成 SSH 密钥并将其添加到我的 Bitbucket 设置中。这为我解决了这个问题。
- In SourceTree, go to Preferences.
- Go to the Accounts tab and select your account.
- There should be an option to generate and copy an SSH key to clipboard.
- Once you have copied that, go to Bitbucket in your browser. Go to [avatar] -> Bitbucket settings.
- Go to SSH keys.
- Click Add key
- Paste in the key you copied.
- 在 SourceTree 中,转到首选项。
- 转到“帐户”选项卡并选择您的帐户。
- 应该有一个选项可以生成 SSH 密钥并将其复制到剪贴板。
- 复制后,在浏览器中转到 Bitbucket。转到 [头像] -> Bitbucket 设置。
- 转到 SSH 密钥。
- 单击添加密钥
- 粘贴您复制的密钥。
I received a confirmation email from Bitbucket that an SSH key had been added to my account.
我收到了一封来自 Bitbucket 的确认电子邮件,表明 SSH 密钥已添加到我的帐户中。
For reference, on macOS, using Terminal, you can use the following command to see the keys generated for your device. This is where the key you generated is stored.
作为参考,在 macOS 上,使用终端,您可以使用以下命令查看为您的设备生成的密钥。这是您生成的密钥的存储位置。
ls -la ~/.ssh
As others have stated, this documentation helped me: Use the SSH protocol with Bitbucket Cloud
正如其他人所说,此文档对我有帮助:将 SSH 协议与 Bitbucket Cloud 一起使用
回答by Hector
Get the ssh done as in the Atlassian tutorial and make sure the private key is being pasted in the profile, not in the repository :)
像 Atlassian 教程一样完成 ssh,并确保将私钥粘贴到配置文件中,而不是存储库中:)
回答by warden
This is probably caused by having multiple SSH keys in SSH agent (and/or BitBucket). Check Atlassian documentation for the workaround for this
这可能是由于 SSH 代理(和/或 BitBucket)中有多个 SSH 密钥造成的。检查Atlassian 文档以了解解决方法