Development Tip

“git clone git@remote.git”를 실행할 때 사용자 이름과 비밀번호를 어떻게 제공합니까?

yourdevel 2020. 9. 29. 18:51
반응형

“git clone git@remote.git”를 실행할 때 사용자 이름과 비밀번호를 어떻게 제공합니까?


다음과 같이 HTTPS 요청에 사용자 이름과 비밀번호를 제공하는 방법을 알고 있습니다.

git clone https://username:password@remote

하지만 다음과 같이 리모컨에 사용자 이름과 비밀번호를 제공하는 방법을 알고 싶습니다.

git clone git@remote.git

나는 이렇게 시도했다 :

git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password

그러나 그들은 작동하지 않았습니다.


사용하다:

git clone https://username:password@github.com/username/repository.git

이 방법은 GitHub 저장소에서 저에게 효과적이었습니다.

Michael Scharf의 의견을 바탕으로 :

Bash 기록 파일에 기록되지 않도록 암호를 생략 할 수 있습니다.

git clone https://username@github.com/username/repository.git

암호를 입력하라는 메시지가 표시됩니다.


user@host:path/to/repo형식에 로그인 할 때 사용 SSH에 힘내을 알려줍니다 host사용자 이름 user. 에서 git help clone:

ssh 프로토콜과 함께 다른 scp와 유사한 구문을 사용할 수도 있습니다.

[user@]host.xz:path/to/repo.git/

앞에있는 부분 @은 사용자 이름이며 인증 방법 (비밀번호, 공개 키 등)은 Git이 아닌 ssh에 의해 결정됩니다. Git은 ssh에 암호를 전달할 방법이 없습니다. ssh는 원격 서버의 구성에 따라 암호를 사용하지 않을 수도 있기 때문입니다.

ssh-agent항상 암호를 입력하지 않으려면 사용

항상 ssh 암호를 입력하지 않으려면 일반적인 솔루션은 공개 / 개인 키 쌍생성 하고 원격 서버 ~/.ssh/authorized_keys파일공개 키를 넣은 다음 개인 키를 ssh-agent. 또한 한 번 로그인하도록 SSH를 통한 Git 구성 , ssh 키 암호에 대한 GitHub의 도움말 페이지 , gitolite의 ssh 문서Heroku의 ssh 키 문서를 참조하십시오 .

GitHub (또는 Heroku 또는 ...)에서 여러 계정 중에서 선택

GitHub 또는 Heroku와 같은 장소에 여러 계정이있는 경우 ssh 키가 여러 개 있습니다 (계정 당 하나 이상). 로그인 할 계정을 선택하려면 사용할 개인 키를 ssh알려야합니다 .

예를 들어, 두 개의 GitHub의 계정을 있다고 가정 foo하고 bar. 에 대한 ssh 키 foo~/.ssh/foo_github_id이고에 대한 ssh 키는 bar입니다 ~/.ssh/bar_github_id. 당신은 액세스하려는 git@github.com:foo/foo.git사용자와 foo계정과 git@github.com:bar/bar.git사용자와 bar계정. 다음을 추가합니다 ~/.ssh/config.

Host gh-foo
    Hostname github.com
    User git
    IdentityFile ~/.ssh/foo_github_id
Host gh-bar
    Hostname github.com
    User git
    IdentityFile ~/.ssh/bar_github_id

그런 다음 다음과 같이 두 개의 저장소를 복제합니다.

git clone gh-foo:foo/foo.git  # logs in with account foo
git clone gh-bar:bar/bar.git  # logs in with account bar

ssh를 아예 피하는 것

일부 서비스는 ssh의 대안으로 HTTP 액세스를 제공합니다.

  • GitHub :

    https://username:password@github.com/username/repository.git
    
  • 거대 함 :

    https://username:password@gitorious.org/project/repository.git
    
  • Heroku : 이 지원 문서를 참조하십시오 .

경고 : 복제 URL에 암호를 추가하면 Git에서 일반 텍스트 암호를 .git/config. HTTP를 사용할 때 암호를 안전하게 저장하려면 자격 증명 도우미를 사용하십시오. 예를 들면 :

git config --global credential.helper cache
git config --global credential.https://github.com.username foo
git clone https://github.com/foo/repository.git

The above will cause Git to ask for your password once every 15 minutes (by default). See git help credentials for details.


In the comments of @Bassetassen's answer, @plosco mentioned that you can use git clone https://<token>@github.com/username/repository.git to clone from GitHub at the very least. I thought I would expand on how to do that, in case anyone comes across this answer like I did while trying to automate some cloning.

GitHub has a very handy guide on how to do this, but it doesn't cover what to do if you want to include it all in one line for automation purposes. It warns that adding the token to the clone URL will store it in plaintext in .git/config. This is obviously a security risk for almost every use case, but since I plan on deleting the repo and revoking the token when I'm done, I don't care.

1. Create a Token

GitHub has a whole guide here on how to get a token, but here's the TL;DR.

  1. Go to Settings > Developer Settings > Personal Access Tokens (here's a direct link)
  2. Click "Generate a New Token" and enter your password again. (here's another direct link)
  3. Set a description/name for it, check the "repo" permission and hit the "Generate token" button at the bottom of the page.
  4. Copy your new token before you leave the page

2. Clone the Repo

Same as the command @plosco gave, git clone https://<token>@github.com/<username>/<repository>.git, just replace <token>, <username> and <repository> with whatever your info is.

If you want to clone it to a specific folder, just insert the folder address at the end like so: git clone https://<token>@github.com/<username>/<repository.git> <folder>, where <folder> is, you guessed it, the folder to clone it to! You can of course use ., .., ~, etc. here like you can elsewhere.

3. Leave No Trace

Not all of this may be necessary, depending on how sensitive what you're doing is.

  • You probably don't want to leave that token hanging around if you have no intentions of using it for some time, so go back to the tokens page and hit the delete button next to it.
  • If you don't need the repo again, delete it rm -rf <folder>.
  • If do need the repo again, but don't need to automate it again, you can remove the remote by doing git remote remove origin or just remove the token by running git remote set-url origin https://github.com/<username>/<repository.git>.
  • Clear your bash history to make sure the token doesn't stay logged there. There are many ways to do this, see this question and this question. However, it may be easier to just prepend all the above commands with a space in order to prevent them being stored to begin with.

Note that I'm no pro, so the above may not be secure in the sense that no trace would be left for any sort of forensic work.


Though there are many answers, myself facing the repeated issue when username or password has special characters in it.

URL encode your username and password for git, then use it as part of URL itself (when there is no security concern).

Say, URL encoded value of username

'user+1' is user%2B1

and URL encoded value of password

'Welcome@1234' is Welcome%401234

Then your GIT Clone URL would look like,

git clone https://user%2B1:Welcome%401234@actual-git-url-for-the-repo works perfectly, whereas,

git clone https://user+1:Welcome@1234@actual-git-url-for-the-repo gives you 403 errors

Hope this helps.

Just in case, want to URL encode online: https://www.urlencoder.org/


I solved this problem in the following way:

enter image description here


On Windows, the following steps should re-trigger the GitHub login window when git cloneing:

  • Search start menu for "Credential Manager"
  • Select "Windows Credentials"
  • Delete any credentials related to Git or GitHub

result


git config --global core.askpass

Run this first before cloning the same way, should be fixed!

참고URL : https://stackoverflow.com/questions/10054318/how-do-i-provide-a-username-and-password-when-running-git-clone-gitremote-git

반응형