Question:
These days when I create a new repository on GitHub on the setup page I get:
1 2 3 |
git remote add origin https://github.com/nikhilbhardwaj/abc.git git push -u origin master |
And whenever I have to push a commit I need to enter my GitHub username and password.
I can manually change that to
1 2 |
git@github.com:nikhilbhardwaj/abc.git |
in the .git/config
. I find this quite irritating – is there some way I can configure git to use SSH by default?
Answer:
Set up a repository’s origin branch to be SSH
The GitHub repository setup page is just a suggested list of commands (and GitHub now suggests using the HTTPS protocol). Unless you have administrative access to GitHub’s site, I don’t know of any way to change their suggested commands.
If you’d rather use the SSH protocol, simply add a remote branch like so (i.e. use this command in place of GitHub’s suggested command). To modify an existing branch, see the next section.
1 2 |
$ git remote add origin git@github.com:nikhilbhardwaj/abc.git |
Modify a pre-existing repository
As you already know, to switch a pre-existing repository to use SSH instead of HTTPS, you can change the remote url within your .git/config
file.
1 2 3 4 5 |
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* -url = https://github.com/nikhilbhardwaj/abc.git +url = git@github.com:nikhilbhardwaj/abc.git |
A shortcut is to use the set-url
command:
1 |
$ git remote set-url origin git@github.com:nikhilbhardwaj/abc.git |