Wednesday, 16 July 2025

Git Cheat Sheet

TKIET LINUX USER GROUP                                                            Harshvardhan Patil

✅ Git Cheat Sheet — Step-by-Step Guide for GitHub


📂 Phase 1: Pushing a File to GitHub (First Time)

1️⃣ Initialize Git in Your Project Folder

git init

Sets up a new Git repository.


2️⃣ Link to Your GitHub Repository

git remote add origin https://github.com/username/repo.git

Connects your local repo to GitHub.


3️⃣ Stage Files for Commit

git add demo.txt

or to add all files:

git add .

4️⃣ Commit the Files

git commit -m "Initial commit: Added demo.txt"

Saves changes in Git history.


5️⃣ Push Files to GitHub

git push -u origin main

Replace main with your branch name, e.g., master if needed.


📝 Phase 2: Modify & Push Updates

1️⃣ Edit demo.txt

2️⃣ Check Status

git status

3️⃣ Stage Updated File

git add demo.txt

4️⃣ Commit the Changes

git commit -m "Updated demo.txt content"

5️⃣ Push Changes to GitHub

git push

🎯 Bonus Git Commands

🔹 Check Commit History

git log

🔹 Clone an Existing Repository

git clone https://github.com/username/repo.git

🔹 List Files in Folder

ls

🛠️ Configure Git (One Time Setup)


git config --global user.name "Your Name" git config --global user.email "you@example.com"

🔄 Restore a File from a Previous Commit

1️⃣ Check Commit History

git log

2️⃣ Restore a Specific File

git checkout <commit-hash> -- demo.txt

Restores demo.txt from specific commit.

3️⃣ View File from Old Commit Without Changing Anything

git show <commit-hash>:demo.txt



🌿 Git Branching: Create, Switch, Merge

ActionCommand
Create a branchgit branch feature-branch
Switch to a branchgit checkout feature-branch or git switch feature-branch
Create & Switchgit checkout -b feature-branch or git switch -c feature-branch
List branchesgit branch
Merge branch to maingit merge feature-branch
Delete a branchgit branch -d feature-branch
Force delete a branchgit branch -D feature-branch
Push branch to GitHubgit push origin feature-branch
Delete branch from GitHubgit push origin --delete feature-branch

📝 Example Workflow of Branching

1️⃣ Create & Switch to Branch

git switch -c feature-branch

2️⃣ Make Changes, Add & Commit

git add . git commit -m "Added new feature"

3️⃣ Switch Back & Merge

git switch main
git merge feature-branch

4️⃣ Delete Branch

git branch -d feature-branch



No comments:

Post a Comment

Git Cheat Sheet

TKIET LINUX USER GROUP                                                                          Harshvardhan Patil ✅ Git Cheat Sheet — Step-...