|
| 1 | +--- |
| 2 | +title: "Git and Github" |
| 3 | +date: "2026-02-09" |
| 4 | +draft: false |
| 5 | +showToc: true |
| 6 | +Author: "Muhammad Ramzan" |
| 7 | +tags: ["git", "github", "version control", "collaboration"] |
| 8 | +description: "A comprehensive guide to using Git and GitHub for version control and collaboration in software development." |
| 9 | +--- |
| 10 | + |
| 11 | +# Git & GitHub: A Quick Guide |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Introduction |
| 16 | + |
| 17 | +Git is a version control system that tracks changes in code and helps developers collaborate efficiently. GitHub is an online platform that hosts Git repositories, making it easier to share code, manage projects, and work on open-source projects. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Getting Started |
| 22 | + |
| 23 | +### Initial Configuration |
| 24 | + |
| 25 | +```bash |
| 26 | +git config --global user.name "your-name" |
| 27 | +git config --global user.email "your-email" |
| 28 | +git config --global core.editor "code" # Set default editor |
| 29 | +git config --list # View all configurations |
| 30 | +``` |
| 31 | + |
| 32 | +### Creating Repositories |
| 33 | + |
| 34 | +```bash |
| 35 | +git init # Initialize a new repository |
| 36 | +git clone <repo-url> # Clone an existing repository |
| 37 | +git clone <repo-url> <folder-name> # Clone into specific folder |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Basic Workflow |
| 43 | + |
| 44 | +### Staging Changes |
| 45 | + |
| 46 | +```bash |
| 47 | +git status # Check current status |
| 48 | +git add <file> # Stage a specific file |
| 49 | +git add . # Stage all files |
| 50 | +git add *.js # Stage all JavaScript files |
| 51 | +git rm <file> # Remove file and stage deletion |
| 52 | +git mv <old-name> <new-name> # Rename file and stage change |
| 53 | +``` |
| 54 | + |
| 55 | +### Committing Changes |
| 56 | + |
| 57 | +```bash |
| 58 | +git commit -m "Describe your changes here" |
| 59 | +git commit -am "Add and commit tracked files" |
| 60 | +git commit --amend -m "Update last commit message" |
| 61 | +git commit --amend --no-edit # Add staged changes to last commit |
| 62 | +``` |
| 63 | + |
| 64 | +### Viewing History |
| 65 | + |
| 66 | +```bash |
| 67 | +git log # View commit history |
| 68 | +git log --oneline # Compact commit history |
| 69 | +git log --graph --oneline --all # Visual branch history |
| 70 | +git log -p # Show changes in each commit |
| 71 | +git log -n 5 # Show last 5 commits |
| 72 | +git show <commit-hash> # Show specific commit details |
| 73 | +git diff # Show unstaged changes |
| 74 | +git diff --staged # Show staged changes |
| 75 | +git diff <branch1> <branch2> # Compare branches |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Branching & Merging |
| 81 | + |
| 82 | +### Managing Branches |
| 83 | + |
| 84 | +```bash |
| 85 | +git branch # List all local branches |
| 86 | +git branch -a # List all branches (local and remote) |
| 87 | +git branch <branch-name> # Create a new branch |
| 88 | +git checkout <branch-name> # Switch to a branch |
| 89 | +git checkout -b <branch-name> # Create and switch to new branch |
| 90 | +git switch <branch-name> # Modern way to switch branches |
| 91 | +git switch -c <branch-name> # Create and switch to new branch |
| 92 | +git branch -d <branch-name> # Delete branch (safe) |
| 93 | +git branch -D <branch-name> # Force delete branch |
| 94 | +git branch -m <new-name> # Rename current branch |
| 95 | +``` |
| 96 | + |
| 97 | +### Merging |
| 98 | + |
| 99 | +```bash |
| 100 | +git merge <branch-name> # Merge branch into current branch |
| 101 | +git merge --no-ff <branch-name> # Merge with merge commit |
| 102 | +git merge --squash <branch-name> # Squash commits before merging |
| 103 | +git merge --abort # Abort merge in case of conflicts |
| 104 | +``` |
| 105 | + |
| 106 | +### Rebasing |
| 107 | + |
| 108 | +```bash |
| 109 | +git rebase <branch-name> # Rebase current branch |
| 110 | +git rebase -i HEAD~3 # Interactive rebase last 3 commits |
| 111 | +git rebase --continue # Continue after resolving conflicts |
| 112 | +git rebase --abort # Abort rebase operation |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Working with Remotes |
| 118 | + |
| 119 | +### Remote Management |
| 120 | + |
| 121 | +```bash |
| 122 | +git remote # List remote connections |
| 123 | +git remote -v # List remotes with URLs |
| 124 | +git remote add origin <repo-url> # Add remote repository |
| 125 | +git remote set-url origin <new-url> # Change remote URL |
| 126 | +git remote rename <old-name> <new-name> # Rename remote |
| 127 | +git remote remove <remote-name> # Remove remote |
| 128 | +git remote show origin # Show remote details |
| 129 | +``` |
| 130 | + |
| 131 | +### Syncing with Remotes |
| 132 | + |
| 133 | +```bash |
| 134 | +git fetch # Download remote changes (no merge) |
| 135 | +git fetch origin # Fetch from specific remote |
| 136 | +git fetch --all # Fetch from all remotes |
| 137 | +git pull # Fetch and merge remote changes |
| 138 | +git pull --rebase # Fetch and rebase instead of merge |
| 139 | +git push # Push changes to remote |
| 140 | +git push -u origin <branch-name> # Push and set upstream |
| 141 | +git push origin --delete <branch-name> # Delete remote branch |
| 142 | +git push --force # Force push (use with caution!) |
| 143 | +git push --force-with-lease # Safer force push |
| 144 | +git push --tags # Push all tags to remote |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Undoing Changes |
| 150 | + |
| 151 | +### Working Directory & Staging |
| 152 | + |
| 153 | +```bash |
| 154 | +git checkout -- <file> # Discard changes in working directory |
| 155 | +git restore <file> # Modern way to discard changes |
| 156 | +git restore --staged <file> # Unstage file |
| 157 | +git reset <file> # Unstage file (keep changes) |
| 158 | +git reset --hard # Discard all local changes |
| 159 | +git clean -fd # Remove untracked files and directories |
| 160 | +git clean -n # Preview files to be removed |
| 161 | +``` |
| 162 | + |
| 163 | +### Commits |
| 164 | + |
| 165 | +```bash |
| 166 | +git reset --soft HEAD~1 # Undo last commit (keep changes staged) |
| 167 | +git reset --mixed HEAD~1 # Undo last commit (keep changes unstaged) |
| 168 | +git reset --hard HEAD~1 # Undo last commit (discard changes) |
| 169 | +git reset --hard <commit-hash> # Reset to specific commit |
| 170 | +git revert <commit-hash> # Create new commit that undoes changes |
| 171 | +git revert HEAD # Revert last commit |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## Stashing |
| 177 | + |
| 178 | +### Temporary Storage |
| 179 | + |
| 180 | +```bash |
| 181 | +git stash # Stash current changes |
| 182 | +git stash save "message" # Stash with description |
| 183 | +git stash list # List all stashes |
| 184 | +git stash show # Show latest stash changes |
| 185 | +git stash show stash@{0} # Show specific stash |
| 186 | +git stash apply # Apply latest stash (keep in list) |
| 187 | +git stash apply stash@{2} # Apply specific stash |
| 188 | +git stash pop # Apply latest stash and remove from list |
| 189 | +git stash drop # Delete latest stash |
| 190 | +git stash drop stash@{1} # Delete specific stash |
| 191 | +git stash clear # Delete all stashes |
| 192 | +git stash branch <branch-name> # Create branch from stash |
| 193 | +``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +## Tagging |
| 198 | + |
| 199 | +### Version Marking |
| 200 | + |
| 201 | +```bash |
| 202 | +git tag # List all tags |
| 203 | +git tag <tag-name> # Create lightweight tag |
| 204 | +git tag -a v1.0 -m "Version 1.0" # Create annotated tag |
| 205 | +git tag -a v1.0 <commit-hash> -m "Tag old commit" # Tag specific commit |
| 206 | +git show <tag-name> # Show tag details |
| 207 | +git push origin <tag-name> # Push specific tag |
| 208 | +git push origin --tags # Push all tags |
| 209 | +git tag -d <tag-name> # Delete local tag |
| 210 | +git push origin --delete <tag-name> # Delete remote tag |
| 211 | +``` |
| 212 | + |
| 213 | +--- |
| 214 | + |
| 215 | +## Advanced Features |
| 216 | + |
| 217 | +### Submodules |
| 218 | + |
| 219 | +```bash |
| 220 | +git submodule add <repo-url> <path> # Add submodule |
| 221 | +git submodule init # Initialize submodules |
| 222 | +git submodule update # Update submodules |
| 223 | +git submodule update --init --recursive # Initialize and update all |
| 224 | +git submodule foreach git pull origin main # Update all submodules |
| 225 | +git submodule deinit <path> # Deinitialize submodule |
| 226 | +git rm <submodule-path> # Remove submodule |
| 227 | +``` |
| 228 | + |
| 229 | +### Cherry-Picking |
| 230 | + |
| 231 | +```bash |
| 232 | +git cherry-pick <commit-hash> # Apply specific commit to current branch |
| 233 | +git cherry-pick <hash1> <hash2> # Apply multiple commits |
| 234 | +git cherry-pick --continue # Continue after resolving conflicts |
| 235 | +git cherry-pick --abort # Abort cherry-pick operation |
| 236 | +``` |
| 237 | + |
| 238 | +### Searching & Finding |
| 239 | + |
| 240 | +```bash |
| 241 | +git grep "search-term" # Search in working directory |
| 242 | +git grep "search-term" <branch-name> # Search in specific branch |
| 243 | +git log -S "search-term" # Find commits with specific content |
| 244 | +git log --grep="pattern" # Search commit messages |
| 245 | +git bisect start # Start binary search for bug |
| 246 | +git bisect bad # Mark current commit as bad |
| 247 | +git bisect good <commit-hash> # Mark commit as good |
| 248 | +git bisect reset # End bisect session |
| 249 | +git blame <file> # Show who changed each line |
| 250 | +git reflog # Show reference logs (recover lost commits) |
| 251 | +``` |
| 252 | + |
| 253 | +### Aliases |
| 254 | + |
| 255 | +```bash |
| 256 | +git config --global alias.co checkout # Create alias |
| 257 | +git config --global alias.br branch |
| 258 | +git config --global alias.st status |
| 259 | +git config --global alias.unstage 'reset HEAD --' |
| 260 | +git config --global alias.last 'log -1 HEAD' |
| 261 | +git config --global alias.visual 'log --graph --oneline --all' |
| 262 | +``` |
| 263 | + |
| 264 | +--- |
| 265 | + |
| 266 | +## GitHub-Specific Features |
| 267 | + |
| 268 | +### Pull Requests & Collaboration |
| 269 | + |
| 270 | +```bash |
| 271 | +# Fetch PR for local testing |
| 272 | +git fetch origin pull/123/head:pr-123 |
| 273 | +git checkout pr-123 |
| 274 | + |
| 275 | +# Working with forks |
| 276 | +git remote add upstream <original-repo-url> |
| 277 | +git fetch upstream |
| 278 | +git merge upstream/main |
| 279 | +``` |
| 280 | + |
| 281 | +### GitHub CLI (gh) |
| 282 | + |
| 283 | +```bash |
| 284 | +gh repo clone <repo-name> # Clone repository |
| 285 | +gh pr create # Create pull request |
| 286 | +gh pr list # List pull requests |
| 287 | +gh pr checkout <pr-number> # Checkout pull request |
| 288 | +gh issue create # Create issue |
| 289 | +gh issue list # List issues |
| 290 | +``` |
| 291 | + |
| 292 | +--- |
| 293 | + |
| 294 | +## Practical Example: Push a Project to GitHub |
| 295 | + |
| 296 | +1. **Configure Git:** |
| 297 | +```bash |
| 298 | +git config --global user.name "your-name" |
| 299 | +git config --global user.email "your-email" |
| 300 | +``` |
| 301 | + |
| 302 | +2. **Initialize repository:** |
| 303 | +```bash |
| 304 | +git init |
| 305 | +``` |
| 306 | + |
| 307 | +3. **Link to GitHub:** |
| 308 | +```bash |
| 309 | +git remote add origin <repo-url> |
| 310 | +``` |
| 311 | + |
| 312 | +4. **Stage and commit:** |
| 313 | +```bash |
| 314 | +git add . |
| 315 | +git commit -m "Initial commit" |
| 316 | +``` |
| 317 | + |
| 318 | +5. **Push to GitHub:** |
| 319 | +```bash |
| 320 | +git push -u origin main |
| 321 | +``` |
| 322 | + |
| 323 | +Your project is now live on GitHub! |
| 324 | + |
| 325 | +--- |
| 326 | + |
| 327 | +## Troubleshooting |
| 328 | + |
| 329 | +### Common Issues |
| 330 | + |
| 331 | +```bash |
| 332 | +# Undo accidental commit to wrong branch |
| 333 | +git reset --soft HEAD~1 |
| 334 | +git stash |
| 335 | +git checkout correct-branch |
| 336 | +git stash pop |
| 337 | +git commit -m "message" |
| 338 | + |
| 339 | +# Fix merge conflicts |
| 340 | +git status # Identify conflicted files |
| 341 | +# Edit files to resolve conflicts |
| 342 | +git add <resolved-files> |
| 343 | +git commit |
| 344 | + |
| 345 | +# Sync fork with upstream |
| 346 | +git fetch upstream |
| 347 | +git checkout main |
| 348 | +git merge upstream/main |
| 349 | +git push origin main |
| 350 | + |
| 351 | +# Recover deleted branch |
| 352 | +git reflog |
| 353 | +git checkout -b <branch-name> <commit-hash> |
| 354 | +``` |
| 355 | + |
| 356 | +--- |
| 357 | + |
| 358 | +## Best Practices |
| 359 | + |
| 360 | +- **Commit often** with clear, descriptive messages |
| 361 | +- **Pull before push** to avoid conflicts |
| 362 | +- **Use branches** for features and bug fixes |
| 363 | +- **Never force push** to shared branches |
| 364 | +- **Review changes** before committing (`git diff`) |
| 365 | +- **Keep commits atomic** (one logical change per commit) |
| 366 | +- **Write meaningful commit messages** (what and why, not how) |
| 367 | +- **Use `.gitignore`** to exclude unnecessary files |
| 368 | +- **Tag releases** for version management |
| 369 | +- **Protect main branch** on GitHub |
| 370 | + |
| 371 | +--- |
0 commit comments