Skip to content

GitHub CheatSheet

Niko Lee edited this page Feb 27, 2023 · 7 revisions

Using GitHub

Prepare SSH key

# Generate
ssh-keygen -t ed25519 -C "<comment>" -f <keyfile>

# Copy/Move private key to `~/.ssh` directory
cp <keyfile> ~/.ssh/

# Protect private key
sudo chmod 600 ~/.ssh/<keyfile>

# Add public key to GitHub, Settings->SSH & GPG Keys->New SSH key
cat <keyfile.pub>

Git configure

Basic settings

# Change to Repo directory
# --global, --system, --local (default)

git config user.name '<your-name>'
git config user.email '<your-email>'

# Credential helper

# Display setting
git config credential.helper

# Remove setting
git config --unset credential.helper

# Save credential
git config credential.helper store

# Cache credential
git config credential.help cache
git config credential.help cache --timeout=900

Edit Git configuration

git config --edit

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        sshCommand = "ssh -i ~/.ssh/<keyfile-private>"
[remote "origin"]
        url = [email protected]:<github-username>/<repo>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[user]
        name = <your-name>
        email = <your-email>
[credential]
        helper = store

Git command

# Initial git repository
git init

# Add file(s)
git add <filename>
# Add all file(s)
git add .

# Commit changes
git commit -m "commit message here"

# Rename default branch master to main
git branch -M main

# Add remote 
git remote add origin [email protected]:<github-username>/<repo>.git

# Push
git push -u origin main

# Checkout new branch
git checkout -b <new-branch-name>

# Switch branch
git switch <other-branch-name>

# Show git branch
git branch -a

# Show git status
git status

# Show different
git diff <branch-1>:file <branch-2>:file

# Clone repository
git clone https://github.com/<github-username>/<repo>.git .
git clone --branch <branch-name> <remote-repo-url>

# "merge" specific files from another branch
git checkout <feature branch> <file1> <file2> <file3> ... 

.gitignore

/local

.gitattributes

  • Windows: Create a new text file (Right click>new>text file) within windows explorer and rename it (Shortcut : F2) as follows
    .gitattributes.
    
    Note: That is DOT + gitattributes + DOT, windows will remove the trailing DOT for you producing the appropriate filename
  • Unix: and it's variants (Ubuntu, Raspberrian, Mac OS etc.)
    touch .gitattributes
    
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Declare NIPO q-file that will have CRLF line endings and encoding as UTF-16LE-BOM
*q text working-tree-encoding=UTF-16LE-BOM eol=CRLF

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary