Monday, June 16, 2025

My GitHub and Git Cheetsheet

 GITHUB + GIT CHEAT SHEET


Assumption:

The repository name is <repo>, and the GitHub username is <user>.

GitHub URL: https://github.com/<user>/<repo>

The local project directory for the source code is <project_name>.

You are working on your own  Linux/macOS/Windows server or development machine.

PART 1: ONE-TIME SETUP

This must be done once for each new project.

1. Install Git (if not already installed which can be verified by calling  git — version)


Linux (with apt)

sudo apt update

sudo apt install git


macOS (with Homebrew)

# Install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install git:

brew install git


Windows (with Chocolate Package Manager)

choco install git.install



2. Configure your Git identity


git config --global user.name "Your Full Name"

git config --global user.email "your@email.com"


3. Create an SSH key for GitHub (if you don’t have one yet)


ssh-keygen -t ed25519 -C "your@email.com"


4. Add your public key to GitHub


cat ~/.ssh/id_ed25519.pub


Copy the output, then:

Go to: https://github.com/settings/keys

Click: New SSH key

Paste the key, name it something like “MyServer”


5. Test SSH connection


ssh -T git@github.com


You should see:


Hi <user>! You've successfully authenticated, but GitHub does not provide shell access.


6. Create your repo on GitHub


Go to: https://github.com/new

Name it whatever you prefer

Leave it empty (don’t initialize with README, license, etc.)


7. Initialize the local project


cd ~/projects/<project_name>    # Or wherever your project lives

git init

git add .

git commit -m "Initial commit"


8. Add the GitHub remote


git remote add origin git@github.com:<user>/<repo>.git


git branch -M main        # Rename local branch to main

git push -u origin main   # Push and set tracking branch





PART 2: DAILY WORKFLOW


These steps are what you use day-to-day as you edit and work on the code.


1. Check current status


git status


2. See what changed


git diff


3. Add new or changed files


git add file1.py file2.txt     # Add specific files

git add .                      # Add all changed files


4. Commit with a message


git commit -m "Fix bug in login logic"


5. Push changes to GitHub


git push


6. Pull latest changes from GitHub


git pull


7. See log of commits


git log --oneline --graph --decorate


8. Create a new branch


git checkout -b feature-x


9. Switch between branches


git checkout main

git checkout feature-x


10. Merge changes from another branch


git checkout main

git merge feature-x


11. Clone an existing repo (on a different machine)


git clone git@github.com:<user>/<repo>.git


12. Avoid pushing unnecessary files

Add a .gitignore file, for example :


__pycache__/

*.pyc

*.log

.DS_Store

venv/


  1. Set origin to remote Github repository


git remote set-url origin git@github.com:<user>/<repo>.git


  1. Use ssh agent on own machine and add ssh key


eval "$(ssh-agent -s)" 


ssh-add ~/.ssh/id_rsa_github


  1. Check connection for <repo>


git remote -v


  1. Check Logs


git log





TROUBLESHOOTING


“src refspec main does not match any”

→ You haven’t committed anything yet, or the branch name is wrong.


“Permission denied (publickey)”

→ SSH key isn’t added or GitHub doesn’t recognize it. Check ssh -T git@github.com.


“Everything up-to-date but nothing on GitHub”

→ You’re pushing to the wrong branch (master instead of main). 


Fix with:


git branch -M main

git push -u origin main


No comments: