oxedyne/fe2o3/dev/git_cheatsheet.md
3.5 KiB, 1 run
created by r1870400018:4028, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | # Git Workflow Cheat Sheet |
| 2 | |
| 3 | ## 1. Branch Management |
| 4 | |
| 5 | ### Check Current Status |
| 6 | ```bash |
| 7 | git status # Show changed files |
| 8 | git branch # List branches (* shows current) |
| 9 | git branch --show-current # Just show current branch name |
| 10 | ``` |
| 11 | |
| 12 | ### Creating & Switching Branches |
| 13 | ```bash |
| 14 | git checkout -b new-branch # Create and switch to new branch |
| 15 | git checkout existing-branch # Switch to existing branch |
| 16 | ``` |
| 17 | |
| 18 | ### Stashing Changes |
| 19 | ```bash |
| 20 | git stash # Save current changes |
| 21 | git stash list # Show all stashes |
| 22 | git stash pop # Apply and remove most recent stash |
| 23 | git stash apply stash@{1} # Apply specific stash without removing |
| 24 | git stash drop stash@{1} # Remove specific stash |
| 25 | ``` |
| 26 | |
| 27 | ## 2. Working with Changes |
| 28 | |
| 29 | ### Basic Workflow |
| 30 | ```bash |
| 31 | git status # Check what's changed |
| 32 | git add specific-file.txt # Stage specific file |
| 33 | git add . # Stage all changes |
| 34 | git commit -m "Message" # Commit with message |
| 35 | ``` |
| 36 | |
| 37 | ### Selective Committing |
| 38 | ```bash |
| 39 | git add -p # Interactively choose parts to stage |
| 40 | git restore --staged file.txt # Unstage a file |
| 41 | git diff # Show unstaged changes |
| 42 | git diff --staged # Show staged changes |
| 43 | ``` |
| 44 | |
| 45 | ### Fixing Mistakes |
| 46 | ```bash |
| 47 | git reset --soft HEAD^ # Undo last commit, keep changes staged |
| 48 | git reset --hard HEAD^ # Undo last commit, discard changes |
| 49 | git restore file.txt # Discard changes in file |
| 50 | ``` |
| 51 | |
| 52 | ## 3. Feature Branch Workflow |
| 53 | |
| 54 | ### Start New Feature |
| 55 | ```bash |
| 56 | git checkout main |
| 57 | git pull # Get latest main |
| 58 | git checkout -b feature-name # Create feature branch |
| 59 | ``` |
| 60 | |
| 61 | ### Work on Feature |
| 62 | ```bash |
| 63 | # Make changes |
| 64 | git add . |
| 65 | git commit -m "Feature progress" |
| 66 | git push -u origin feature-name # First push |
| 67 | git push # Subsequent pushes |
| 68 | ``` |
| 69 | |
| 70 | ### Finish Feature |
| 71 | ```bash |
| 72 | git checkout main |
| 73 | git pull # Get any new changes |
| 74 | git merge feature-name # Merge feature into main |
| 75 | git push # Push merged changes |
| 76 | git branch -d feature-name # Delete local branch |
| 77 | git push origin -d feature-name # Delete remote branch |
| 78 | ``` |
| 79 | |
| 80 | ## 4. Common Scenarios |
| 81 | |
| 82 | ### Save Work in Progress |
| 83 | ```bash |
| 84 | git diff > my_changes.patch # Backup changes to file |
| 85 | git stash # Or use stash |
| 86 | ``` |
| 87 | |
| 88 | ### Handle Merge Conflicts |
| 89 | ```bash |
| 90 | git status # See conflicted files |
| 91 | # Edit files to resolve conflicts |
| 92 | git add resolved-file.txt |
| 93 | git commit # Complete the merge |
| 94 | ``` |
| 95 | |
| 96 | ### Update Feature Branch with Main |
| 97 | ```bash |
| 98 | git checkout feature-branch |
| 99 | git merge main # Get changes from main |
| 100 | ``` |
| 101 | |
| 102 | ## 5. Useful Commands |
| 103 | |
| 104 | ### View History |
| 105 | ```bash |
| 106 | git log # Show commit history |
| 107 | git log --oneline # Compact history view |
| 108 | git show commit-hash # Show specific commit details |
| 109 | ``` |
| 110 | |
| 111 | ### Remote Operations |
| 112 | ```bash |
| 113 | git remote -v # Show remote repositories |
| 114 | git fetch # Get remote changes without merging |
| 115 | git pull # Get and merge remote changes |
| 116 | ``` |
| 117 | |
| 118 | ### Configuration |
| 119 | ```bash |
| 120 | git config --global user.name "Your Name" |
| 121 | git config --global user.email "your.email@example.com" |
| 122 | ``` |
| 123 | |
| 124 | ## 6. Tips |
| 125 | |
| 126 | - Always check which branch you're on before making changes |
| 127 | - Commit frequently with clear messages |
| 128 | - Pull from main regularly to stay up to date |
| 129 | - Use feature branches for new work |
| 130 | - Backup important changes before risky operations |
| 131 | - Add generated files and build artifacts to .gitignore |