-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_cheat_sheet.tex
237 lines (190 loc) · 11.5 KB
/
git_cheat_sheet.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
\documentclass[10pt,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage{ifthen}
\usepackage[landscape]{geometry}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{bm}
\usepackage{mdframed}
\usepackage{soul}
\usepackage{listings}
% To make this come out properly in landscape mode, do one of the following
% 1.
% pdflatex latexsheet.tex
%
% 2.
% latex latexsheet.tex
% dvips -P pdf -t landscape latexsheet.dvi
% ps2pdf latexsheet.ps
% If you're reading this, be prepared for confusion. Making this was
% a learning experience for me, and it shows. Much of the placement
% was hacked in; if you make it better, let me know...
% 2008-04
% Changed page margin code to use the geometry package. Also added code for
% conditional page margins, depending on paper size. Thanks to Uwe Ziegenhagen
% for the suggestions.
% 2006-08
% Made changes based on suggestions from Gene Cooperman. <gene at ccs.neu.edu>
% To Do:
% \listoffigures \listoftables
% \setcounter{secnumdepth}{0}
% This sets page margins to .5 inch if using letter paper, and to 1cm
% if using A4 paper. (This probably isn't strictly necessary.)
% If using another size paper, use default 1cm margins.
\ifthenelse{\lengthtest { \paperwidth = 11in}}
{ \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
}
% Turn off header and footer
\pagestyle{empty}
% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%x
{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
{-1explus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%
{\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\small\bfseries}}
\makeatother
% Define BibTeX command
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
% Don't print section numbers
\setcounter{secnumdepth}{0}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}
\newcommand{\hlc}[2][yellow]{ {\sethlcolor{#1} \hl{#2}} }
% -----------------------------------------------------------------------
\begin{document}
\raggedright
\footnotesize
\begin{multicols}{3}
% multicol parameters
% These lengths are set only within the two main columns
%\setlength{\columnseprule}{0.25pt}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}
\begin{center}
\Large{Git Cheat Sheet} \\
\end{center}
\section{Configuration}
\subsection{User Information}
Git uses a set of user name and user email to sign the commit messages developed by the developer, as such these are required fields that are better configured globally when working from a personal or dedicated computer.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git config --global user.name "[your_name]"
$ git config --global user.email [your email]
\end{lstlisting}
Important to notice that if using your full name when setting the user name the command should not forget to use the "" to enclose a string that accepts spaces on it.
\subsection{Text Editor}
You can configure the default text editor that will be used when Git needs you to type in a message. If not configured, Git uses your system’s default editor.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git config --global core.editor [editor]
\end{lstlisting}
\subsection{Merge Tool}
The right merging tool can save a lot of time and errors when having to merge non fast-forward codes. There is a plethora of merge tools available, however I strongly recommend meld for its intuitive use and simple but effective graphical capability.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git config --global merge.tool [tool_name]
\end{lstlisting}
\subsection{Save on Typing}
If you are using HTTPS URL to push your changes to a given repository the Git server will ask for confirmation through your username and password for every command accessing the repo. The command below will store the credentials in memory for a few minutes and will perform the validation automatically. The automatically validation process only happens after an initial manual validation and will last for a couple of minutes.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git config --global credential.helper cache
\end{lstlisting}
\section{Working with Remotes}
\subsection{Tracking }
Checking out a local branch from a remote-tracking branch automatically creates what is called a “tracking branch” (and the branch it tracks is called an “upstream branch”). Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.
\subsection{Cloning Remotes}
The command display a list of the commits in the repository. By default it shows the SHA-1 number , the commit message and the author of the commit. Of relevant use is the option \hlc[lightgray]{- -oneline} which display a brief version of the above message description that consist of the SHA-1 number and the commit message
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git clone
https://github.com/dmiralles2009/git_cheat_sheet.git
\end{lstlisting}
The command is ideal when trying to go back to previous states as its shows the SHA-1 number required for the checkout action.
\subsection{Adding Remotes}
Git repositories can be hosted locally or remotely. Git makes an special dsitinction for repositories hosted remotely in its interface. When a repository is created locally and it is desired to hosted remotely after its creation, a remote addition is required with:
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git remote add <remote_name> <url>
\end{lstlisting}
Notice that traditionally the remote name is defined as \hlc[lightgray]{origin}, but this is only and identifier. Nothing stops you from using another name for your remote repositories.
\subsection{Inspecting Remotes}
The command is ideal when trying to get some information about the remote \hlc[lightgray]{\textless remote\_name \textgreater}. The command prints a relationship of the remote branches that are being tracked and its local origins.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git remote show <remote_name>
\end{lstlisting}
\section{Local and Remote Branches}
Checking out a local branch from a remote-tracking branch automatically creates what is called a “tracking branch” (and the branch it tracks is called an “upstream branch”). Tracking branches are local branches that have a direct relationship to a remote branch.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git checkout -b [branch] [remote]/[branch]
\end{lstlisting}
This command is ideal when for example checking out a branch to perform a bug fix, because it will set the upstream state automatically.
\subsection{Deleting Branches}
\subsubsection{Remotes Branches}
After work on a given branch has been completed, and the user wants to delete that deprecated branch, the git push command can be used for deletions as in:
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git push origin --delete <branch_name>
\end{lstlisting}
Notice that \hlc[lightgray]{branch\_name} refers to the remote branch that is targeted in the deletion.
\subsubsection{Local Branches}
After work on a given branch has been completed, and the user wants to delete that deprecated branch, the git push command can be used for deletions as in:
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git branch (-d | -D) [-r] <branch_name>
\end{lstlisting}
The -d option delete the branch only if it is fully merged to its tracking or upstream branch, however the -D option deletes it regarding its state. In here the \hlc[lightgray]{branch\_name} refers to the local branch that is targeted in the deletion.
\section{Additional Tools}
\subsection{Stashing}
Stashing lets to record the current state of the working directory and the index locally without affecting the HEAD pointer. This is very useful when you want to save the code (you do not consider it ready to commit) but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git stash save
\end{lstlisting}
The previous command will save your work locally so that you can easily change your effort to other branches if needed.
Once work has been completed
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git stash list
\end{lstlisting}
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git stash apply stash@{[stash_number]}
\end{lstlisting}
\subsection{Renaming, copying and removing}
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git log <options>
\end{lstlisting}
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git mv [old_file_name] [new_file_name]
\end{lstlisting}
Git is not very good in renaming automatically things, in order to do so a move operation is performed
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git rm [options] <file_name>
\end{lstlisting}
Of particular interest is the option \hlc[lightgray]{- - cached}, which will remove the file from the git repository but keep it in your file system.
\subsection{Logs}
The command display a list of the commits in the repository. By default it shows the SHA-1 number , the commit message and the author of the commit. Of relevant use is the option \hlc[lightgray]{- -oneline} which display a brief version of the above message description that consist of the SHA-1 number and the commit message
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ git log <options>
\end{lstlisting}
The command is ideal when trying to go back to previous states as its shows the SHA-1 number required for the checkout action.
\subsection{gitk}
Even though is not officially part of the Git software, the gitk tool is an excellent visualizer of a repository history. Simply run gitk on the terminal and a display with all commit history will be displayed. However one of the most useful features of gitk is to show the difference between commits given its SHA-1 number.
\begin{lstlisting}[language=bash, backgroundcolor = \color{lightgray}]
$ gitk [from] [to]
\end{lstlisting}
In here from and to refer to the SHA-1 number of the targeting commits.
\rule{0.3\linewidth}{0.25pt}
\scriptsize
Copyright \copyright\ 2016 Damian Miralles
\href{https://github.com/dmiralles2009/Prelims.git}{https://github.com/dmiralles2009/Prelims.git} \newline
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
\href{http://creativecommons.org/licenses/by-nc-sa/3.0/}{License}
\end{multicols}
\end{document}