<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_definitions}
\input{common_latex/common_latex_help_format}
%===================================================================

\begin{document}

\begin{labcover}
\labtitle{Help Topic: The Git Add and Remove Commands}
\vspace{0.1in}
\labdate{Fall 2024}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Oscar Viquez, oviquezr@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labline
\end{labcover}

\wikidoc{file=Help.GitAddRemove}
\wikidoc{auth=mikerb@mit.edu}

%====================================================================
\section*{The Git \cmd{add} and \cmd{remove} Commands}
%====================================================================

The \cmd{git add} command allows you add files or a directories to
your locally checked out tree in preparation for committing them to
the Git server. And as you may guess, the \cmd{git rm} command tags a
file in your local checkout for removal on the server upon your next
commit. More info on \cmd{git add} and \cmd{git rm} can always be
found by Googling "Git book" and reading the full PDF online
free, or just typing \cmd{git help add} anytime on the command
line.

%---------------------------------------------------------------------
\subsection*{Basic usage of the git \cmd{add} command}

The basic syntax for the \cmd{git add} command is:

\vspace{0.05in}
\begin{consoleverb}
 $ git add my_file.cpp my_file.h        #(one or more files)
 $ git add my_project/                  #(a directory)
\end{consoleverb}
\vspace{0.15in}

\noindent
The \cmd{git add} command does {\em not} require a network connection
to the server and only declares an intention to add file(s) to the
master copy on the Git server upon the next commit and push. Since
\cmd{git status} also doesn't require a network connection to the
server, you can confirm the results of your \cmd{add} immediately:

\vspace{0.1in}
\begin{consoleverb}
 $ git status
 Your branch is up to date with 'origin/main'.

 No commits yet

 Changes to be committed:
   (use "git rm --cached &lt;file&gt;..." to unstage)
     new file:   my_file.cpp
     new file:   my_file.h
     new file:   my_project/my_project.cpp
     new file:   my_project/my_project.h
\end{consoleverb}
\vspace{0.1in}

\noindent
Note: When adding a directory, the operation is applied recursively to all
files and subdirectories.

\vspace{0.05in}
\noindent
Note (1): When adding a file, don't be afraid to use the command line conveniences
such as \cmd{git add *.pdf}. All the previously unversioned PDF files will now
be scheduled for addition.

\vspace{0.05in}
\noindent
Note (2): Git does not track empty directories, only files. If you need to
ensure the availability of an empty directory in your repository, you can
create a \texttt{.gitignore} file within that directory, and add that file
to version control.

%---------------------------------------------------------------------
\subsection*{Basic usage of the Git \cmd{remove} command}

The basic syntax for the \cmd{git rm} command (\cmd{rm} for
``remove'') is:

\vspace{0.05in}
\begin{consoleverb}
 $ git rm my_file.cpp my_file.h        #(one or more files)
 $ git rm -r my_project/               #(a directory; use -r for recursive)
\end{consoleverb}
\vspace{0.15in}

\noindent
The \cmd{git rm} command does {\em not} require a network connection
to the server and only declares an intention to remove the file(s)
from the master copy on the Git server upon the next commit. It will
also delete the local copy.

\vspace{0.1in}
\noindent
Note (1): If removing files with staged changes (to be committed later),
the \cmd{git rm} command will produce an error. The message will point
out the files that have been staged and inform you that you can either:

\begin{itemize}
\item Unstage the file with the \cmd{git rm --cached &lt;file&gt;} command,
but keep the local copy.

\item Force removal of the file with the \cmd{git rm -f &lt;file&gt;} command,
which will unstage the file and remove the local copy.
\end{itemize}

\vspace{0.1in}
\noindent
Note (2): If removing files that are unversioned (not under version
control), the \cmd{git rm} command will produce an error. The message
will point out that Git does not currently track any information about
that file and thus cannot remove it from the repository. The local
file can be removed using the (non-git) \cmd{rm} command.

\vspace{0.05in}
\begin{consoleverb}
 $ touch test.txt
 $ git rm test.txt
 fatal: pathspec 'test.txt' did not match any files
 $ rm test.txt
\end{consoleverb}
\vspace{0.15in}


\end{document}

</pre></body></html>