|
| 1 | +# Git Commands |
| 2 | + |
| 3 | +## 1. Install git |
| 4 | + |
| 5 | +check version `git --version` |
| 6 | + |
| 7 | +## 2. Configuring some settings |
| 8 | + |
| 9 | +>Like Name, Email, Default Editor & Line Ending |
| 10 | +
|
| 11 | +>Different levels for setting - System, Global & Local. |
| 12 | +
|
| 13 | +```bash |
| 14 | +git config --global user.name "Harsh Verma" |
| 15 | +``` |
| 16 | + |
| 17 | +```bash |
| 18 | +git config --global user.email [email protected] |
| 19 | +``` |
| 20 | + |
| 21 | +```bash |
| 22 | +git config --global core.editor "code --wait" |
| 23 | +``` |
| 24 | + |
| 25 | +```bash |
| 26 | +git config --global -e |
| 27 | +``` |
| 28 | + |
| 29 | +**Line endings -** |
| 30 | + |
| 31 | +| Windows | macOS/Linux | |
| 32 | +| ------- | ----------- | |
| 33 | +| \r\n | \n | |
| 34 | + |
| 35 | +* *For Windows* |
| 36 | +```bash |
| 37 | +git config --global core.autocrlf true |
| 38 | +``` |
| 39 | +* *For Linux* |
| 40 | +```bash |
| 41 | +git config --global core.autocrlf input |
| 42 | +``` |
| 43 | + |
| 44 | +## 3. Taking help |
| 45 | +Use `-h` or `--help` |
| 46 | + |
| 47 | +## 4. Initializing a Repo |
| 48 | + |
| 49 | +```bash |
| 50 | +git init |
| 51 | +``` |
| 52 | + |
| 53 | +**Additional cmds -** |
| 54 | + |
| 55 | +* Make directory - `mkdir Moon` |
| 56 | + |
| 57 | +* Open directory - `cd Moon` |
| 58 | + |
| 59 | +* Open a file - |
| 60 | + * `start .git` *for Windows* |
| 61 | + * `open .git` *for Linux* |
| 62 | + |
| 63 | +* Remove a file - |
| 64 | + * rm -rf .git |
| 65 | + |
| 66 | +* Listing files - |
| 67 | + * ls |
| 68 | + * ls -a |
| 69 | + |
| 70 | +## 5. Staging Area |
| 71 | + |
| 72 | +>For checking the state of repo and staging area |
| 73 | +```bash |
| 74 | +git status |
| 75 | +``` |
| 76 | + |
| 77 | +**Additional cmds -** |
| 78 | + |
| 79 | +* Creating a new file `echo hello > file1.txt` |
| 80 | +* Appending content `echo world >> file1.txt` |
| 81 | +* Adding files to staging area - |
| 82 | + * `git add file1.txt file2.txt` |
| 83 | + * `git add *.txt` |
| 84 | + * `git add .` |
| 85 | + |
| 86 | + |
| 87 | +## 6. Commiting Changes |
| 88 | + |
| 89 | +* Adding short comment |
| 90 | +```bash |
| 91 | +git commit -m "Initial commit" |
| 92 | +``` |
| 93 | +* Adding short and long comment |
| 94 | +```bash |
| 95 | +git commit |
| 96 | +``` |
| 97 | + |
| 98 | +## 7. Skipping the stagging area |
| 99 | + |
| 100 | +>Rarely used |
| 101 | +```bash |
| 102 | +git commit -a -m "Some message" |
| 103 | +``` |
| 104 | +or |
| 105 | +```bash |
| 106 | +git commit -am "Some message" |
| 107 | +``` |
| 108 | + |
| 109 | +## 8. Removing files |
| 110 | + WD : Working Directory |
| 111 | + SA : Staging Area |
| 112 | + |
| 113 | +> Removing file from both WD & SA |
| 114 | +
|
| 115 | +```bash |
| 116 | +git rm file2.txt |
| 117 | +``` |
| 118 | + |
| 119 | +**Additional cmds -** |
| 120 | + |
| 121 | +* File only remove from WD - `rm file2.txt` |
| 122 | +* Checking SA - `git ls-files` |
| 123 | + |
| 124 | +## 9. Renaming or Moving files |
| 125 | + |
| 126 | +> Changes reflected in both WD and SA |
| 127 | +
|
| 128 | +```bash |
| 129 | +git mv file1.txt main.js |
| 130 | +``` |
| 131 | + |
| 132 | +**Additional cmds -** |
| 133 | + |
| 134 | +* Renaming file - `mv file1.txt main.js` |
| 135 | + |
| 136 | +* Adding to SA - `git add file1.txt main.js` |
| 137 | + |
| 138 | +## 10. Ignoring Files |
| 139 | + |
| 140 | +>We can add file names like logs/ *.class *.log bin/ |
| 141 | +
|
| 142 | +```bash |
| 143 | +echo logs/ > .gitignore |
| 144 | +``` |
| 145 | +or |
| 146 | +```bash |
| 147 | +echo > .gitignore |
| 148 | +``` |
| 149 | + |
| 150 | +**Removing a file only from SA -** |
| 151 | +```bash |
| 152 | +git rm --cached bin/ |
| 153 | +``` |
| 154 | +```bash |
| 155 | +git rm --cached -r bin/ |
| 156 | +``` |
| 157 | + |
| 158 | +## 11. Short status |
| 159 | + |
| 160 | +```bash |
| 161 | +git status -s |
| 162 | +``` |
| 163 | + |
| 164 | +## 12. Viewing the staged and unstaged changes |
| 165 | + |
| 166 | +* Staged changes |
| 167 | +```bash |
| 168 | +git diff --staged |
| 169 | +``` |
| 170 | + |
| 171 | +* Unstaged changes |
| 172 | +```bash |
| 173 | +git diff |
| 174 | +``` |
| 175 | + |
| 176 | +**Additional cmds -** |
| 177 | +* Open file in cmd - `cat file2.txt` |
| 178 | +* Open file in cmd with numbers - `cat -n file2.txt` |
| 179 | + |
| 180 | + |
| 181 | +## 13. Using Visual diff Tools |
| 182 | + |
| 183 | +* Configuring VScode- |
| 184 | + |
| 185 | +```bash |
| 186 | +git config --global diff.tool vscode |
| 187 | +``` |
| 188 | +* Configuring launching - |
| 189 | + |
| 190 | +```bash |
| 191 | +git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE" |
| 192 | +``` |
| 193 | +* Viewing unstaged changes - |
| 194 | +```bash |
| 195 | +git difftool |
| 196 | +``` |
| 197 | +* Viewing staged changes - |
| 198 | +```bash |
| 199 | +git difftool --staged |
| 200 | +``` |
| 201 | + |
| 202 | +## 14. Viewing the history |
| 203 | + |
| 204 | +* Full logs - |
| 205 | +```bash |
| 206 | +git log |
| 207 | +``` |
| 208 | +* Logs in single line - |
| 209 | +```bash |
| 210 | +git log --oneline |
| 211 | +``` |
| 212 | +* Reverse Order - |
| 213 | +```bash |
| 214 | +git log --oneline --reverse |
| 215 | +``` |
| 216 | +> **"space"** for moving a head and **"q"** for quitting |
| 217 | +
|
| 218 | +## 15. Viewing the commit |
| 219 | +* Using unique identifier - |
| 220 | +```bash |
| 221 | +git show 921a |
| 222 | +``` |
| 223 | + |
| 224 | +* Viewing Last commit - |
| 225 | +```bash |
| 226 | +git show HEAD |
| 227 | +``` |
| 228 | +* One step back - |
| 229 | +```bash |
| 230 | +git show HEAD~1 |
| 231 | +``` |
| 232 | +* Viewing particular file - |
| 233 | +```bash |
| 234 | +git show HEAD~1:.gitignore |
| 235 | +``` |
| 236 | +* Listing all the files - |
| 237 | +```bash |
| 238 | +git ls-tree HEAD~1 |
| 239 | +``` |
| 240 | + |
| 241 | +## 16. Unstaging the files |
| 242 | +```bash |
| 243 | +git restore --staged file1.txt |
| 244 | +``` |
| 245 | + |
| 246 | +## 17. Discharding local changes |
| 247 | + |
| 248 | +```bash |
| 249 | +git restore . |
| 250 | +``` |
| 251 | + |
| 252 | +**Additional cmds -** |
| 253 | +* Removing all untracked files - |
| 254 | +```bash |
| 255 | +git clean |
| 256 | +``` |
| 257 | +```bash |
| 258 | +git clean -fd |
| 259 | +``` |
| 260 | + |
| 261 | +## 18. Restoring a file to an Earlier Version |
| 262 | + |
| 263 | +```bash |
| 264 | +git restore --source=HEAD~1 file1.js |
| 265 | +``` |
| 266 | + |
| 267 | +--- |
| 268 | +--- |
| 269 | +###### Sources : |
| 270 | +* Videos - |
| 271 | + * [Git Tutorial](https://youtu.be/8JJ101D3knE) |
| 272 | + * [Markdown Formatting](https://youtu.be/bpdvNwvEeSE) |
| 273 | +* Websites - |
| 274 | + * [Basic writing and formatting syntax |
| 275 | +](https://docs.github.com/en/github/writing-on-github/basic-writing-and-formatting-syntax) |
0 commit comments