Git:有什么方法可以设置默认登录凭据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7842719/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:07:39  来源:igfitidea点击:

Git: Any way to set default login credentials?

git

提问by Kirk Strobeck

I'm using terminal for mac and running the line

我正在使用 Mac 终端并运行线路

git push origin master

It asks me for my github.com username and password every time,
is there any way to have it automatically use my credentials?

它每次都要求我提供我的 github.com 用户名和密码,
有没有办法让它自动使用我的凭据?



I keep getting the error

我不断收到错误

error: The requested URL returned error: 403 while accessing 
https://github.com/atheycreek/churchdeploy.git/info/refs


[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = https://github.com/atheycreek/churchdeploy.git

So I changed it to

所以我把它改成

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git:github.com/atheycreek/churchdeploy.git

now I get..

现在我得到..

kirkstrobeck:churchdeploy kirkstrobeck$ git push origin master
ssh: Could not resolve hostname git: nodename nor servname provided, or not known
fatal: The remote end hung up unexpectedly


I changed it to

我把它改成

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]/atheycreek/churchdeploy.git

and now i get ..

现在我得到..

kirkstrobeck:churchdeploy kirkstrobeck$ git push origin master
fatal: '[email protected]/atheycreek/churchdeploy.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

回答by Colin R

From your description of things, it sounds like your [Project Dir]/.git/config file is setup with the line url = https..., and not url = [email protected].... Can you check that file to see what it says? It'd be great if you could post your entire "remote origin" section. It probably looks something like this:

从你的描述的东西,这听起来像你的[项目DIR] /。混帐/ config文件是建立与线url = https...,而不是url = [email protected]...。你能检查那个文件看看它说了什么吗?如果您可以发布整个“远程来源”部分,那就太好了。它可能看起来像这样:

[remote "origin"]
    url = https://github.com/atheycreek/churchdeploy.git
    fetch = +refs/heads/*:refs/remotes/origin/*

but needs to use ssh instead of http like this:

但需要像这样使用 ssh 而不是 http:

[remote "origin"]
    url = [email protected]:atheycreek/churchdeploy.git
    fetch = +refs/heads/*:refs/remotes/origin/*

回答by manojlds

Setup your ssh keys appropriately with empty passphrase and you need not enter the credentials: http://help.github.com/mac-set-up-git/

使用空密码正确设置您的 ssh 密钥,您无需输入凭据:http: //help.github.com/mac-set-up-git/

回答by Sean

If on OSX, you should be able to use the osxkeychain helper. You can check to see if you already have it installed by typing:

如果在 OSX 上,您应该能够使用 osxkeychain 助手。您可以通过键入以下内容来检查您是否已经安装了它:

git credential-osxkeychain

If you get a message saying that's not a valid git command, you can install it by doing:

如果你收到一条消息说这不是一个有效的 git 命令,你可以通过执行以下操作来安装它:

curl -s -O http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain
chmod u+x git-credential-osxkeychain
sudo mv git-credential-osxkeychain `dirname \`which git\``

Then tell git to use it with:

然后告诉 git 使用它:

git config --global credential.helper osxkeychain

You will be asked to provide your credentials once more the next time you do a pull/push. From then on, git should remember your info.

下次执行拉/推操作时,系统会要求您再次提供凭据。从那时起,git 应该记住你的信息。

回答by Mikhail Glushenkov

Using an empty passphrase is considered bad practice. Quoting Help.Github:

使用空密码被认为是不好的做法。引用Help.Github

Passwords aren't very secure, you already know this. If you use one that's easy to remember, it's easier to guess or brute-force. If you use one that's random it's hard to remember, and thus you're more inclined to write the password down. Both of these are Very Bad Things?. This is why you're using ssh keys.

But using a key without a passphrase is basically the same as writing down that random password in a file on your computer. Anyone who gains access to your drive has gained access to every system you use that key with. This is also a Very Bad Thing?. The solution is obvious: add a passphrase.

密码不是很安全,你已经知道了。如果您使用易于记忆的方法,则更容易猜测或蛮力。如果您使用随机的密码就很难记住,因此您更倾向于写下密码。这两个都是非常糟糕的事情?。这就是您使用 ssh 密钥的原因。

但是使用没有密码短语的密钥与在计算机上的文件中记下随机密码基本相同。任何可以访问您的驱动器的人都可以访问您使用该密钥的每个系统。这也是一件非常糟糕的事情?. 解决方案很明显:添加密码。

The right solution here is to use ssh-agent- this way, Git will ask for your password only once per session. See this pagefor pointers on how to set it up on your system.

正确的解决方案是使用ssh-agent- 这样,Git 每次会话只会要求您输入一次密码。有关如何在系统上进行设置的指示,请参阅此页面

回答by Newtopian

setup your API token as well as your SSH credential (password less though preferably with password with an agent)

设置您的 API 令牌以及您的 SSH 凭据(密码较少,但最好使用代理密码)

The link providedin other answer provides instructions for this as well. Specifically look for step 2 at the bottom.

其他答案中提供的链接也提供了相关说明。具体查找底部的第 2 步。