How do I avoid the specification of the username and password at every git push?

前端 未结 18 1461
我在风中等你
我在风中等你 2020-11-28 00:02

I git push my work to a remote Git repository.

Every push will prompt me to input username and password. I would

相关标签:
18条回答
  • 2020-11-28 00:41

    Wire your git client to your OS credential store. For example in Windows you bind the credential helper to wincred:

    git config --global credential.helper wincred
    

    Is there a way to skip password typing when using https:// on GitHub?

    0 讨论(0)
  • 2020-11-28 00:42

    Permanently authenticating with Git repositories,

    Run the following command to enable credential caching.

    $ git config credential.helper store
    $ git push https://github.com/repo.git
    
    Username for 'https://github.com': <USERNAME>
    Password for 'https://USERNAME@github.com': <PASSWORD>
    

    Use should also specify caching expire,

    git config --global credential.helper 'cache --timeout 7200'
    

    After enabling credential caching, it will be cached for 7200 seconds (2 hours).

    Note: Credential helper stores an unencrypted password on a local disk.

    0 讨论(0)
  • 2020-11-28 00:42

    All this arises because git does not provide an option in clone/pull/push/fetch commands to send the credentials through a pipe. Though it gives credential.helper, it stores on the file system or creates a daemon etc. Often, the credentials of GIT are system level ones and onus to keep them safe is on the application invoking the git commands. Very unsafe indeed.

    Here is what I had to work around. 1. Git version (git --version) should be greater than or equal to 1.8.3.

    GIT CLONE

    For cloning, use "git clone URL" after changing the URL from the format, http://{myuser}@{my_repo_ip_address}/{myrepo_name.git} to http://{myuser}:{mypwd}@{my_repo_ip_address}/{myrepo_name.git}

    Then purge the repository of the password as in the next section.

    PURGING

    Now, this would have gone and

    1. written the password in git remote origin. Type "git remote -v" to see the damage. Correct this by setting the remote origin URL without password. "git remote set_url origin http://{myuser}@{my_repo_ip_address}/{myrepo_name.git}"
    2. written the password in .git/logs in the repository. Replace all instances of pwd using a unix command like find .git/logs -exec sed -i 's/{my_url_with_pwd}//g' {} \; Here, {my_url_with_pwd} is the URL with password. Since the URL has forward slashes, it needs to be escaped by two backward slashes. For ex, for the URL http://kris:passs123@192.168.0.1/proj.git -> http:\\/\\/kris:passs123@192.168.0.1\\/proj.git

    If your application is using Java to issue these commands, use ProcessBuilder instead of Runtime. If you must use Runtime, use getRunTime().exec which takes String array as arguments with /bin/bash and -c as arguments rather then the one which takes a single String as argument.

    GIT FETCH/PULL/PUSH

    1. set password in the git remote url as : "git remote set_url origin http://{myuser}:{mypwd}@{my_repo_ip_address}/{myrepo_name.git}"
    2. Issue the command git fetch/push/pull. You will not then be prompted for the password.
    3. Do the purging as in the previous section. Do not miss it.
    0 讨论(0)
  • 2020-11-28 00:47

    Appears that, at least when using TortoiseGIT on Windows, it is possible to create the SSH keys and transfer these to the GIT server using simply:

    > ssh-keygen.exe
    > ssh-copy-id [username]@[GIT_server]
    
    0 讨论(0)
  • 2020-11-28 00:48

    I used the answer that Pavel suggested and it worked for me. My difference was to do it while I was adding the remote like so: git remote add (alias) https://(name:password)@github.com/(the remote address).git

    0 讨论(0)
  • 2020-11-28 00:52

    I was using the https link (https://github.com/org/repo.git) instead of the ssh link;

    git@github.com:org/repo.git  
    

    Switching solved the problem for me!

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