This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_tutorial.tex
440 lines (409 loc) · 12.7 KB
/
github_tutorial.tex
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
\documentclass[paper=a4, fontsize=12pt]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{setspace}
\usepackage{wrapfig}
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\usepackage{subcaption}
\usepackage{geometry}
\geometry{lmargin=1in,rmargin=1in}
\usepackage{alltt}
\usepackage[english]{babel} % English language/hyphenation
\usepackage[protrusion=true,expansion=true]{microtype}
\usepackage{amsmath,amsfonts,amsthm} % Math packages
\usepackage[pdftex]{graphicx}
\usepackage{url}
\usepackage{placeins}
%%% Listing github codes
\usepackage{listings}
\usepackage{xcolor}
%%% Custom sectioning
\usepackage{sectsty}
\allsectionsfont{\small\scshape}
%%% Custom headers/footers (fancyhdr package)
\usepackage{fancyhdr}
\pagestyle{fancyplain}
\fancyhead{} % No page header
\fancyfoot[L]{\texttt{git help} for help} % Empty
\fancyfoot[C]{} % Empty
\fancyfoot[R]{\thepage} % Pagenumbering
\renewcommand{\headrulewidth}{0pt} % Remove header underlines
\renewcommand{\footrulewidth}{0pt} % Remove footer underlines
\setlength{\headheight}{13.6pt}
%%% Equation and float numbering
%%% Maketitle metadata
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Horizontal rule
\title{
\vspace{-1in}
\usefont{OT1}{bch}{b}{n}
\normalfont \normalsize \textsc{Github} \\ [25pt]
\horrule{0.5pt} \\[0.4cm]
\Large A MINI TUTORIAL \\
\horrule{2pt} \\[0.5cm]
}
\author{
\normalfont \normalsize
Aditya Gore\\[-3pt] \normalsize
\today
}
\date{}
%\usepackage{Sweave}
\begin{document}
\maketitle
%%% Set github code environment
\lstdefinestyle{gitnumber}{
numbers=left,
numberstyle=\tiny,
backgroundcolor=\color{gray!15},
upquote=true,
showstringspaces=false,
morecomment=[l][\color{blue!80}\ttfamily]{https://},
morecomment=[l][\color{blue!80}\ttfamily]{http://}
}
\lstdefinestyle{git}{
backgroundcolor=\color{gray!15},
upquote=true,
showstringspaces=false,
morecomment=[l][\color{blue!80}\ttfamily]{https://},
morecomment=[l][\color{blue!80}\ttfamily]{http://}
}
\lstset{style=git}
\section*{Configure the User Settings}
\begin{enumerate}
\item The commands that are used to configure
\begin{itemize}
\item System wide configuration
\begin{lstlisting}
git config --system
\end{lstlisting}
\item User based configuration
\begin{lstlisting}
git config --global
\end{lstlisting}
\item Project based configuration
\begin{lstlisting}
git config
\end{lstlisting}
\end{itemize}
\item The variables that we can configure are
\begin{itemize}
\item User name
\begin{lstlisting}
git config --global user.name "Your name"
\end{lstlisting}
\item User email address
\begin{lstlisting}
git config --global user.emal "Your email address"
\end{lstlisting}
\item Custom editor
\begin{lstlisting}
git config --global core.editor "Editor"
\end{lstlisting}
\item Git bash colored user interface
\begin{lstlisting}
git config --global color.ui true
\end{lstlisting}
\end{itemize}
\item To view the configurations
\begin{lstlisting}
git config --list
\end{lstlisting}
\item To view the display format
\begin{lstlisting}
echo $PS1
\end{lstlisting}
\item To add keyboard shortcuts
\begin{lstlisting}
git config --global alias.<shortcut> <command>
\end{lstlisting}
\end{enumerate}
\section*{Create Repository w/o Github}
\begin{enumerate}
\item If you want to start off with the project without creating a repository on Github then create the directory called ``xyz'' where you want to start the project and then use git bash to navigate to the folder and once in there use the command
\begin{lstlisting}
git init
\end{lstlisting}
This command will start tracking any changes that are made in the ``xyz'' folder from here on.
\end{enumerate}
\section*{Create Repository on Github}
\begin{enumerate}
\item Go to \url{https://github.com/} and create a repository ``xyz''.
\item Once created click on clone or download button and get a url link to the project which will look like \url{https://github.com/yourname/xyz.git}.
\item Open the github bash in folder where you want the project and use the following command to clone the project.
\begin{lstlisting}
git clone https://github.com/yourname/xyz.git
\end{lstlisting}
\item This will create a folder called ``xyz'' in the directory you used the command.
\end{enumerate}
\section*{Making commits}
\begin{enumerate}
\item To add all the changes to the ``staged'' index use the command
\begin{lstlisting}
git add .
\end{lstlisting}
\item To commit the changes to the same ``branch''
\begin{lstlisting}
git commit -m "Write down the message about the commit"
\end{lstlisting}
\item To remove a deleted file (working directory) from the repository
\begin{lstlisting}
git rm <file>
\end{lstlisting}
\item To move or rename a file
\begin{lstlisting}
git mv <file1> <file2>
\end{lstlisting}
\item To add and commit at the same time
\begin{lstlisting}
git commit -am "write the message here"
\end{lstlisting}
\end{enumerate}
\section*{Viewing commits and logs}
\begin{enumerate}
\item To view commits made so far
\begin{lstlisting}
git log
\end{lstlisting}
\item To view past $3$ commits
\begin{lstlisting}
git log -n 3
\end{lstlisting}
\item We can use time filter to view the commits
\begin{lstlisting}
git log --since=2016-09-15 --until==2016-09-22
\end{lstlisting}
\item We can use author filter to view the commits
\begin{lstlisting}
git log --author="Aditya"
\end{lstlisting}
The author can be matched on partial string but is case sensitive
\item To filter by words in the commit message
\begin{lstlisting}
git log --grep=<regex>
\end{lstlisting}
\item To check the commits from ``HEAD'' going backwards.
\begin{lstlisting}
git log HEAD
\end{lstlisting}
\item Useful options to use with git log
\begin{lstlisting}
git log --oneline
git log --oneline -3 (previous 3 commits)
git log --oneline HEAD~3..HEAD~1
git log HEAD~10.. filename (to see all the commits made for that file)
git log -p HEAD~10.. filename (to see the changes to the file)
git log --stat --summary
git log --format=oneline
git log --format=short
git log --format=full
git log --format=fuller
git log --format=email
git log --graph
git log --oneline --graph --all --decorate
\end{lstlisting}
\item To see the changes from a previous commit
\begin{lstlisting}
git show HEAD~2
\end{lstlisting}
\item To compare the commits
\begin{lstlisting}
git diff HEAD~2 <file>
git diff HEAD~3..HEAD~1 <file>
\end{lstlisting}
\item To compare branches
\begin{lstlisting}
git diff <branch>.<commit>..<branch>.<commit>
\end{lstlisting}
commitname is optional
\end{enumerate}
\section*{Managing repositories/commits}
\begin{enumerate}
\item To check the status of the current branch
\begin{lstlisting}
git status
\end{lstlisting}
It gives the difference between the working directory, staging index and git repository.
\item To see the differences between the repository and working directory, file by file
\begin{lstlisting}
git diff <file><optional>
\end{lstlisting}
\item To view the differences between the git repository and staging index, file by file
\begin{lstlisting}
git diff --staged
\end{lstlisting}
\item To view changes side by side
\begin{lstlisting}
git diff --color-words <file>
\end{lstlisting}
\item Undo changes in the working directory
\begin{lstlisting}
git checkout -- <file>
\end{lstlisting}
This will checkout the the files from the repository at the current branch to the working directory.
\item Undo changes in the staging index
\begin{lstlisting}
git reset HEAD <file>
\end{lstlisting}
This will remove the file from staging index but would not modify the file in the working directory. If filename is not provided then it will remove all the files from the staging index without modifying the files in the working directory.
\item To amend the commit (can only change the most recent commit)
\begin{lstlisting}
git add <file>
git commit --amend -m "write the message from the recent commit"
\end{lstlisting}
This changes the most recent commit (the one that HEAD points to)
\item To revert changes made to a file to an older commit
\begin{lstlisting}
git checkout <commit> -- <file>
git commit -m "Revert the changes to an older commit"
\end{lstlisting}
2afd5fd3b0 is the SHA for the commit where you want to revert to. The first command will checkout the file in the staging index.
\item To revert to the changes in one single step
\begin{lstlisting}
git revert <commit>
\end{lstlisting}
\item Another way to undo a commit.
\begin{lstlisting}
git reset --soft <commit>
\end{lstlisting}
The $--$soft option just moves the HEAD pointer to the repository. The staging index and the working directory remains unchanged.\\
The $--$mixed option moves the HEAD pointer to the repository and also modifies the staging index to reflect the repository. It does not change the working directory.\\
The $--$hard option moves the HEAD pointer to the repository and makes the changes to the staging index and working directory to reflect the repository.
\item Removing untracked files
\begin{lstlisting}
git clean -n
git clean -f
\end{lstlisting}
-n tells you what it would do.
-f forces the command to remove the untracked files.
\item To ignore tracked files
\begin{lstlisting}
git rm --cached <file>
\end{lstlisting}
First remove it from the staging index. Then add the filename to the .gitignore file.
\item To track an empty directory
Add a file called .gitkeep
\end{enumerate}
\section*{Referencing commits}
\begin{enumerate}
\item To see the the tree-ish structure of commit
\begin{lstlisting}
git ls-tree HEAD~2
\end{lstlisting}
This will show you the list of files in grand parent (second parent) of the commit where HEAD is pointing at.
\item To check if one branch is completely contained in other branch
\begin{lstlisting}
git branch --merged
\end{lstlisting}
This will show the a list of branches that contains the same commits as the current branch.
\end{enumerate}
\section*{Creating and Managing Branches}
\begin{enumerate}
\item To view the current branch
\begin{lstlisting}
git branch
\end{lstlisting}
\item To create a new branch
\begin{lstlisting}
git branch <branch>
\end{lstlisting}
Just creates a new branch but does not switch to it.
\item To switch to a branch
\begin{lstlisting}
git checkout <branch>
\end{lstlisting}
\item To create and switch to branch
\begin{lstlisting}
git checkout -b <branch>
\end{lstlisting}
\item To rename a branch
\begin{lstlisting}
git branch -m <branch1> <branch2>
\end{lstlisting}
\item To delete branch
\begin{lstlisting}
git branch -d <branch>
\end{lstlisting}
Will delete branch if it is not current or if there are no children, unless all the children commits are merged into current branch.
\item To merge two branches
\begin{lstlisting}
git checkout <branch1>
git merge <branch2>
\end{lstlisting}
This will merge branch2 into branch1
\item To abort a conflicting merge
\begin{lstlisting}
git merge --abort
\end{lstlisting}
\item To switch branches before commiting the changes
\begin{lstlisting}
git stash save "write the message here"
\end{lstlisting}
\item To vew the stash
\begin{lstlisting}
git stash list
\end{lstlisting}
\item To see the modifications in the stash
\begin{lstlisting}
git stash show -p <stash>
\end{lstlisting}
\item To retrieve the file from stash
\begin{lstlisting}
git stash apply <stash>
git stash pop <stash>
\end{lstlisting}
apply command just retrieves whereas pop command retrieves and deletes the file from stash
\item To delete item from the stash
\begin{lstlisting}
git stash drop <stash>
\end{lstlisting}
\item To delete all the items from the stash
\begin{lstlisting}
git stash clear
\end{lstlisting}
\end{enumerate}
\section*{Working with remote repositories}
\begin{enumerate}
\item To see the remote repository
\begin{lstlisting}
git remote
\end{lstlisting}
\item To add a remote to current local repository
\begin{lstlisting}
git remote add <alias> <url>
\end{lstlisting}
\item To get more information about the remote
\begin{lstlisting}
git remote -v
\end{lstlisting}
\item To push a branch to a remote repository
\begin{lstlisting}
git push -u <remote> <branch>
\end{lstlisting}
The -u options is to make sure that the remote repository is tracked.
\item To start tracking a non tracking branch
\begin{lstlisting}
git branch --set-upstream <branch> <remote>/<branch>
\end{lstlisting}
\item To sync a local copy of remote repository
\begin{lstlisting}
git fetch <remote> <branch>
\end{lstlisting}
\item To merge the local copy of remote repository with local branch
\begin{lstlisting}
git merge <remote>/<branch>
\end{lstlisting}
\item To sync and merge the local copy of remote repository with local branch
\begin{lstlisting}
git pull
\end{lstlisting}
\item To checkout a branch from a local copy of remote repository
\begin{lstlisting}
git branch <branch> <remote>/<branch>
\end{lstlisting}
\item To delete a remote branch
\begin{lstlisting}
git push <remote> :<branch>
git push --delete <branch>
\end{lstlisting}
\end{enumerate}
\end{document}