@@ -318,13 +318,230 @@ After committing, always:
318318 git show HEAD
319319 ```
320320
321+ ## Push to Remote
322+
323+ ### When to Push
324+
325+ After creating commits, automatically push to remote with these rules:
326+
327+ 1 . ** Automatic push for feature branches** :
328+ - Push immediately after commits on non-protected branches
329+ - Inform user what was pushed after completion
330+
331+ 2 . ** Ask permission ONLY for main/master** :
332+ ```
333+ You have commits ready to push to main/master branch.
334+ This is a protected branch. Please confirm:
335+ - Have all tests passed?
336+ - Is the code reviewed?
337+ - Are you ready to deploy?
338+
339+ Proceed with push to main/master? (yes/no)
340+ ```
341+
342+ 3 . ** Always check remote status first** :
343+ ``` bash
344+ # Check if branch exists on remote
345+ git branch -r | grep origin/[current-branch]
346+
347+ # Check if local is ahead/behind
348+ git status -sb
349+
350+ # Fetch latest without merging
351+ git fetch origin
352+ ```
353+
354+ ### Push Process
355+
356+ #### Step 1: Verify Branch and Remote
357+
358+ ``` bash
359+ # Check current branch
360+ BRANCH=$( git branch --show-current)
361+
362+ # Determine if it's a protected branch
363+ if [[ " $BRANCH " == " main" ]] || [[ " $BRANCH " == " master" ]]; then
364+ # Requires user confirmation (see rules above)
365+ PROTECTED=true
366+ else
367+ # Will push automatically
368+ PROTECTED=false
369+ fi
370+
371+ # Check tracking branch
372+ git branch -vv
373+
374+ # See commits that will be pushed
375+ git log origin/$BRANCH ..HEAD --oneline
376+ ```
377+
378+ #### Step 2: Inform User What Will Be Pushed
379+
380+ ``` bash
381+ # Show the commits that will be pushed
382+ echo " Pushing the following commits to origin/$BRANCH :"
383+ git log origin/$BRANCH ..HEAD --oneline
384+
385+ # Example output:
386+ # "Pushing the following commits to origin/feature/oauth:
387+ # abc1234 feat: add OAuth2 authentication
388+ # def5678 test: add auth tests
389+ # ghi9012 fix: handle token refresh"
390+ ```
391+
392+ #### Step 3: Handle Different Scenarios
393+
394+ ** New Branch (not on remote):**
395+ ``` bash
396+ echo " Creating new remote branch and pushing..."
397+ git push -u origin $BRANCH
398+ echo " ✓ Successfully pushed $BRANCH to origin"
399+ ```
400+
401+ ** Existing Branch (already tracking):**
402+ ``` bash
403+ echo " Pushing to origin/$BRANCH ..."
404+ git push
405+ echo " ✓ Successfully pushed updates to origin/$BRANCH "
406+ ```
407+
408+ ** Behind Remote (need to pull first):**
409+ ``` bash
410+ # Fetch and check
411+ git fetch origin
412+
413+ # Always use rebase when pulling to keep history clean
414+ echo " Branch is behind remote. Syncing with rebase..."
415+ git pull --rebase
416+ echo " Retrying push after rebase..."
417+ git push
418+ echo " ✓ Successfully pushed after rebasing on remote changes"
419+ ```
420+
421+ #### Step 4: Verify and Report Success
422+
423+ ``` bash
424+ # Confirm push completed
425+ git log origin/$BRANCH ..HEAD --oneline
426+
427+ # Report success to user
428+ echo " ✓ Push complete. Your branch is up to date with 'origin/$BRANCH '"
429+
430+ # For feature branches, suggest next steps
431+ if [[ " $PROTECTED " == " false" ]]; then
432+ echo " "
433+ echo " Next steps:"
434+ echo " - Create a pull request: gh pr create"
435+ echo " - View on GitHub: gh repo view --web"
436+ fi
437+ ```
438+
439+ ### Push Rules
440+
441+ 1 . ** Automatic push for feature branches** :
442+ ``` bash
443+ # For non-protected branches, push automatically after commits
444+ # Just inform the user what was pushed:
445+ " Pushing 3 commits to origin/feature/oauth-impl..."
446+ " ✓ Successfully pushed to origin/feature/oauth-impl"
447+ ```
448+
449+ 2 . ** Ask permission ONLY for protected branches** :
450+ ``` bash
451+ # Protected branches require confirmation
452+ if [[ " $BRANCH " == " main" ]] || [[ " $BRANCH " == " master" ]] || [[ " $BRANCH " == release/* ]]; then
453+ echo " Ready to push to $BRANCH (protected branch)"
454+ echo " Please confirm push to protected branch (yes/no):"
455+ # Wait for user confirmation
456+ else
457+ # All other branches push automatically
458+ echo " Pushing to origin/$BRANCH ..."
459+ git push
460+ fi
461+ ```
462+
463+ 3 . ** NEVER force push without explicit permission** :
464+ ``` bash
465+ # If force push is needed, always ask regardless of branch:
466+ " This requires a force push which will overwrite remote history.
467+ This can affect other developers. Are you sure you want to proceed?"
468+ ```
469+
470+ 4 . ** Always show what will be pushed** :
471+ ``` bash
472+ # Before pushing, show commits
473+ echo " Pushing the following commits to origin/$BRANCH :"
474+ git log origin/$BRANCH ..HEAD --oneline
475+ ```
476+
477+ 5 . ** Handle push rejections gracefully** :
478+ ``` bash
479+ # If push is rejected, automatically handle it
480+ echo " Push rejected. Syncing with remote using rebase..."
481+ git fetch origin
482+ git pull --rebase
483+
484+ # Retry push after rebase
485+ echo " Retrying push after rebase..."
486+ git push
487+
488+ # If still fails, it's likely branch protection
489+ if [ $? -ne 0 ]; then
490+ echo " Push still rejected. This is likely due to:"
491+ echo " - Branch protection rules"
492+ echo " - Insufficient permissions"
493+ echo " "
494+ echo " Creating a pull request instead..."
495+ gh pr create
496+ fi
497+ ```
498+
499+ ### Common Push Scenarios
500+
501+ ** Feature Branch Workflow:**
502+ ``` bash
503+ # After commits on feature branch, automatically push
504+ echo " Pushing feature branch to origin..."
505+ git push -u origin feature/oauth-implementation
506+ echo " ✓ Successfully pushed to origin/feature/oauth-implementation"
507+ echo " "
508+ echo " Pull request can be created with: gh pr create"
509+ ```
510+
511+ ** Hotfix Push:**
512+ ``` bash
513+ # For urgent fixes, push immediately
514+ echo " Pushing hotfix to origin..."
515+ git push origin hotfix/critical-security-fix
516+ echo " ✓ Hotfix pushed successfully"
517+ echo " "
518+ echo " Creating urgent PR for review..."
519+ gh pr create --title " HOTFIX: Critical security fix" --label " urgent,hotfix"
520+ ```
521+
522+ ** Release Branch:**
523+ ``` bash
524+ # Release branches are treated like protected branches
525+ echo " Ready to push to release branch"
526+ echo " Please confirm the following are complete:"
527+ echo " ✓ All tests pass"
528+ echo " ✓ Version numbers updated"
529+ echo " ✓ Changelog updated"
530+ echo " "
531+ echo " Proceed with push to release branch? (yes/no)"
532+ # Only release and main/master branches ask for confirmation
533+ ```
534+
321535## Best Practices
322536
3235371 . ** Atomic commits** : Each commit should work independently
3245382 . ** Clear messages** : Future developers should understand why
3255393 . ** Group related changes** : But don't mix unrelated changes
3265404 . ** Test before committing** : Ensure code works
3275415 . ** Review diff carefully** : Check for debug code, comments, secrets
542+ 6 . ** Always rebase when syncing** : Use ` git pull --rebase ` to keep history clean
543+ 7 . ** Automatic push for feature branches** : No confirmation needed, just inform user
544+ 8 . ** Respect branch protection** : Only ask confirmation for main/master/release branches
328545
329546## Quick Reference
330547
0 commit comments