|
| 1 | +# 🧠 Git Commands Cheatsheet (with Inline Comments) |
| 2 | + |
| 3 | +## 🔧 Configuration |
| 4 | + |
| 5 | +```bash |
| 6 | +git --help # Show overview of Git commands |
| 7 | +git config --global user.name "Simeon" # Set your Git username globally |
| 8 | +git config --global user.email "[email protected]" # Set your Git email globally |
| 9 | +git config list --global # List global Git configuration settings |
| 10 | +git config --global core.editor "code --wait" # Set VS Code as default Git editor |
| 11 | +``` |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 🔑 Credential Manager |
| 16 | + |
| 17 | +```bash |
| 18 | +git credential-manager github list # List GitHub credentials currently used |
| 19 | +git credential-manager github login # Authenticate and store GitHub credentials |
| 20 | +``` |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## 📁 Repositories |
| 25 | + |
| 26 | +```bash |
| 27 | +git init # Initialize a new Git repository in the current folder |
| 28 | +git init -b main # Initialize with 'main' as the default branch |
| 29 | + |
| 30 | +git clone <repository_url> # Clone an existing repository |
| 31 | +git clone <repository_url> <new_dir> # Clone and rename the directory |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## 📝 Tracking Files |
| 37 | + |
| 38 | +```bash |
| 39 | +git status # Show current changes and staging info |
| 40 | +touch README.md # Create a new file named README.md |
| 41 | +git add <file_name> # Stage a specific file |
| 42 | +git add -A # Stage all modified and new files |
| 43 | +``` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## ✅ Committing |
| 48 | + |
| 49 | +```bash |
| 50 | +git commit -m "Your message" # Commit staged changes with a short message |
| 51 | +git commit # Open editor to write a full commit message |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## 🌐 Remotes |
| 57 | + |
| 58 | +```bash |
| 59 | +git remote -v # View remote repository URLs |
| 60 | +git remote add origin <url> # Add a remote named 'origin' |
| 61 | +git remote show origin # Show detailed info about 'origin' |
| 62 | +git remote add upstream <url> # Add a remote for upstream (main project repo) |
| 63 | +git remote rm origin # Remove a remote called 'origin' |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## 🔁 Syncing Changes |
| 69 | + |
| 70 | +```bash |
| 71 | +git fetch origin # Download latest changes from 'origin' (no merge) |
| 72 | +git pull origin <branch> # Fetch + merge updates from a remote branch |
| 73 | +git pull --no-rebase origin <branch> # Fetch + merge without rebasing to preserve the commit history |
| 74 | +git pull upstream main # Pull latest changes from upstream main branch |
| 75 | +git push origin <branch> # Push your local branch to 'origin' |
| 76 | +git push -u origin main # Push and set upstream tracking for main |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## 🌿 Branching |
| 82 | + |
| 83 | +`git branch` performs branch admin tasks (eg. list, delete) |
| 84 | +`git switch` switches your current working directory to different branches |
| 85 | + |
| 86 | +Git provides multiple ways to perform similar actions, and your choice of command strategy will end up being a mix of personal preference and standard practices in your team. |
| 87 | + |
| 88 | +```bash |
| 89 | +git branch -v # List all local branches with last commit |
| 90 | +git branch -a # List all branches (local + remote) |
| 91 | +git branch <branch_name> # Create a new branch from the current commit |
| 92 | +git branch -d <branch_name> # Delete a local branch |
| 93 | + |
| 94 | +git switch <branch_name> # Switch to an existing branch |
| 95 | +git switch -c <branch_name> # Create a new branch from the current commit and switch to it |
| 96 | +git switch -c <branch_name> <commit_id> # Create a new branch from a specified commit and switch to it |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 🔄 Merging & Conflicts |
| 102 | + |
| 103 | +```bash |
| 104 | +git merge <branch_name> # Merge specified branch into current one |
| 105 | +# If conflicts occur: |
| 106 | +# <<<<<<<, =======, >>>>>>> markers will show in affected files |
| 107 | +# Manually edit and fix conflicts, then: |
| 108 | +git add <file_name> # Stage the resolved file |
| 109 | +git commit # Commit the merge resolution |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 🚀 Branching Workflow |
| 115 | + |
| 116 | +```bash |
| 117 | +git switch -c feature-branch # Create and switch to a new branch |
| 118 | +# Make code changes |
| 119 | +git add -A # Stage all new and modified files |
| 120 | +git commit -m "Feature updates" # Commit with a clear message |
| 121 | +git push origin feature-branch # Push new branch to remote |
| 122 | +``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## 📤 Uploading Changes to GitHub |
| 127 | + |
| 128 | +**Initial Setup:** |
| 129 | +```bash |
| 130 | +# Fork the repo on GitHub |
| 131 | +git clone <repo_url> # Clone forked repo to local |
| 132 | +``` |
| 133 | + |
| 134 | +**Making Changes:** |
| 135 | +```bash |
| 136 | +git switch -c <new_branch> # Create and switch to a feature branch |
| 137 | +# Edit files |
| 138 | +git status # Check what's modified |
| 139 | +git add -A # Stage all changes |
| 140 | +git commit # Commit changes |
| 141 | +git push origin <branch> # Push feature branch to GitHub |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## ❌ Undoing Mistakes |
| 147 | + |
| 148 | +### 🔄 Accidental PR Merge |
| 149 | + |
| 150 | +> On GitHub: |
| 151 | +1. Click **"Revert"** on the merged PR |
| 152 | +2. Merge the auto-created `revert-1-<branch>` PR |
| 153 | +3. Click "Revert" again on the revert-PR |
| 154 | +4. Leave the final "revert of a revert" PR open as the actual submission |
| 155 | + |
| 156 | + |
| 157 | +### ⛔ Undoing an accidental push to GitHub |
| 158 | + |
| 159 | +**⚠️ The commands shown here should be used with caution.** |
| 160 | + |
| 161 | +#### Important caveats |
| 162 | +* Generally, anything uploaded to the internet should be considered to have been made public permanently. |
| 163 | + * If you accidentally uploaded a password or other credentials, those should be revoked immediately. |
| 164 | +* It is considered bad practice to change Git history. These commands should only be used in very specific circumstances. |
| 165 | + |
| 166 | +#### Steps |
| 167 | +* Optionally, backup your current latest commit that was accidentally pushed |
| 168 | + * `git switch -c backup-branch` |
| 169 | +* Ensure your local branch is at the correct commit that should be uploaded |
| 170 | + * Use `git log` to find the commit ID you want to revert a branch to |
| 171 | +* Revert your local branch to the correct commit |
| 172 | + * `git switch -C [branch name] [commit id]` to overwrite the branch, and set it to the specified commit ID |
| 173 | +* Overwrite the GitHub repository with the updated branch |
| 174 | + * `git push origin [branch name] --force` |
0 commit comments