Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
373 lines (362 loc) · 18.8 KB
/
Copy pathindex.html
File metadata and controls
373 lines (362 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description"
content="A beginner-friendly blog that explains Git & GitHub commands, workflows, interview questions, and best practices.">
<title>Git & Github</title>
<link rel="icon" href="/github.png" type="image/x-icon">
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<h1>Git & Github Notes</h1>
<table>
<caption>Common Git Commands</caption>
<thead>
<tr>
<th scope="col">Command</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git init</code></td>
<td>Initialize a new Git repository. Git creates a hidden directory called .git that stores all the
objects and refs that Git uses.</td>
</tr>
<tr>
<td><code>git add [file]</code></td>
<td>Add changes in the specified [file] to the staging area.</td>
</tr>
<tr>
<td><code>git add .</code></td>
<td>Add all changes in the current directory to the staging area.</td>
</tr>
<tr>
<td><code>git commit -m "message"</code></td>
<td>Commit (save) the staged changes with a "message".</td>
</tr>
<tr>
<td><code>git remote add origin [url]</code></td>
<td>Add a remote repository with the name "origin".</td>
</tr>
<tr>
<td><code>git config --global user.name "Your Name"</code></td>
<td>Set your Git username.</td>
</tr>
<tr>
<td><code>git clone [url]</code></td>
<td>Clone a repository from a remote URL.</td>
</tr>
<tr>
<td><code>git remote -v</code></td>
<td>List all remote repositories.</td>
</tr>
<tr>
<td><code>git branch</code></td>
<td>List all branches in the repository.</td>
</tr>
<tr>
<td><code>git branch -M [Branch Name]</code></td>
<td>Rename the current branch to [Branch Name]</td>
</tr>
<tr>
<td><code>git checkout [branch]</code></td>
<td>Switch to the specified branch.</td>
</tr>
<tr>
<td><code>git checkout -b [branch]</code></td>
<td>Create and switch to a new [branch]</td>
</tr>
<tr>
<td><code>git push origin [branch]</code></td>
<td>Push changes to the specified branch on the remote repository.</td>
</tr>
<tr>
<td><code>git push origin main --force</code></td>
<td>Force push to GitHub to overwrite the previous pushed commit.</td>
</tr>
<tr>
<td><code>git pull origin [branch]</code></td>
<td>Fetch and merge changes from the specified branch on the remote repository.</td>
</tr>
<tr>
<td><code>git status</code></td>
<td>Show the status of changes in the working directory and staging area.</td>
</tr>
<tr>
<td><code>git reset</code></td>
<td>Undo commits</td>
</tr>
<tr>
<td><code>git log</code></td>
<td>View commit history</td>
</tr>
<tr>
<td><code>git merge [branch]</code></td>
<td>Merge another [branch] into current one</td>
</tr>
<tr>
<td><code>git commit --amend</code></td>
<td>Modify the last commit message or include additional changes.</td>
</tr>
<tr>
<td><code>git reset --soft HEAD~1</code></td>
<td>Undo the last commit but keep changes staged.</td>
</tr>
<tr>
<td><code>git reset --mixed HEAD~1</code></td>
<td>Undo the last commit and unstage changes.</td>
</tr>
<tr>
<td><code>git reset --hard HEAD~1</code></td>
<td>Undo the last commit and <b>discard all changes (dangerous)</b>.</td>
</tr>
<tr>
<td><code>git restore [file]</code></td>
<td>Discard changes in the working directory for a file (safe).</td>
</tr>
<tr>
<td><code>git restore --staged [file]</code></td>
<td>Unstage a file while keeping changes in the working directory.</td>
</tr>
<tr>
<td><code>git revert [commit-hash]</code></td>
<td>Create a new commit that reverses the effects of a previous commit. (Safe for shared branches)
</td>
</tr>
<tr>
<td><code>git clean -fd</code></td>
<td>Remove untracked files and directories. (⚠️ Use with caution)</td>
</tr>
<tr>
<td><code>git stash</code></td>
<td>Temporarily save uncommitted changes</td>
</tr>
<tr>
<td><code>git stash pop</code></td>
<td>Apply stashed changes and remove the stash.</td>
</tr>
<tr>
<td><code>git stash apply</code></td>
<td>Apply stashed changes but keep the stash for reuse.</td>
</tr>
</tbody>
</table>
<section>
<p>This is a shortlist of the most commonly used Git commands. For beginners, the first 16 are enough and
widely used by most GitHub users.</p>
<p><u>Fork</u> means copying someone else's repository to your own GitHub account so you can develop or
contribute from your side. Later, you can send a pull request to the original owner for merging.</p>
<em><strong>Note:</strong> If you want to use GitHub Pages for hosting, ensure your homepage file is named
'index.html'</em>
</section>
<article>
<h2>♠ Interview Questions</h2>
<dl>
<dt>1. What is Git and What are the advantages of using Git?</dt>
<dd>
<p>
Answer: Git is a distributed version control system (VCS) that helps developers track changes in
source code and collaborate effectively.
</p>
<h5>Advantages of using Git:</h5>
<ul>
<li>Tracks every change with detailed history.</li>
<li>Supports branching and merging for parallel development.</li>
<li>Works offline (distributed).</li>
<li>Fast and efficient performance.</li>
<li>Allows easy collaboration via platforms like GitHub or GitLab.</li>
</ul>
</dd>
<dt>2. What is the difference between Git and GitHub?</dt>
<dd>
<p>
Answer: Git is a local version control system, while GitHub is a cloud-based platform for
hosting Git repositories.
</p>
<h5>Key Differences:</h5>
<ul>
<li>Git: Tracks changes in code locally on your computer.</li>
<li>GitHub: Allows you to share code online, collaborate, and manage repositories.</li>
</ul>
</dd>
<dt>3. How do you create a new branch in Git?</dt>
<dd>
<p>
Answer: You can create and switch to a new branch using the command below:
</p>
<ul>
<li><code>git checkout -b [new-branch-name]</code></li>
</ul>
<h3>Or, you can create and switch in two steps:</h3>
<ul>
<li><code>git branch [new-branch-name]</code></li>
<li><code>git checkout [new-branch-name]</code></li>
</ul>
</dd>
<dt>4. What is a merge conflict and how do you resolve it?</dt>
<dd>
<p>
Answer: A merge conflict occurs when Git cannot automatically merge changes because the same
part of a file was modified differently in two branches.
</p>
<h5>To resolve it:</h5>
<ul>
<li>Git highlights the conflict markers in the file.</li>
<li>You manually edit the file to choose or combine changes.</li>
<li>Then, stage and commit the resolved file.</li>
</ul>
<ul>
<li><code>git add [filename]</code></li>
<li><code>git commit</code></li>
</ul>
</dd>
<dt>5. Explain the purpose of the staging area in Git.</dt>
<dd>
<p>
Answer: The staging area in Git is a place where changes are prepared before committing. It
allows you to review and control exactly what will go into your next commit.
</p>
</dd>
<dt>6. How do you revert a commit in Git?</dt>
<dd>
<p>
Answer: You can undo changes from a specific commit using:
</p>
<ul>
<li><code>git revert <commit-hash></code> <span class="comment">// creates a new commit
that reverses the changes.</span>
</li>
</ul>
<h5>To completely remove a commit (use with caution):</h5>
<ul>
<li><code>git reset --hard HEAD~1</code></li>
</ul>
</dd>
<dt>7. What is the purpose of the <code>.gitignore</code> file?</dt>
<dd>
<p>
Answer: The .gitignore file tells Git which files or directories to ignore. It helps
keep unnecessary, temporary, or sensitive files out of your repository.
</p>
<h5>Common examples:</h5>
<ul>
<li><code>node_modules/</code></li>
<li><code>.env</code></li>
<li><code>dist/</code></li>
</ul>
</dd>
<dt>8. How can you view the commit history in Git?</dt>
<dd>
<p>
Answer: You can use the following commands to view commit history:
</p>
<ul>
<li><code>git log</code> <span class="comment">// shows detailed commit information.</span></li>
<li><code>git log --oneline</code> <span class="comment">// displays a compact view with one
line per commit.</span></li>
<li><code>git log --graph</code> <span class="comment">// shows a visual representation of the
branch structure.</span></li>
<li><code>git log --oneline --graph</code> <span class="comment">// combines both compact view
and graph for a clear summary.</span></li>
</ul>
</dd>
</dl>
</article>
<article>
<h2>Basic things of git and github</h2>
<ul>
<li><strong>Repository:</strong> A folder where Git tracks your project and its history.</li>
<li><strong>Clone:</strong> Make a copy of a remote repository on your computer.</li>
<li><strong>Stage:</strong> Tell Git which changes you want to save next.</li>
<li><strong>Commit:</strong> Save a snapshot of your staged changes.</li>
<li><strong>Branch:</strong> Work on different versions or features at the same time.</li>
<li><strong>Merge:</strong> Combine changes from different branches.</li>
<li><strong>Pull:</strong> Get the latest changes from a remote repository.</li>
<li><strong>Push:</strong> Send your changes to a remote repository.</li>
</ul>
<h3>Basic process of creating github repository</h3>
<ul>
<li>Create a repository from github's website</li>
<li>from vscode terminal run this command_</li>
<li><code>git init</code></li>
<li><code>git add .</code></li>
<li><code>git commit -m "message"</code></li>
<li><code>git branch -M [Branch Name]</code></li>
<li><code>git remote add origin [url]</code></li>
<li><code>git push -u origin [Branch Name]</code></li>
</ul>
<h3>Or connect your github account with vscode to use the inbuild feature</h3>
<ul>
<li>Go to source control tab</li>
<li>Initialize repository</li>
<li>Give commit message and press the commit button</li>
<li>Pubish branch (Choose repository type public/private)</li>
</ul>
<h2>Save or commit the further changes to github repository</h2>
<ul>
<li><code>git add .</code></li>
<li><code>git commit -m "message"</code></li>
<li><code>git push -u origin [Branch Name]</code></li>
</ul>
<h3>Or directly from vscode</h3>
<ul>
<li>Go to source control tab</li>
<li>Initialize repository</li>
<li>Give commit message and press the commit button</li>
<li>Finally sync all changes</li>
</ul>
<h3>How to overwrite recent commit <small>(To avoiding creatation of unnecessary commit)</small></h3>
<ul>
<li><code>git add .</code></li>
<li><code>git commit --amend --no-edit</code><p class="comment">// git commit --amend → replaces (rewrites) the most recent commit with whatever is currently staged</p><p class="comment">// --no-edit → tells Git “don't open the editor, just keep the same commit message as before.” <br> (Don't write if you want to change the commit message also)</p></li>
<li><code>git push origin main --force</code></li>
</ul>
<h3>How to create a new repo keeping the commit history of a old repo</h3>
<ul>
<li>Open a folder</li>
<li><code>git clone [old repository clone url]</code></li>
<li>Create a repository manually</li>
<li><code>git remote set-url [new repository link]</code><p class="comment">// Replace the current remote URL with the new repository URL</p></li>
<li><code>git push -u origin main</code></li>
</ul>
<h3>How to push code with local commits into an existing repository</h3>
<ul>
<li>If there is no remote configured yet, use:<br /><code>git remote add origin [repository url]</code><p class="comment">// Add a new remote repository and name it 'origin'</p></li>
<li>Else if replacing an existing remote, use:<br /><code>git remote set-url origin [repository url]</code><p class="comment">// Update the existing 'origin' remote to point to a new repository URL</p></li>
<li><code>git add .</code></li>
<li><code>git commit -m "message"</code></li>
<li><code>git fetch origin</code><p class="comment">// Download the latest changes from the remote repository without modifying your local branch</p></li>
<li>If you want to overwrite the previous commits: <br/> <code>git push -u origin main --force</code><p class="comment">// Upload your local commits to the remote repository</p></li>
<li>Else if you want to keep the previous commits: <br /> <code>git merge origin/main --allow-unrelated-histories</code> <p class="comment">// Merge the latest remote changes into your current branch and resolve conflict</p></li>
<li><code>git push -u origin main</code><p class="comment">// Upload your local commits to the remote repository</p></li>
</ul>
<h3>How to push code into a new branch</h3>
<ul>
<li>Clone a repository and do edit</li>
<li><code>git checkout -b [Branch Name]</code><p class="comment">// create a new branch named "Branch Name" and switch in that branch</p></li>
<li><code>git add .</code></li>
<li><code>git commit -m "message"</code></li>
<li><code>git push -u origin [Branch Name]</code></li>
</ul>
<h2>Git Best Practices</h2>
<ul>
<li>Write meaningful commit messages</li>
<li>Use branches for features and fixes</li>
<li>Pull changes before pushing</li>
<li>Use <code>.gitignore</code> to avoid committing sensitive or unnecessary files</li>
<li>Never use <code>git reset --hard</code> on shared branches</li>
</ul>
</article>
</main>
<footer>
<p>This Blog-page is created by - Pratush - <a href="https://pratushportfolio.vercel.app/" target="_blank"
rel="noopener">pratushportfolio.vercel.app</a></p>
<p>If you are interested to learn more then checkout this website - <a href="https://www.w3schools.com/git/"
target="_blank" rel="noopener">w3schools.com/git</a></p>
</footer>
</body>
</html>