Git リポジトリで恒久的に認証する
In addition to SSH, Bitbucket Data Center and Server supports HTTP or HTTPS for pushing and pulling from managed Git repositories. However, Git does not cache the user's credentials by default, so you need to re-enter them each time you perform a clone, push, or pull.
This page describes two methods for permanently authenticating with Git repositories so that you can avoid typing your username and password each time you are pushing to or pulling from Bitbucket.
On this page
認証情報キャッシュの使用
HTTPS 認証情報キャッシュ機能を使用するには、Git 1.7.9 以上が必要です。
Windows
On Windows you can use the application git-credential-winstore.
- ソフトウェアをダウンロードします。
- 実行します。
- リポジトリに初めてアクセスするときに認証情報が求められ、Windows が今後の使用のために認証情報を保存します。
Linux
Linux では、Git 1.7.9 以降に同梱されている "cache" 認証ヘルパーを使用できます。Git のドキュメントには次の内容が記載されています。
This command caches credentials in memory for use by future git programs. The stored credentials never touch the disk, and are forgotten after a configurable timeout. The cache is accessible over a Unix domain socket,
restricted to the current user by filesystem permissions.
認証情報のキャッシュを有効化するには、以下のコマンドを使用します。認証情報のキャッシュを有効化すると、パスワードを入力してから 1 時間 (3600 秒) キャッシュされます。
git config --global credential.helper 'cache --timeout 3600'
"cache" 認証ヘルパーのすべての構成オプションの概要を確認するには、次のコマンドを実行します。
git help credential-cache
macOS
If you’re using macOS, Git comes with an "oxykeychain" mode. For more instructions, see credential storage.
.netrc ファイルの使用
The .netrc
file is a mechanism that allows you to specify which credentials to use for which server. This method allows you to avoid entering a username and password every time you push to or pull from Git, but your Git password is stored in plain text.
警告
- Git uses a utility called cURL under the covers, which respects the use of the .netrc file. Be aware that other applications that use cURL to make requests to servers defined in your
.netrc
file will also now be authenticated using these credentials. Also, this method of authentication is potentially unsuitable if you are accessing your Bitbucket instance via a proxy, as all cURL requests that target a path on that proxy server will be authenticated using your.netrc
credentials. - cURL will not match the machine name in your .netrc if it has a username in it, so make sure you edit your
.git/config
file in the root of your clone of the repository and remove the user and '@' part from any clone URL's (URL fields) that look likehttps://user@machine.domain.com/...
to make them look likehttp://machine.domain.com/...
Windows
- Create a text file called
_netrc
in your home directory (e.g.c:\users\kannonboy\_netrc
). cURL has problems resolving your home directory if it contains spaces in its path (e.g.c:\Documents and Settings\kannonboy
). However, you can update your%HOME%
environment variable to point to any directory, so create your_netrc
in a directory with no spaces in it (for examplec:\curl-auth\
) then set your%HOME%
environment variable to point to the newly created directory. 以下の形式を使用して、認証情報を保存するサーバーの認証情報をファイルに追加します。
machine stash1.mycompany.com login myusername password mypassword machine stash2.mycompany.com login myotherusername password myotherpassword
Linux または macOS
- Create a file called
.netrc
in your home directory (~/.netrc
). Unfortunately, the syntax requires you to store your passwords in plain text - so make sure you modify the file permissions to make it readable only to you. Add credentials to the file for the server or servers you want to store credentials for, using the format described in the 'Windows' section above. You may use either IP addresses or hostnames, and you do not need to specify a port number, even if you're running Bitbucket on a non-standard port.
- And that's it! Subsequent
git clone
,git pull
andgit push
requests will now be authenticated using the credentials specified in this file.