└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Ultimate Git Guide: Master Version Control πŸš€ 2 | 3 | A comprehensive guide to Git commands with detailed notes for better understanding. Use this guide to master Git like a pro! πŸ› οΈ 4 | 5 | --- 6 | 7 | ## 1. Set Up Git πŸ‘€ 8 | 9 | Configure your identity for commits: 10 | 11 | | **Description** | **Command** | 12 | |-------------------------------------|-------------------------------------------------| 13 | | Set your username | `git config --global user.name ""` | 14 | | Set your email address | `git config --global user.email `| 15 | 16 | **Notes**: 17 | - Replace `` and `` with your actual name and email. 18 | - These settings are global and apply to all repositories on your machine. 19 | - Use `git config --list` to view all your Git configurations. 20 | - To set configurations for a specific repository, omit the `--global` flag. 21 | - Always configure your identity before making commits to ensure proper attribution. 22 | 23 | --- 24 | 25 | ## 2. Working with Commits πŸ’Ύ 26 | 27 | Stage and save changes to your repository: 28 | 29 | | **Description** | **Command** | 30 | |-------------------------------------|-------------------------------------------------| 31 | | Stage a specific file | `git add ` πŸ“‚ | 32 | | Stage all files | `git add .` πŸ“‚ | 33 | | Commit changes with a message | `git commit -m "MSG"` πŸ’Ύ | 34 | | Initialize a new Git repository | `git init` πŸ†• | 35 | 36 | **Notes**: 37 | - Use `git add ` to stage specific files for the next commit. 38 | - Use `git add .` to stage all changes in the current directory and subdirectories. 39 | - Always write clear and descriptive commit messages. For example: `git commit -m "Add login feature"`. 40 | - Use `git init` to create a new Git repository in the current directory. This creates a hidden `.git` folder. 41 | - Commits are snapshots of your project at a specific point in time. Make frequent, small commits for better tracking. 42 | 43 | --- 44 | 45 | ## 3. Check Status & History πŸ“œ 46 | 47 | Monitor the state of your repository and view commit history: 48 | 49 | | **Description** | **Command** | 50 | |-------------------------------------|-------------------------------------------------| 51 | | Check the status of files | `git status` πŸ“Š | 52 | | View the commit history | `git log` πŸ“œ | 53 | 54 | **Notes**: 55 | - `git status` shows the current state of your working directory, including untracked, modified, and staged files. 56 | - Use `git log --oneline` for a compact view of the commit history. 57 | - Use `git log --graph` to visualize the commit history with branches. 58 | - Use `git log -p` to see the changes made in each commit. 59 | - Use `git log --author=""` to filter commits by a specific author. 60 | 61 | --- 62 | 63 | ## 4. Undo Changes πŸ”„ 64 | 65 | Revert or reset changes in your repository: 66 | 67 | | **Description** | **Command** | 68 | |-------------------------------------|-------------------------------------------------| 69 | | Remove the last N commits | `git reset (--hard || --soft) HEAD~N` | 70 | | Revert to a specific commit | `git reset ` βͺ | 71 | | View all actions (including resets) | `git reflog` πŸ•’ | 72 | | Reset to a specific commit | `git reset --hard ` πŸ”„ | 73 | 74 | **Notes**: 75 | - `--soft` keeps changes in the staging area, while `--hard` discards all changes. 76 | - Use `git reflog` to recover lost commits or branches. It shows a history of all actions, including resets. 77 | - Be cautious with `git reset --hard` as it permanently discards changes. 78 | - Use `git revert ` to create a new commit that undoes the changes of a specific commit. 79 | - Use `git checkout ` to temporarily switch to a specific commit. 80 | 81 | --- 82 | 83 | ## 5. Branching 🌿 84 | 85 | Create, manage, and switch between branches: 86 | 87 | ### Types of Branches: 88 | - **Local branch** 🏠: Exists in your local repository. 89 | - **Remote branch** 🌐: Exists in the remote repository. 90 | - **Remote tracking branch** πŸ”„: A copy of the remote branch used for merging. 91 | - **Local tracking branch** πŸ“‘: Local branches that track remote branches. 92 | 93 | ### Branch Commands: 94 | 95 | | **Description** | **Command** | 96 | |-------------------------------------------|--------------------------------------------------| 97 | | Show local branches only | `git branch` 🌿 | 98 | | Create a new branch | `git branch ` 🌿 | 99 | | Create and switch to a new branch | `git checkout -b ` 🌿 | 100 | | Switch to the specified branch | `git checkout ` 🌿 | 101 | | Switch to another branch | `git switch ` πŸ”„ | 102 | | Create and switch using switch command | `git switch -c ` πŸ”„ | 103 | | List all branches (local & remote) | `git branch -a` 🌿 | 104 | | List remote branches only | `git branch -r` 🌿 | 105 | | Push a branch to remote | `git push origin ` 🌐 | 106 | | Delete a branch (safe) | `git branch -d ` πŸ—‘οΈ | 107 | | Force delete a branch | `git branch -D ` πŸ—‘οΈ | 108 | | Delete a remote branch | `git push origin --delete ` πŸ—‘οΈ | 109 | | Rename a branch | `git branch -M ` ✏️ | 110 | 111 | **Notes**: 112 | - Use `git branch -d` to delete a branch only if it has been merged. 113 | - Use `git branch -D` to force delete an unmerged branch. 114 | - Use `git push origin --delete ` to delete a branch from the remote repository. 115 | - Use `git branch -M` to rename a branch. This is useful if you made a typo in the branch name. 116 | - Always create branches for new features or bug fixes to keep the `main` branch stable. 117 | 118 | --- 119 | 120 | ## 6. Remote Repositories 🌐 121 | 122 | Connect and interact with remote repositories: 123 | 124 | | **Description** | **Command** | 125 | |-------------------------------------|-------------------------------------------------| 126 | | Connect local repo to remote | `git remote add origin ` | 127 | | Push changes and set up tracking | `git push -u origin main` 🌐 | 128 | 129 | **Notes**: 130 | - Replace `` with the URL of your remote repository. 131 | - The `-u` flag sets up tracking between the local and remote branches. 132 | - Use `git remote -v` to view the list of remote repositories. 133 | - Use `git remote remove origin` to disconnect from a remote repository. 134 | - Use `git fetch` to download changes from the remote repository without merging them. 135 | 136 | --- 137 | 138 | ## 7. Rebase & Cherry-pick πŸ”„ 139 | 140 | Rewrite commit history and apply specific changes: 141 | 142 | | **Description** | **Command** | 143 | |-------------------------------------|-------------------------------------------------| 144 | | Rebase current branch | `git rebase ` πŸ”„ | 145 | | Apply changes from a specific commit| `git cherry-pick ` πŸ’ | 146 | 147 | **Notes**: 148 | - Use `git rebase` to maintain a clean commit history. It moves the entire branch to a new base commit. 149 | - Use `git cherry-pick` to apply specific commits to another branch. This is useful for picking bug fixes from one branch to another. 150 | - Be cautious with `git rebase` as it rewrites commit history, which can cause conflicts. 151 | - Use `git rebase --abort` to cancel an ongoing rebase. 152 | 153 | --- 154 | 155 | ## 8. Stashing Changes πŸ“¦ 156 | 157 | Temporarily save and restore changes: 158 | 159 | | **Description** | **Command** | 160 | |-------------------------------------|-------------------------------------------------| 161 | | Temporarily save changes | `git stash` πŸ“¦ | 162 | | Restore saved changes | `git stash apply` πŸ“¦ | 163 | | List all stashed changes | `git stash list` πŸ“‹ | 164 | | Restore a specific stash | `git stash apply ` πŸ“¦ | 165 | | Delete a specific stash | `git stash drop ` πŸ—‘οΈ | 166 | | Delete all stashed changes | `git stash clear` πŸ—‘οΈ | 167 | | Save changes with a message | `git stash push -m "message"` πŸ“¦ | 168 | | Restore and delete recent stash | `git stash pop` πŸ“¦ | 169 | 170 | **Notes**: 171 | - Use `git stash` to save work-in-progress before switching branches. 172 | - Use `git stash apply` to restore changes without removing them from the stash list. 173 | - Use `git stash pop` to apply and remove the most recent stash. 174 | - Use `git stash push -m "message"` to save changes with a descriptive message. 175 | - Use `git stash list` to view all stashed changes. 176 | 177 | --- 178 | 179 | ## 9. Pulling Updates πŸ”„ 180 | 181 | Fetch and merge changes from a remote repository: 182 | 183 | | **Description** | **Command** | 184 | |-------------------------------------|-------------------------------------------------| 185 | | Fetch and merge changes | `git pull` πŸ”„ | 186 | 187 | **Notes**: 188 | - `git pull` is a combination of `git fetch` and `git merge`. 189 | - Use `git pull --rebase` to avoid merge commits and maintain a linear history. 190 | - Always pull changes before pushing to avoid conflicts. 191 | - Use `git fetch` to download changes without merging them. 192 | 193 | --- 194 | 195 | ## 10. GitHub Commands πŸ™ 196 | 197 | Clone and interact with GitHub repositories: 198 | 199 | | **Description** | **Command** | 200 | |-------------------------------------|-------------------------------------------------| 201 | | Clone a repository | `git clone ` πŸ“₯ | 202 | 203 | **Notes**: 204 | - Replace `` with the URL of the repository you want to clone. 205 | - Cloning creates a local copy of the remote repository. 206 | - Use `git clone --branch ` to clone a specific branch. 207 | - Use `git clone --depth 1 ` to clone only the latest commit (shallow clone). 208 | 209 | --- 210 | 211 | ## 11. Cleaning Up 🧹 212 | 213 | Remove untracked files and discard changes: 214 | 215 | | **Description** | **Command** | 216 | |-------------------------------------|-------------------------------------------------| 217 | | Remove untracked files (dry run) | `git clean -dn` 🧹 | 218 | | Delete untracked files | `git clean -df` 🧹 | 219 | | Discard changes in a file | `git restore ` πŸ—‘οΈ | 220 | | Discard all changes | `git restore .` πŸ—‘οΈ | 221 | | Unstage a file | `git restore --staged ` πŸ—‘οΈ | 222 | | Delete a file | `git rm ` πŸ—‘οΈ | 223 | 224 | **Notes**: 225 | - Use `git clean -dn` to preview which files will be deleted. 226 | - Use `git clean -df` to permanently delete untracked files. 227 | - Use `git restore` to discard changes in tracked files. 228 | - Use `git rm` to remove files from the working directory and staging area. 229 | - Use `git restore --staged` to unstage a file without discarding changes. 230 | 231 | --- 232 | 233 | ## 12. Merging Changes πŸ”„ 234 | 235 | Combine changes from different branches: 236 | 237 | | **Description** | **Command** | 238 | |-------------------------------------|-------------------------------------------------| 239 | | Merge changes from a branch | `git merge ` πŸ”„ | 240 | 241 | **Notes**: 242 | - Always ensure you’re on the target branch (e.g., `main`) before merging. 243 | - Resolve merge conflicts if they occur. Use `git status` to identify conflicting files. 244 | - Use `git merge --abort` to cancel a merge in progress. 245 | - Use `git merge --no-ff` to create a merge commit even if the merge is a fast-forward. 246 | 247 | --- 248 | 249 | That's it! You're now ready to use Git like a pro. πŸŽ‰ 250 | --------------------------------------------------------------------------------