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

前端 未结 18 1782
我在风中等你
我在风中等你 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: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.

提交回复
热议问题