Github draft

Last modified 7 months ago / Edit on Github
Danger icon
This is a draft, the content is not complete and of poor quality!

👉 Note: SSH.
👉 Note: Git.
👉 Note: Gitkraken.

View README.md localhost

pip install grip # https://github.com/joeyespo/grip
cd myrepo
grip # Running on http://localhost:6419/

Update: grip support with limited times of usage, we can use VSCode markdown previwer instead.

Clone with Personal Access Tokens (PAT)

Go to this link to generate a new PAT. Then using your PAT as a password.

$ git clone https://github.com/fakeuser/fake-repo.git
Username: <your_username>
Password: <your_personal_access_token>

Clone via git@ (ssh)

Success icon

Update: It's easier if we use Github CLI!

👉 More references: this and this.

If using 1 single account or need to create id keys?
# Windows + Linux
ssh-keygen -t rsa -b 4096 -C "[email protected]"
# (-C for adding comment only)
# Enter a file:
# Linux: /home/thi/.ssh/id_rsa
# Windows: C:\Users\dinha\.ssh\id_rsa
# Enter password
Tell who you are
# Tell who you are? (it's global, you may need to set it locally for each repo)
git config --global user.name "Thi Dinh"
git config --global user.email "[email protected]"

In case you have multiple accounts, you have to indicate separatedly the account in each repository,

git config user.name "Thi"
git config user.email "[email protected]"
ssh/config for different accounts

If 2 accounts on 2 different platforms,

# ~/.ssh/config
# Default github account: [email protected]
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes

# Other github account: [email protected]
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_ideta
IdentitiesOnly yes

If 2 accounts on the same platforms, eg. Github

Host *
IdentityFile ~/.ssh/id_rsa.thi
AddKeysToAgent yes

Host *
IdentityFile ~/.ssh/id_rsa.ideta
AddKeysToAgent yes
If you use zsh?

It's important on linux, otherwise, you won't be able to use ssh-agent in zsh environment

# ~/.zshrc
plugins=(git ssh-agent)

update_ssh(){
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa.ideta
ssh-add ~/.ssh/id_rsa.thi
}
alias ud_ssh='update_ssh'

Everytime you have problems, just ud_ssh.

Reset ssh-agent on terminal
# Delete all cached keys (be careful)
ssh-add -D

# Be careful, it will reset and use different agent!!!!
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
# ssh-add ~/.ssh/id_rsa_ideta

# Check saved keys
ssh-add -l
# Go to https://github.com/settings/keys
# copy public key
cat ~/.ssh/id_rsa.pub
# clone some repo
git clone [email protected]:dinhanhthi/dinhanhthi.com.git

Info

  • Commits to a fork don't appear in your contributions graph.
  • Commits to a generated from template can appear in your contributions graph.
  • Get size of a github repo: https://api.github.com/repos/<user>/<repo> then find property "size". The size is in KB.

Repo template

Archived on 14/10/21

I wanna make a theme notetheme2 based on dinhanhthi.com.

  1. Make dinhanhthi.com be a template (Go to Settings)

  2. Create a new repo based on this template.

  3. Create a new branch notetheme2 on dinhanhthi.com. Make changes on this branch.

  4. Everything we have a "theme change" on dinhanhthi.com/master, merge it to branch dinhanhthi.com/notetheme2.

  5. If there are files (only for files) in dinhanhthi.com/notetheme2 you wanna keep, add below line to .gitattributes (under branch dinhanhthi.com/notetheme2) before performing the merge,

    # add line to .gitattributes
    echo 'file_name.txt merge=ours' >> .gitattributes
    # on windows, remove `''`
  6. If there are folders (or files) in dinhanhthi.com/notetheme2 you don't wanna keep (from /master), just delete them and make a new commit. From this time, later merges will ignore them.

  7. If there are folders in /notethem2 you wanna keep the current state (instead of merging from master), create a script reset_folders.sh

    #!/bin/sh
    # used for branch notetheme2 only
    echo 'Reset some only-this-branch folders after merging.'
    git reset folder_1 folder_2
    git checkout .
    git add .
    git commit -m "update from master"

    Each time you run the merge, run

    git merge master && sh reset_folders.sh
  8. Update changes from dinhanhthi.com/notetheme2 to repo notetheme2[ref].

    # add dinhanhthi.com as a remote
    git remote add template [URL of the template repo]
    # update the changes
    git fetch --all
    # update from template's branch
    git merge template/notetheme2
  9. If there is an error fatal: refusing to merge unrelated histories, try to add --allow-unrelated-histories. There must be conflict.

    # keep remotes
    git merge -X theirs template/notetheme2 --allow-unrelated-histories
    # keep local
    git merge -X ours template/notetheme2 --allow-unrelated-histories

Add Shields tags

👉 Main shield site here.
👉 Example of usage: my main github page.

# scikit-learn badge
http://img.shields.io/badge/-Scikit%20Learn-efa300?style=flat-square&logo=scikit-learn&logoColor=fff
# ...<NAME>-<Background-Color>?style...&logo=<LOGO>&logoColor=<LOGO-COLOR>
  • Logo name can be found at simpleicon. If the name includes spaces, replace them with dashes (e.g: ?logo=visual-studio-code)
  • Wanna create a custom logo?
    • Change SVG to PNG if needed: use this site.
    • (Optional) Change logo's color to white: use this site.
    • Must resize the image to 14x14: use this site.
    • Convert to base64 file: use this site, then copy data:image/png;base64,iVB... and paste after &logo=

Troubleshooting

🐞 fatal: Authentication failed for: It's because you enabled two-factor authentication in your Github account.

  1. Generate a new token: click here.
  2. Copy that token and use it as a new password.

🐞Could not read from remote repository

ssh: connect to host github.com port 22: No route to host
fatal: Could not read from remote repository
# solution
nano ~/.ssh/config
# add following
Host github.com
Hostname ssh.github.com
Port 443
# run again to check
ssh -T [email protected]

🐞 push declined due to email privacy restrictions

This problem happens when you set your email to private (Go to https://github.com/settings/emails, you clicked on "Keep my email addresses private"). When you tick on this option, Github will generate an alternative email ("noreply" email) for you, eg. [email protected]. What you should do to make this email work with the git push:

  1. Make sure the repo you are working all is linked with this new email.

    # Check email you use in the current project
    cd <current-project>
    cat .git/config

    # If above doesn't have any email, it means you are using the global
    # Check the global
    git config user.email

    # You must:
    # either change the current project to "noreply" email
    cd <current-project>
    git config user.email <noreply-email>
    # or change the global
    git config --global user.email <noreply-email>
  2. Make sure ALL the commits you are going to push are assigned to new email (It means that the things you have just commited were assigned to the old email)

    # If you only have 1 commit assigned to the old email
    # Reset it
    git reset --soft HEAD~1

    If you have multiple commit to fix, try this.

🐞 Warning: Remote Host Identification Has Changed

Open /Users/thi/.ssh/known_hosts, remove the line containing Github. Push again (make sure to use the right user), you will be asked for trusting Github again.

💬 Comments

Support Thi Support Thi