本文介绍了无法推送到Bitbucket上的Git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的存储库,并且遇到了一个奇怪的错误。我之前在Bitbucket上使用过Git,但我只是重新格式化了,现在我似乎无法让Git工作。在做完提交之后,我不得不将我的电子邮件和名称添加到全局变量中,但是它确实没问题。



当我尝试使用命令时:

  git push origin master 

不起作用。我收到以下消息:

pre $ code $ git push origin master
Permission denied(publickey)。
致命:无法从远程存储库读取。

请确保您拥有正确的访问权限
并存在存储库。

我在这里不知所措。我与朋友共享这个存储库的朋友,访问它并且推送到它就好,但我似乎无法让它工作。

方案

为那些刚刚开始使用Windows和Git和BitBucket的人撰写这篇文章。谁不熟悉Bash(因为在搜索问题中的错误消息时,这既是常见问题,也是高搜索结果)。



对于那些谁不介意HTTPS和正在寻找快速修复的人员,请滚动到此答案的底部以获取懒惰

中的说明

那些想要解决实际问题的人,请按照以下说明操作:

尽快解决SSH问题



这是从VonC链接到的URL派生的一组指令。它被修改为尽可能有弹性和简洁。




  • 不要输入 $ 或任何不以 $ 开头的行( $ 表示这是您键入的内容进入GitBash)。


  • 打开GitBash




设置你的全球信息,如果你还没有:

  $ git config --global user.nameYour Name
$ git config --global user.emailyou@example.com

检查OpenSSH:

  $ ssh -v本地主机
OpenSSH_4.6p1,OpenSSL ...

看到类似的东西?


  • 是:继续。

  • 否:跳至懒惰部分或按照来自VonC的链接文章



查看您是否已经生成密钥:

  $ ls -a〜/ .ssh / id_ * 

如果有两个d您可以跳过下一步。

  $ ssh-keygen 

pre>

保留所有默认设置,输入密码。您现在应该看到使用此命令的结果:

  $ ls -a〜/ .ssh / id_ * 

code>

检查现有配置文件:

  $ ls -a〜/ .ssh / config 

如果得到结果,这个文件的错误信息。如果没有文件存在,请执行以下操作:

  $ echoHost bitbucket.org>> 〜/ .ssh / config 
$ echoIdentityFile〜/ .ssh / id_rsa>> 〜/ .ssh / config

确认内容:

  $ cat〜/ .ssh / config 

主机bitbucket.org
IdentityFile〜/ .ssh / id_rsa




  • 需要IdentityFile之前的单个空格。
    ul>

    检查每次运行GitBash时启动SSH代理:

      $ cat〜/ .bashrc 




    • 如果您看到一个名为 start_agent ,此步骤已完成。 如果没有文件,请继续。
    • 是一个不包含此功能的文件,您处于粘滞状态。追加到它可能是安全的(使用下面的说明),但它可能不是!如果不确定,请在按照以下说明进行备份之前备份.bashrc,或者跳过 FOR THE LAZY 部分。


    在GitBash中输入以下内容以创建.bashrc文件:

      $ echoSSH_ENV = $ HOME / .ssh / environment>> 〜/ .bashrc 
    $ echo>> 〜/ .bashrc
    $ echo#启动ssh-agent>> 〜/ .bashrc
    $ echofunction start_agent {>> 〜/ .bashrc
    $ echoecho \正在初始化新的SSH代理程序... \>> 〜/ .bashrc
    $ echo#spawn ssh-agent>> 〜/ .bashrc
    $ echo/ usr / bin / ssh-agent | sed's / ^ echo /#echo /'> \\ $ {SSH_ENV} \>> 〜/ .bashrc
    $ echoecho succeeded>> 〜/ .bashrc
    $ echochmod 600 \\ $ {SSH_ENV} \>> 〜/ .bashrc
    $ echo。\\ $ {SSH_ENV} \> / dev / null>> 〜/ .bashrc
    $ echo/ usr / bin / ssh-add>> 〜/ .bashrc
    $ echo}>> 〜/ .bashrc
    $ echo>> 〜/ .bashrc
    $ echoif [-f \\ $ {SSH_ENV} \]; then>> 〜/ .bashrc
    $ echo。\\ $ {SSH_ENV} \> / dev / null>> 〜/ .bashrc
    $ echops -ef | grep \ $ {SSH_AGENT_PID} | grep ssh-agent $> / dev / null || {>> 〜/ .bashrc
    $ echostart_agent; >> 〜/ .bashrc
    $ echo}>> 〜/ .bashrc
    $ echoelse>> 〜/ .bashrc
    $ echostart_agent; >> 〜/ .bashrc
    $ echofi>> 〜/ .bashrc

    验证文件是否已成功创建(您的应该只在出现yourusername时才有区别) :

      $ cat〜/ .bashrc 
    SSH_ENV = / c / Users / yourusername / .ssh / environment

    #启动ssh-agent
    函数start_agent {
    echo正在初始化新的SSH代理...
    #spawn ssh-agent
    / usr / bin / ssh-agent | sed's / ^ echo /#echo /'> $ {SSH_ENV}
    echo成功
    chmod 600$ {SSH_ENV}
    。 $ {SSH_ENV}> / dev / null
    / usr / bin / ssh-add
    }

    if [-f$ {SSH_ENV}];然后
    。 $ {SSH_ENV}> / dev / null
    ps -ef | grep $ {SSH_AGENT_PID} | grep ssh-agent $> / dev / null || {
    start_agent;
    }
    else
    start_agent;
    fi




    • 关闭GitBash并重新打开它。 / li>
    • 您应该询问您的密码(对于您先前生成的SSH文件)。 如果没有提示,您没有设置密码或GitBash没有运行.bashrc脚本(这很奇怪,因此请考虑查看它的内容!)。如果你是在Mac(OS X)上运行它,默认情况下不会执行 .bashrc - .bash_profile 是。为了解决这个问题,把这个片段放到 .bash_profile [[-s〜/ .bashrc]]&&源〜/ .bashrc



    如果你没有输入密码,你会看到类似这在启动GitBash时:

     初始化新的SSH代理... 
    成功
    添加身份:/ c /Users/yourusername/.ssh/id_rsa(/c/Users/yourusername/.ssh/id_rsa)

    以下应该返回结果:

      $ ssh-add -l 

    但是,如果您从 ssh-add -l 获取以下内容:

     无法打开与身份验证代理的连接。 

    它没有产生SSH代理,您的.bashrc可能是原因。



    如果在启动GitBash时看到以下内容:

     初始化新的SSH代理。 .. 
    sh.exe::没有这样的文件或目录

    这意味着你忘记了在回显文件(即扩展变量)时使用\来转义$。重新创建.bashrc来解决此问题。



    验证代理

      $ ssh-add -l 
    正在运行并添加了您的密钥:



    应该返回类似于此的内容:

      2048 0f:37:21:af:1b:31:d5:cd:65:58:b2:68:4a:ba:a2:46 /Users/yourusername/.ssh/id_rsa(RSA)

    运行以下命令获取您的公钥:

      $ cat〜/ .ssh / id_rsa.pub 

    以ssh-rsa ......开头。


    • 点击GitBash窗口图标

    • 点击修改

    • 点击标记
    • 使用鼠标突出显示公钥(包括前面的 ssh-rsa 位和尾部的 == youremail@yourdomain.com

    • 右键单击该窗口(执行复制)
    • 将您的公钥粘贴到记事本中。
    • b $ b
    • 删除所有换行符,使其只有一行。
    • CTRL + A ,然后 CTRL + C 将公钥复制到剪贴板中。 通过执行以下步骤来关闭BitBucket:


      • 打开浏览器并导航到网站

      • 登录

      • 点击您的头像(右上)

      • 点击管理帐户

      • 点击SSH密钥(在左侧菜单的安全下)

      • 点击添加密钥

      • 输入全局公钥
      • code>标签
      • 粘贴您从记事本复制的公钥



      现在,您的密钥列表中应该显示全球公钥条目。
      $ b


      • 返回到GitBash

      • cd到包含您的项目的目录中

      • 将您的起源更改为SSH变体(如果您运行
      • pre> $ git remote -v

    切换到SSH网址:

      $ git remote set-url origin git@bitbucket.org:youraccount / yourproject.git 

    检查工作是否正常:

      $ git remote show origin 

    您应该看到如下所示:

     警告:将IP地址'...'的RSA主机密钥永久添加到已知主机列表中。 
    * remote origin
    获取URL:git@bitbucket.org:youruser / yourproject.git
    推送URL:git@bitbucket.org:youruser / yourproject.git
    分支头: master
    远程分支:
    跟踪的主人
    为'git push'配置的本地参考:
    主人推动主人(快速前进)

    完成!

    您可以选择使用HTTPS的SSH。它将要求您在远程操作过程中输入密码(键入一次后会暂时缓存)。这里是你如何配置HTTPS:



    对于懒惰



    您应该按照VonC的描述修复SSH问题;但是,如果您急于提交并且没有 tools / time / knowledge 来立即生成新的公钥,请将您的起源设置为HTTPS替代:

     > https://accountname@bitbucket.org/accountname/reponame.git 

    使用GUI工具,例如或。

    以下是这个替代原始URL的文档。



    命令行添加一个原点如果一个不存在:

      git remote add origin https://accountname@bitbucket.org/accountname/reponame.git 

    改变现有原点的命令行:

      git remote set-url origin https://accountname@bitbucket.org/accountname/reponame.git 

    注意:您的帐户名称不是您的电子邮件。



    您可能还想设置全局信息:

      git config --global user.nameYour Name
    git config --global user.emailyou@example.com

    然后再次尝试您的推送(无需再次提交)

      git push origin master 


    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.

    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.

    解决方案

    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).

    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

    For those looking to solve the actual problem, follow the instructions below:

    Fixing the SSH issue as fast as possible

    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.

    • Don't type the $ or any lines that do not begin with $ (the $ means this is something you type into GitBash).

    • Open GitBash

    Set your global info if you haven't already:

    $ git config --global user.name "Your Name"
    $ git config --global user.email "you@example.com"
    

    Check for OpenSSH:

    $ ssh -v localhost
    OpenSSH_4.6p1, OpenSSL...
    

    See something like that?

    • Yes: Continue.
    • No: Skip to the FOR THE LAZY section or follow the linked article from VonC.

    See if you have generated the keys already:

    $ ls -a ~/.ssh/id_*
    

    If there are two directories, 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.

    Check you are starting the SSH agent every time you run GitBash:

    $ 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 LAZY section.

    Enter the following into GitBash to create your .bashrc file:

    $ 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

    If you didn't enter a passphrase, you would have seen something like this when starting 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:

    Could not open a connection to your authentication agent.
    

    It didn't spawn the SSH agent and your .bashrc is likely the cause.

    If, when starting GitBash, you see this:

    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.

    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 ......"

    • 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 == youremail@yourdomain.com 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 then CTRL+C to copy the public key again to your clipboard.

    Configure your private key with BitBucket by performing the following steps:

    • Open your browser and navigate to the BitBucket.org site
    • 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

    A Global Public Key entry should now be visible in your list of keys.

    • 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 LAZY steps)

    Check your remotes:

    $ git remote -v
    

    Switch to the SSH url:

    $ git remote set-url origin git@bitbucket.org: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: git@bitbucket.org:youruser/yourproject.git
      Push  URL: git@bitbucket.org: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:

    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/knowledge to generate a new public key right now, set your origin to the HTTPS alternative:

    > https://accountname@bitbucket.org/accountname/reponame.git
    

    Using a GUI tool such as TortoiseGit or command line tools.

    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://accountname@bitbucket.org/accountname/reponame.git
    

    Command line to change an existing origin:

    git remote set-url origin https://accountname@bitbucket.org/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 "you@example.com"
    

    Then try your push again (no need to commit again)

    git push origin master
    

    这篇关于无法推送到Bitbucket上的Git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 01:52
查看更多