Cheatsheet: Git Commands, SSH Key Management, and Clipboard Operations

Git Commands: Moving Changes to a New Branch

  1. Check Current Branch:

    git branch
  2. Create and Switch to a New Branch:

    git checkout -b new-branch-name
  3. Stage Changes:

    git add .
  4. Commit Changes:

    git commit -m "Commit message"
  5. Push Changes to New Branch:

    git push origin new-branch-name
  6. Switch Back to the Original Branch:

    git checkout original-branch-name
  7. Update Original Branch with Remote Changes:

    git fetch origin
    git reset --hard origin/original-branch-name

Rebase vs. Merge

  • Rebase:

    • Reapplies changes on top of another branch.
    • Cleaner, linear history but rewrites commit history.
    • Command: git rebase branch-name
  • Merge:

    • Combines changes from one branch into another.
    • Maintains history of both branches, creates a merge commit.
    • Command: git merge branch-name

SSH Key Management

  1. Install OpenSSH Server (if not installed):

    sudo apt update
    sudo apt install openssh-server
  2. Enable SSH Service to Start on Boot:

    sudo systemctl enable ssh
  3. Start SSH Service Immediately:

    sudo systemctl start ssh
  4. Verify SSH Service Status:

    sudo systemctl status ssh
  5. Copy Public Key to Server (using ssh-copy-id):

    ssh-copy-id username@server-ip
  6. Manually Add Public Key to authorized_keys:

    • Connect to server:
      ssh username@server-ip
    • Create .ssh directory:
      mkdir -p ~/.ssh
      chmod 700 ~/.ssh
    • Add public key:
      nano ~/.ssh/authorized_keys
    • Set permissions:
      chmod 600 ~/.ssh/authorized_keys
      chown username:username ~/.ssh
      chown username:username ~/.ssh/authorized_keys
  7. Test SSH Connection to GitHub:

    ssh -T git@github.com
  8. Check and Update Remote URL:

    git remote -v
    git remote set-url origin git@github.com:username/repository.git

Clipboard Operations

  1. Install xclip:

    sudo apt update
    sudo apt install xclip
  2. Install xsel:

    sudo apt update
    sudo apt install xsel
  3. Copy File Contents to Clipboard:

    • Using xclip:

      xclip -sel clip < filename
    • Using xsel:

      xsel --clipboard < filename
  4. Extract and Clean Public SSH Key:

    • Using awk:

      awk '{print $2}' ~/.ssh/id_rsa.pub
    • Using sed:

      sed 's/^[ \t]*//;s/[ \t]*$//' ~/.ssh/id_rsa.pub
    • Using cut:

      cut -d ' ' -f 2 ~/.ssh/id_rsa.pub
    • Using tr:

      tr -d ' ' < ~/.ssh/id_rsa.pub
  5. Copy Cleaned Key to Clipboard:

    • Using xclip:

      awk '{print $2}' ~/.ssh/id_rsa.pub | xclip -sel clip
    • Using xsel:

      awk '{print $2}' ~/.ssh/id_rsa.pub | xsel --clipboard
  6. Paste Key in Terminal:

    • Use Ctrl + V or Shift + Insert.

This cheatsheet covers Git branching and remote operations, managing SSH keys, and clipboard operations on Ubuntu.