Cheatsheet: Git Commands, SSH Key Management, and Clipboard Operations
Git Commands: Moving Changes to a New Branch
-
Check Current Branch:
git branch
-
Create and Switch to a New Branch:
git checkout -b new-branch-name
-
Stage Changes:
git add .
-
Commit Changes:
git commit -m "Commit message"
-
Push Changes to New Branch:
git push origin new-branch-name
-
Switch Back to the Original Branch:
git checkout original-branch-name
-
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
-
Install OpenSSH Server (if not installed):
sudo apt update sudo apt install openssh-server
-
Enable SSH Service to Start on Boot:
sudo systemctl enable ssh
-
Start SSH Service Immediately:
sudo systemctl start ssh
-
Verify SSH Service Status:
sudo systemctl status ssh
-
Copy Public Key to Server (using
ssh-copy-id
):ssh-copy-id username@server-ip
-
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
- Connect to server:
-
Test SSH Connection to GitHub:
ssh -T git@github.com
-
Check and Update Remote URL:
git remote -v git remote set-url origin git@github.com:username/repository.git
Clipboard Operations
-
Install
xclip
:sudo apt update sudo apt install xclip
-
Install
xsel
:sudo apt update sudo apt install xsel
-
Copy File Contents to Clipboard:
-
Using
xclip
:xclip -sel clip < filename
-
Using
xsel
:xsel --clipboard < filename
-
-
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
-
-
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
-
-
Paste Key in Terminal:
- Use
Ctrl + V
orShift + Insert
.
- Use
This cheatsheet covers Git branching and remote operations, managing SSH keys, and clipboard operations on Ubuntu.