Connect To Github Private Repo
Description:
So I’ve been using Google Drive to store my scripts in a “public repo” that I push my sanitized scripts to every so often, but I’ve always wanted to do it the “right way” by using version control (git) and sending to Github. Updated post to include pushing to personal and company repo at same time. Here are the steps I did to do that from my W10 machine:
To Resolve:
-
First, you have to find out if you want to use HTTPS or SSH for doing ‘pushes’ to Github/Gitlab/ect. I went with HTTPS for now. I may change it later - see this
-
Sign in to Github and create a repo, for example https://github.domain.com/gerry/powershell-test. The repo name is
powershell-test
and is initialized with a README.md on Github.- In the top right, click on Settings => Developer Settings => Personal Access Tokens => Click the ‘Generate new token’ button => Check ‘repo’ => Create => Jot this down somewhere, should look something like
1ef09e5aac03f99fc5135a105d104bac70652898
- In the top right, click on Settings => Developer Settings => Personal Access Tokens => Click the ‘Generate new token’ button => Check ‘repo’ => Create => Jot this down somewhere, should look something like
-
If you haven’t already, install GCM. This will allow Windows to store your git credentials securely.
-
Clean up your git repos on your computer. See my other post on this for more details. But the idea is that you:
- Clear your credential manager of anything pointing to Github
- Clear your global git configs and set them back almost empty:
1 2 3 4 5 6 7
Remove-Item ~/.gitconfig # Now, set it back to what you want it to be: # NOTE: It is important that you run this in powershell on Windows if you have WSL installed. If you do this in bash, vscode will ignore your git config. git config --global core.autocrlf true git config --global credential.helper manager git config --global credential.useHttpPath true
- The key for multiple accounts here is the useHttpPath setting and here. This will allow you to push to multiple accounts as needed.
- Now, for each repo you initialize, set it’s settings with a
--local
switch so you can push to multiple Github accounts.
1 2 3 4 5 6 7 8 9
New-Item -itemtype Directory -Path "c:\scripts\repo\home\powershell-test" cd "c:\scripts\repo\home\powershell-test" git init . git config -l # see that my user.name and user.email is not listed. This is because all that is inherited at this time is git Global settings. We will need these set for commits so we add them like: git config --local user.name 'Gerry Williams' git config --local user.email 'gerry.williams@domain.com' git config -l # looks good now
-
Now, to add the Github repo to my computer and authenticate with it, I run the following in vscode:
1 2 3 4 5 6 7 8 9 10 11
New-Item -ItemType File -Path . -Name "myfile.txt" git add --all git commit -m 'first' git remote add origin https://github.domain.com/gerry/powershell-test git pull origin master --allow-unrelated-histories # When you do the pull, GCM will pop up over VSCode and ask you to enter credentials into it instead of vscode # enter username for github.com and the password as the access token git push --set-upstream origin master # now anytime you do a `git push`, it will use the credential manager and shouldn't ask for a password
- The Git Credential Manager for Windows will often make a mistake saving your credentials, I have found that manually entering them as a ‘generic credential’ works well:
- Open Credential Manager ( Run =>
rundll32.exe keymgr.dll, KRShowKeyMgr
) - Enter
git:https://your-repo.git
as the address - Enter
your-github-username
as the user - Enter
your-github-personal-access-token
as the password - Also, they have been using
main
instead of master lately so the whole thing looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
git config --local user.name 'Gerry Williams' git config --local user.email 'me@company.com' New-Item -ItemType File -Path . -Name "myfile.txt" | Out-Null git add --all git commit -m 'first' # Don't forget to add the .git on the end or Credential Manager will not tie these together! git remote add origin https://github.domain.com/gerry/powershell-test.git git branch -M main #credential manager # git:https://github.domain.com/gerry/powershell-test # username: your-github-username # password: your-github-Personal-Access-Token git pull origin main --allow-unrelated-histories git push --set-upstream origin main # if you open the repo in vscode, it should work fine now
- Open Credential Manager ( Run =>
-
Using SSH instead would work like (NOTE: I haven’t ever tested this since HTTPS seems more common):
- Generate a SSH key pair and copy the public portion to your clipboard
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-add ~/.ssh/id_rsa
clip < ~/.ssh/id_rsa.pub
- Add the ssh key to your github account.
- Clone the repo by the following:
1 2 3
git config --local user.name 'Gerry Williams' git config --local user.email 'gerry.williams@domain.com' git clone git@github.domain.com/gerry/powershell-test.git
- Generate a SSH key pair and copy the public portion to your clipboard
-
To reiterate how I separate my work and home credentials, I just run:
1 2 3 4 5 6 7 8 9 10
git config --global credential.useHttpPath true git config --global core.autocrlf true git config --global credential.helper manager git config --global --list # verify that is all that is listed # now open credential manager and delete all https://github.com saved credentials # now each time you try again in vscode, it will ask on a per folder basis # enter username for github.com and the password as the access token, GCM will update Credential Manager for you.
- Quicker repo creation after you have a personal access token:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# create repo in Github but don't initialize with a readme
# Create folder in File Explorer
# Inside folder, open powershell regular (non-elevated)
git init .
git config --local user.name 'Gerry Williams'
git config --local user.email 'gerry.williams@domain.com'
New-Item -itemtype File -Path . -Name "myfile.txt"
git add --all
git commit -m 'first'
git branch -M master
git remote add origin https://github.domain.com/gerry/powershell-test
git pull origin master --allow-unrelated-histories
# enter github username and personal-access-token in window that pops up.
# now add folder to vscode workspace and it will auto-detect git and credentials
git push --set-upstream origin master
- Troubleshooting:
- Run
git status
often and see if it shows any errors. If so, correct them by googling the answers. - Run
git config -l
and look for the correct origin. - If it is wrong, type
git remote rm origin
andgit remote add origin https://path/to/repository
then confirmgit remote get-url --all origin
- Run
Comments