Cannot push to Git repository on Bitbucket

后端 未结 21 666
春和景丽
春和景丽 2020-11-29 14:40

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

相关标签:
21条回答
  • 2020-11-29 15:36

    This error also shows up when the repository does not exist. I tried all the answers until I saw the repo name was missing a dash

    0 讨论(0)
  • 2020-11-29 15:37

    I got this error

    Connection to bitbucket.org closed by remote host. fatal: Could not read from remote repository. Please make sure you have the correct access rights.

    Then i tried

    git config --global user.email "you@example.com"

    worked without quotes.

    0 讨论(0)
  • 2020-11-29 15:37

    Git has changed some of its repo instructions - check that you have connected your local repo to the Git cloud - check each of these steps to see if you have missed any.

    Git documentation[https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh] if you prefer following documentation - it is far more detailed and worth reading to understand why the steps below have been summarised.

    My Git Checklist:-

    1. The master branch has changed to main
    2. If you have initialised your repo and want to start from scratch, un-track git with $rm -rf .git which recursively removes git
    3. Check you're not using "Apple Git". Type which git it should say /usr/local/bin/git - if you are install git with Homebrew $brew install git
    4. Configure your name and email address for commits (be sure to use the email address you have registered with Github):
    $git config --global user.name "Your Name"
    $git config --global user.email "you@example.com"
    
    • Configure git to track case changes in file names:
    $git config --global core.ignorecase false
    

    If you have made a mistake you can update the file $ls -a to locate file then $open .gitignore and edit it, save and close.

    1. Link your local to the repo with an SSH key. SSH keys are a way to identify trusted computers, without involving passwords.
      Steps to generate a new key
    • Generate a new SSH key by typing ssh-keygen -t rsa -C "your_email@example.com" SAVE THE KEY
    • You'll be prompted for a file to save the key, and a passphrase. Press enter for both steps leaving both options blank (default name, and no passphrase).
    • Add your new key to the ssh-agent: ssh-add ~/.ssh/id_rsa
    • Add your SSH key to GitHub by logging into Github, visiting Account settings and clicking SSH keys. Click Add SSH key

    You can also find it by clicking your profile image and the edit key under it in the left nav.

    • Copy your key to the clipboard with the terminal command: pbcopy < ~/.ssh/id_rsa.pub

    • In the Title field put something that identifies your machine, like YOUR_NAME's MacBook Air

    • In the Key field just hit cmd + V to paste the key that you created earlier - do not add or remove and characters or whitespace to the key

    • Click Add key and check everything works in the terminal by typing: ssh -T git@github.com

      You should see the following message:

      Hi YOUR_NAME! You've successfully authenticated, but GitHub does not provide shell access.
      

    Now that your local machine is connected to the cloud you can create a repo online or on your local machine. Git has changed the name master for a branch main. When linking repos it is easier to use the HTTPS key rather than the SSH key. While you need the SSH to link the repos initially to avoid the error in the question.

    Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    Follow the steps you now get on your repo - GitHub has added an additional step to create a branch (time of writing Oct 2020).

    • to create a new repository on the command line echo "# testing-with-jest" >> README.md git init git add README.md git commit -m "first commit" git branch -M main git remote add origin — (use HTTPS url not SSH) git push -u origin main

    • to push an existing repository from the command line git remote add origin (use HTTPS url not SSH) git branch -M main git push -u origin main

    If you get it wrong you can always start all over by removing the initialisation from the git folder in your local machine $rm -rf .git and start afresh - but it is useful to check first that none of the steps above are missed and always the best source of truth is the documentation - even if it takes longer to read and understand!

    0 讨论(0)
  • 2020-11-29 15:38

    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.

    1. In SourceTree, go to Preferences.
    2. Go to the Accounts tab and select your account.
    3. There should be an option to generate and copy an SSH key to clipboard.
    4. Once you have copied that, go to Bitbucket in your browser. Go to [avatar] -> Bitbucket settings.
    5. Go to SSH keys.
    6. Click Add key
    7. Paste in the key you copied.

    I received a confirmation email from Bitbucket that an SSH key had been added to my account.

    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.

    ls -la ~/.ssh
    

    As others have stated, this documentation helped me: Use the SSH protocol with Bitbucket Cloud

    0 讨论(0)
  • 2020-11-29 15:40

    This is probably caused by having multiple SSH keys in SSH agent (and/or BitBucket). Check Atlassian documentation for the workaround for this

    0 讨论(0)
  • 2020-11-29 15:41

    I had the same problem. My SSH keys were set correctly. I fixed this problem like this.

    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.

    0 讨论(0)
提交回复
热议问题