Install Git
pkg update && pkg upgrade
pkg install git unzip openssh
- Git
- Tracks code changes
- Unzip
- Extracts zip files
- OpenSSH
- Enables secure SSH connections
Initialize A Repository
git init
- Navigate first to your project using cd
- Makes the cd a Git repository
- Creates a .git folder and this folder stores all Git metadata: commit history, branches, configuration, etc.
- You only need to run git init once per project
Configuration
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
git config --list
- git config --global
-
- First-time setup
- Only when changing
- git config --list
- Check
Local Copy Of Remote Repo
git clone <repository-URL>
git clone git@github.com:username/repo.git
git clone <repository-URL> my-folder
- git init is not needed after cloning
- git clone automatically sets up a remote called origin
- Clone via SSH is more secure than HTTPS, but requires an SSH key
What To Do After Cloning
-
Go into the repo folder:
cd repo -
Check or switch branches if needed:
git branch git checkout feature-branch -
Edit files → stage → commit → push:
git add . git commit -m "My changes" git push
Remote Repository Management
HTTPS (Easy Setup)
git remote add origin https://github.com/username/repo.git
- First auth requires username + PAT, then cached
- Generate token: GitHub → Settings → Developer settings → Personal Access Tokens
SSH (Secure & Convenient)
Step 1: Generate SSH Key In Termux
ssh-keygen -t ed25519 -C "your_email@example.com"
- Private key
- ~/.ssh/id_ed25519
- Public key
- ~/.ssh/id_ed25519.pub
- Enter file in which to save the key:
→ Press Enter (default) - Enter passphrase:
→ Type one or press Enter (none)
Step 2: Add Public Key To GitHub
cat ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/id_ed25519
- Copy the output
- GitHub → Settings → SSH and GPG keys → New SSH key → Paste
- chmod 600 ~/.ssh/id_ed25519
- Permission
Step 3: Test Connection
ssh -T git@github.com
- Success message: Hi username! You've successfully authenticated…
Step 4: Add Remote Using SSH URL
git remote add origin git@github.com:username/repo.git
Choosing Between HTTPS And SSH
-
HTTPS
- Quick & easy
- Good for temporary use or public computers
-
SSH
- Best for frequent work
- Great for automation, scripts, and Termux Widgets
Change Remote URL
git remote set-url remote_name newURL
Remove Remote
git remote remove remote_name
List Remote
git remote -v
Branch Management
List Branches
git branch
Create A New Branch
git branch branch_name
Switch Branch
git checkout branch_name
git switch branch_name
- checkout or switch
Create & Switch In One Step
git checkout -b new_branch
Merge To main Branch
git checkout main
git merge branch_name
Renames The Current Branch
git branch -m new-name
git branch -M new-name
- git branch -m new-name
- Fails if a branch with the new name already exists
- git branch -M new-name
- Overwrites the target branch if it already exists
Delete Branch
git branch -d branch_name
git branch -D branch_name
- git branch -d branch_name
- This works only if the branch has already been merged
- git branch -D branch_name
- Deletes the branch even if it hasn’t been merged
Stage Files
git add filename
git add .
git reset
git reset file
- git add .
- . stages all changes
- git reset
- Unstage added files
- Add changes to the staging area before making a commit
Commit Changes
git commit -m "Commit message"
git commit --allow-empty -m "Commit message"
git log
- --allow-empty
- Creates a commit with no changes
- git log
- View history
Check Status
git status
-
Before starting work
- Check which files have been modified or are untracked
→ Helps you see “Do I need to commit this file?”
- Check which files have been modified or are untracked
-
After git add
- Verify which files are staged
→ Shows “Which files will be included in the next commit?”
- Verify which files are staged
-
Before committing
- Double-check which changes are not yet committed to avoid missing anything
-
During work
- If you’re editing multiple files, use it to keep track of your changes and stay organized
Fetch & Merge
git pull remote_name branch_name
git pull origin main --rebase
- git pull origin main --rebase
- Apply your commits after the remote commits
- Get the latest changes from the remote repository and merge them into your local branch
Conflict
When two changes edit the same part, you must fix it manually.
Check What’s Conflicted
git status
- Files marked “both modified” need to be fixed
Open The Conflicted File
<<<<<<< HEAD
your changes
=======
incoming changes
>>>>>>> origin/main
- <<<<<<< HEAD → your local changes
- ======= → separator
- >>>>>>> origin/main → changes from remote
Resolve A Git Conflict
nano
git checkout --theirs .
git checkout --ours .
- nano
- Resolve the conflict manually
- git checkout --theirs .
- Use the remote version
- git checkout --ours .
- Keep the local version
- Delete the conflict markers (<<<<<<<, =======, >>>>>>>) after editing manually
- In rebase, ours and theirs are swapped
After Fixing The File
git add .
git rebase --continue
git add .
git commit
- git rebase --continue
-
- Using sit status
- rebase in progress
- git commit
-
- Using git status
- You are in the middle of a merge
How To Confirm It’s Fully Done
git status
If you see:
- nothing to commit
- working tree clean
👉 That means the merge is complete and the conflict is over
Push To Remote
git push remote_name branch_name
git push -u origin main
git push
- git push -u origin main
- First time(Link the local branch to the remote branch)
- git push
- Second time
Overwrite
git push --force
git push --force-with-lease
git push -f
- git push --force
- Overwrites the remote branch
- git push --force-with-lease
- Overwrites only if the remote hasn’t changed
- git push -f
- -f = --force
How To Deploy (Step By Step)
First Deploy To GitHub
cd project
git init
git config --global user.name "Lanlanwi"
git config --global user.email "example@gmail.com"
git remote add origin URL
git branch -M main
git add .
git commit -m "first commit"
git pull origin main --rebase
git push -u origin main
Next Deployment
git add .
git commit -m "update"
git pull
git push
Change Remote URL And Deploy
git remote set-url origin URL
git add .
git commit -m "update"
git pull origin main --rebase
git push -u origin main
Force Push (Overwrite Remote)
git add .
git commit -m "update (force push)"
git push --force
Empty The Remote Repository
git reset
git commit --allow-empty -m "clear"
git push origin main --force